Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 32 of 32

Thread: Issues with return

  1. #26
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    that looks like it will work

  2. The Following User Says Thank You to camel-man For This Useful Post:

    TheMatrixMage (September 18th, 2014)

  3. #27
    Junior Member
    Join Date
    Sep 2014
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Issues with return

    Also now when I print testing I get the object label. I can change that using a toString method of some sort right? I want to get it to tell me the coordinates right back, but through printing the object instead of the coordinates individually if possible.

  4. #28
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    you will have to make your own method to tell you the coordinates. Something like

     
    public String toString(){
    return x1 +" " +x2 + " " +.... all the values.
     
    }
     
    Then in your main you can say System.out.print(testing.toString());
     
    and it will give you the coordinates

  5. #29
    Junior Member
    Join Date
    Sep 2014
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Issues with return

    Now how would I call upon translate from the initial code to shift all the coordinates left 3 and up 5?

  6. #30
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    You would call it like you do any other method. testing.translate();
    Although you need to give the shiftX and shiftY values . Right now in your Rawr class they do not have any.

  7. #31
    Junior Member
    Join Date
    Sep 2014
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Issues with return

    Awesome, got that part working
    So now I need to be able to send the coordinates to calculate every side of the polygon through sideLength1to2 and then have them use perimeter() directly after to calculate the perimeter of the translated polygon.

    Is there a way to get it to have it take the coordinates or do I need to use a similar setup as with the previous?

    --- Update ---

    import java.lang.Math;


    public class Rawr {
    private double x1;
    private double x2;
    private double x3;
    private double x4;
    private double y1;
    private double y2;
    private double y3;
    private double y4;
    private double shiftX;
    private double shiftY;
    private double s1;
    private double s2;
    private double s3;
    private double s4;


    public Rawr(){
    x1 = 0;
    x2 = 0;
    x3 = 0;
    x4 = 0;
    y1 = 0;
    y2 = 0;
    y3 = 0;
    y4 = 0;

    }

    public void setValues(double newX1, double newX2, double newX3, double newX4, double newY1, double newY2, double newY3, double newY4){
    x1 = newX1;
    x2 = newX2;
    x3 = newX3;
    x4 = newX4;
    y1 = newY1;
    y2 = newY2;
    y3 = newY3;
    y4 = newY4;
    }


    public void translate(double shiftX, double shiftY){
    x1 = (x1 + shiftX);
    x2 = (x2 + shiftX);
    x3 = (x3 + shiftX);
    x4 = (x4 + shiftX);
    y1 = (y1 + shiftY);
    y2 = (y2 + shiftY);
    y3 = (y3 + shiftY);
    y4 = (y4 + shiftY);

    }

    private void sideLength1to2(double x1, double x2, double x3, double x4, double y1, double y2, double y3, double y4){
    double s1 = Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1) * (y2 - y1));
    double s2 = Math.sqrt((x3 - x2)*(x3 - x2) + (y3 - y2) * (y3 - y2));
    double s3 = Math.sqrt((x4 - x3)*(x4 - x3) + (y4 - y3) * (y4 - y3));
    double s4 = Math.sqrt((x1 - x4)*(x1 - x4) + (y1 - y4) * (y1 - y4));
    return;
    }

    private void perimeter(){
    double p = s1 + s2 + s3 + s4;
    }
    public String toString(){
    return "(" + x1 + "," + y1 + ")" + "(" + x2 + "," + y2 + ")" + "(" + x3 + "," + y3 + ")" + "(" + x4 + "," + y4 + ")";
    }

    }


    Tester


    public class RawrTester {
    public static void main(String[] args){

    Rawr testing = new Rawr();
    testing.setValues(1,2,1,4,5,2,3,4);
    System.out.println(testing.toString());
    testing.translate(-3,5);
    System.out.println(testing.toString());
    Rawr testing2 = new Rawr();
    testing2.setValues(1,2,1,4,5,2,3,4);
    System.out.println(testing.sideLength1to2(-2,-1,-2,1,10,7,8,9) + testing2.sideLength1to2(1,2,1,4,5,2,3,4));

    }

    }

    From this I am told that sideLength1to2 is not visible from the type Rawr. How am I to fix this when I am required to leave sideLength1to2 private?

    --- Update ---

    I guess my only issue is knowing how to use the private class whilst leaving it as private.
    Last edited by TheMatrixMage; September 18th, 2014 at 01:29 AM.

  8. #32
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Issues with return

    Please post your code correctly using code or highlight tags which are explained near the top of this link.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  2. Replies: 1
    Last Post: July 29th, 2013, 06:22 AM
  3. Issues with my ordering program - if statement issues?
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 31st, 2012, 10:17 PM
  4. Other possible issues with if statements - book return program
    By Shenaniganizer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2012, 07:29 PM
  5. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM