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 1 of 2 12 LastLast
Results 1 to 25 of 32

Thread: Issues with return

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

    Default Issues with return

    Hello, I'm new to java and programming in general and am now in a CS110 course in college.

    Currently I am unsure how to fix my issues with return in this small bit of code:

    private void sideLength1to2(){
    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;

    It cannot resolve any of the s variables when trying to calculate the perimeter.


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

    Default Re: Issues with return

    The s variables are local to sideLength method and the method perimeter doesn't know what s1,s2... are.

    Post your full code, so i can give you further help. I would suggest making s variables attributes to your class.

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

    Default Re: Issues with return

    This is what I have thus far.

    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;


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

    public void translate(){
    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 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;

    I'm supposed to be creating a polygon through coordinates that can then be translated. Also that has a private method that determines the side lengths and a method to calculate perimeter.

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

    Default Re: Issues with return

    your perimeter method has no way of knowing what your S variables are. This is because you declare your S variables in your sideLength method, making them local to that function. One way you can fix it is to make your S variables attributes of the class like you did with your x and y variables.
    Another way would be to add parameters to your perimeter method like so

    private void perimeter(double s1, double s2, double s3, double s4){
    double p = s1 + s2 + s3 + s4;
    }

    Then whenever you called the perimeter method you would have to pass those variables.

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

    Default Re: Issues with return

    In this case would it still pull the values obtained from sideLength1to2?

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

    Default Re: Issues with return

    What is your definition of pull?

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

    Default Re: Issues with return

    If I used a Tester class to plug in values to retrieve the side lengths for that particular polygon, could they then be used for the perimeter?

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

    Default Re: Issues with return

    If you do one of the prior revisions I listed above than there shouldn't be a problem getting anything done. It depends on how you want to call your methods and in which way you plan on using them. That will determine the best way to declare your variables.

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

    TheMatrixMage (September 17th, 2014)

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

    Default Re: Issues with return

    Thank you so much for your help so far. Any hints now for my tester class (I established the s variables at the beginning of my main class). I'm having issues of thinking how to instantiate the polygon (1,2)(2,4)(1,6)(3,5) as an object.

  11. #10
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    You should use some parameters in your constructor for the Rawr class. That way when you create the object in your main you can give it initial values that you like (1,2)(2,4)(1,6)(3,5). If I'm understanding right.

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

    Default Re: Issues with return

    Alright, well sorry to say I'm not fully understanding how I would manage this. I'll hopefully whip something up, thanks again.

  13. #12
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    When you get stuck. Post your full code here. It makes it much easier to point you in the right direction.

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

    Default Re: Issues with return

    You mean in the tester class I might start it off as?


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

    double x1 = 1;
    double x2 = 2;
    double x3 = 1;
    double x4 = 3;
    double y1 = 2;
    double y2 = 4;
    double y3 = 6;
    double y4 = 5;

    }
    }

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

    Default Re: Issues with return

    I was referring to the rawr class where you would declare your S variables at the top like so.

    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 s1;
    private double s2;
    private double s3;
    private double s4;
    private double shiftX;
    private double shiftY;
     
     
    public Rawr(double x1, double x2, double x3, double x4,double y1, double y2, double y3, double y4){
    this.x1 = x1;
    this.x2 = x2;
    this.x3 = x3;
    this.x4 = x4;
    this.y1 = y1;
    this.y2 = y2;
    this.y3 = y3;
    this.y4 = y4;
    }
     
    public void translate(){
    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(){
     s1 = Math.sqrt((x2 - x1)*(x2 - x1) + (y2 - y1) * (y2 - y1));
    s2 = Math.sqrt((x3 - x2)*(x3 - x2) + (y3 - y2) * (y3 - y2));
    s3 = Math.sqrt((x4 - x3)*(x4 - x3) + (y4 - y3) * (y4 - y3));
    s4 = Math.sqrt((x1 - x4)*(x1 - x4) + (y1 - y4) * (y1 - y4));
    return;
    }
     
    private void perimeter(){
    double p = s1 + s2 + s3 + s4;
    }
     
    }

    And notice the constructor parameters. This will allow you to initialize the values you want from your tester class whne you create the object.

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

    Default Re: Issues with return

    Ah yes, my apologies I have already done that. My question now is, as I have created a tester class to run off this one, in the tester class how would I go about instantiating an object (the polygon) at particular points such as those previously mentioned?

  17. #16
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    Look up at my last post ( I edited in a little more)

    If you look at the constructor you can now pass in the values you want from the tester class.

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

    Default Re: Issues with return

    Ah but I'm supposed to default them to 0.

  19. #18
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    You can have both constructors but I must be missing what you are trying to do then.

    So walk me through it here. You want to create an object of type Rawr in your tester class? What values do you want to change in your rawr class? What methods do you plan on calling?

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

    Default Re: Issues with return

    I need to instantiate the polygon at particular points (the points just depend on the case and the previous ones supplied were random)
    Then I need to print the object used in the Tester Class
    I then need to translate the object 3 units left, 5 up.
    Then I print the object to the console again.
    Then I instantiate a second object with the same coordinates as the first one before it translated.
    Then I print the difference in perimeter between the first and second one through calculations in the main class.
    Then I print the expected difference (Obviously 0)

    This portion of it all is to be done in the tester.

    The class I originally supplied the code in is my main class.

    In this Tester I am to implement a main method that performs the above unit testing.

  21. #20
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    start with one thing at a time. So you want to give the coordinates of the object after you create it in main correct? You will need to create a method that will supply the points you want for the polygon. Start there. Then in your main class you can call that method.

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

    Default Re: Issues with return

    So add something like this to the main?

    public Rawr(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;
    }

  23. #22
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    That's a constructor. So it can't be in your main. If you want to give x and y values you can make that into a method.
    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;
    }

    Then in your main you can create the object and call that method like so

    Rawr testing = new Rawr();
    testing.setValues... blah blah and what ever values you decide to give it

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

    Default Re: Issues with return

    So are both of those going into my main class? or is one going into my tester? Also can you clarify on the usage of the test.setValues? the ... blah blah has got me a little

  25. #24
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: Issues with return

    the constructor is for you Rawr class. testing.setValues(1,2,3,4,56) are the values(I just put random numbers) you are wanting to use to set for your x and y values. You will call that in your main class. That will only work once you add the setValues method to your Rawr class.

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

    Default Re: Issues with return

    So my main should look something like

    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(){
    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 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;
    }


    }


    Whilst my tester is


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

    Rawr testing = new Rawr();
    testing.setValues(1,2,1,4,5,2,3,4);


    }

    }

Page 1 of 2 12 LastLast

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, 10: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