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.

Results 1 to 23 of 23

Thread: Where to begin?

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Where to begin?

    Hey.

    I have this assignment but i have no idea of how to fix a part of it and i have been stuck on it for a while.
    Any ideas? I get stuck at p1 and p2. I can print them but they want me to get them thru the toStrings.
    I think that i can handle the rest. I just need to know of how i can make a method that returns the values and
    so that i can use those by the "toStrings".

    Any ideas?

    Create a class Point that when executed using this code:

    Point p1 = new Point(); //These once right here!!!
    Point p2 = new Point(3,4);

    System.out.println(p1.toString()); // ==> (0,0)
    System.out.println(p2.toString()); // ==> (3,4)

    if (p1.isEqualTo(p2)) // False!
    System.out.println("The two points are equal");

    double dist = p1.distanceTo(p2);
    System.out.println("Point Distance: "+dist);

    p2.move(5,-2); // ==> (8,2)
    p1.moveToXY(8,2); // ==> (8,2)

    if (p1.isEqualTo(p2)) // True!
    System.out.println("The two points are equal");

    results in the following console print-out:

    (0,0)
    (3,4)
    Point Distance: 5.0
    The two points are equal

    The class Point should of course be able to handle other points with different (x,y) values. Notice:

    The coordinates (x,y) are always integers.
    The method toString returns a string with coordinates suitable for print-outs.
    Distance between two points is computed in the same way as in Exercise 15, Assignment 1.
    Two points are equal if they have the same coordinates.
    Method move moves the point certain steps in x- and y-direction.
    Method moveToXY provide a new set of coordinates.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    how i can make a method that returns the values and
    so that i can use those by the "toStrings".
    I think you are not understanding the requirements. The toString() method of a class returns a String containing the values in a class.
    Since it is inside the class it can access the class's values directly and does not need to call other methods to get the values.

    This statement describes what toString should do:
    The method toString returns a string with coordinates suitable for print-outs.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    I think you are not understanding the requirements. The toString() method of a class returns a String containing the values in a class.
    Since it is inside the class it can access the class's values directly and does not need to call other methods to get the values.

    This statement describes what toString should do:
    That's very true however now i feel more stuck now haha.
    Seeing how they want this and also seeing that there wouldn't be any need for a method for that..
    Because i only get errors trying to print the first part (from the start to the toStrings).
    What am i doing wrong?

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    217
    My Mood
    Cool
    Thanks
    0
    Thanked 24 Times in 24 Posts

    Default Re: Where to begin?

    1. Do you know how to create a method that takes no arguments and returns a value?
    2. Do you know how to build a string from elements within the class?
    3. Do you know how to return that String when a method is called?

    If you answered yes to these questions then you know how to build a toString method that returns a String
    when the object is the argument of a print statement.

    Regards,
    Jim

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    i only get errors trying to print the first part (from the start to the toStrings).
    What am i doing wrong?
    Please copy the full text of the error message and paste it here.
    Also post the code.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.
    Also post the code.
    km222nb_lab3.Point@15db9742
    km222nb_lab3.Point@6d06d69c

    I am almost 100% sure of that i am in the wrong here.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    You forgot to post the code for the class's toString method.

    km222nb_lab3.Point@15db9742
    That is the String returned by the Object class's default toString method: <classname>@<hexvalue>

    When you are overrding a method in a class you should add the @Override annotation just before the method to tell the compiler to check if you have defined the method correctly.
       @Override
       public void theMethodToOverride() {
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    You forgot to post the code for the class's toString method.


    That is the String returned by the Object class's default toString method: <classname>@<hexvalue>

    When you are overrding a method in a class you should add the @Override annotation just before the method to tell the compiler to check if you have defined the method correctly.
       @Override
       public void theMethodToOverride() {

    I see. In either case. I can now print from the toString method that i have made. However i cannot update the values that i have, and i think that they might overlap aswell.
    The reason for why i use "p" and "P" is because i needed those for the Point method... i cannot start the code without it because it says that the Point p1 and Point p2 needs the
    constructor....

    So the print is just (0,0) two times over.

    public class Point {
    	public static int P = 0;
    	public static int p = 0;
     
    	public Point(int i, int j) {
    		i = P;
    		j = p;
    	}
     
    	public static void main(String[] args) {
     
    		Point p1 = new Point(0, 0); // These once right here!!!
    		Point p2 = new Point(3, 4);
     
    		System.out.println(p1.toString()); // ==> (0,0)
    		System.out.println(p2.toString()); // ==> (3,4)
    	}
     
    	public String toString() {
    		String value = Integer.toString(p);
    		String value2 = Integer.toString(P);
    		String car = "(" + value2 + "," + value + ")";
    		return car;
    	}
    }

  9. #9
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    217
    My Mood
    Cool
    Thanks
    0
    Thanked 24 Times in 24 Posts

    Default Re: Where to begin?

    Don't use static for instance fields.
    Use something easier to discern (p and P don't cut it). How about x and y
    And your assignments in the constructor are backwards.

    Regards,
    Jim

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    The variables in the Point class should not be static. Having them static means that all the instances of the Point class will share that one variable instead of each instance of the class having its own variable with a unique value.

    The assignment statements in the Point class's constructor are reversed. They should assign the class variables the values that are passed to the constructor.
      classVar = passedValue;
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by jim829 View Post
    Don't use static for instance fields.
    Use something easier to discern (p and P don't cut it). How about x and y
    And your assignments in the constructor are backwards.

    Regards,
    Jim
    Wow.. i had no idea that the assignments didn't catch on because of the way that they were written.
    I mean i know that "++example" and "example++" both have different meanings but i didn't that it was this picky haha.
    Thanks

    Quote Originally Posted by Norm View Post
    The variables in the Point class should not be static. Having them static means that all the instances of the Point class will share that one variable instead of each instance of the class having its own variable with a unique value.

    The assignment statements in the Point class's constructor are reversed. They should assign the class variables the values that are passed to the constructor.
      classVar = passedValue;
    Yeah thanks

    However i am still getting a "error exist" but i can execute it and get the right results.. without any error message.

    public class Point {
    	public int x = 0;
    	public int y = 0;
     
    	public Point(int i, int j) {
     
    		x = i;
    		y = j;
    	}
     
    	public static void main(String[] args) {
     
    		Point p1 = new Point(0, 0); // These once right here!!!
    		Point p2 = new Point(3, 4);
     
    		System.out.println(p1.toString()); // ==> (0,0)
    		System.out.println(p2.toString()); // ==> (3,4)
    	}
     
    	@Override
    	public String toString() {
    		String value = Integer.toString(y);
    		String value2 = Integer.toString(x);
    		String car = "(" + value2 + "," + value + ")";
    		return car;
    	}
    }

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    i am still getting a "error exist"
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.
    It went away once i moved the points to their own publics.

    Atm i am trying to figure out how i should go about to see if p1 == p2.
    Since it only saves the value stored at x and y only.

    What i need to have in my code is the boolean part but not sure of how i should go about comparing those two. Tips?

    public int x = 0;
    	public int y = 0;
     
    	public Point(int i, int j) {
     
    		x = i;
    		y = j;
    	}
     
    	public static Point p1 = new Point(0, 0);// These once right here!!!
    	public static Point p2 = new Point(3, 4);
     
    	public static void main(String[] args) {
     
    		System.out.println(p1.toString()); // ==> (0,0)
    		System.out.println(p2.toString()); // ==> (3,4)
     
     
    //This part right here is in the assignment and it needs to be here. 
    		if (p1.isEqualTo(p2)) // False!
    			System.out.println("The two points are equal");
    	}
     
    	private boolean isEqualTo(Point p2) {
    //This is where i am right now... 
    		String a = "(0,0)";
    		if (a != p1.toString()) {
    			return false;
    		} else
    			return true;
    	}
     
    	@Override
    	public String toString() {
    		String value = Integer.toString(y);
    		String value2 = Integer.toString(x);
    		String car = "(" + value2 + "," + value + ")";
    		return car;
    	}
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    What does having two points be equal mean? Describe what needs to be true for two points to be equal.
    Don't write any code yet. Describe what needs to be tested in English.
    Note: It has nothing to do with a toString method.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    What does having two points be equal mean? Describe what needs to be true for two points to be equal.
    Don't write any code yet. Describe what needs to be tested in English.
    Note: It has nothing to do with a toString method.
    Alright! So we have two sets of points. Points for "p1" and points for "p2".

    This part of the code :
    if (p1.isEqualTo(p2)) // False!
    			System.out.println("The two points are equal");
    Which exist in the main, has to somehow be able to, via the method "isEqualTo" figure out if the
    points are "p1" is equal to the points at "p2".

    That's what i think that they are asking for.
    PS: Full assignment is at the top of the page, but i think that you already know that.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    Please describe in English (no code) what needs to be done to test if two Point objects are equal.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    Please describe in English (no code) what needs to be done to test if two Point objects are equal.
    Ehm.. we have to see if two things are equal to each other?
    Via some sort of method that tests whether it is true or not?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    Given two points, what needs to be tested to see if they are equal?
    Describe in English, not with code.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    Given two points, what needs to be tested to see if they are equal?
    The numbers for the x and y for both given points.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    Ok, that was what I was looking for.
    Now given that the method is inside of one instance of a Point and has local access to the x,y values for that instance, how would it compare those local values to the values in the other Point that was passed to the method?
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    Ok, that was what I was looking for.
    Now given that the method is inside of one instance of a Point and has local access to the x,y values for that instance, how would it compare those local values to the values in the other Point that was passed to the method?
    private boolean isEqualTo(Point p22) {
    		if (p1.x == p2.x && p1.y == p2.y) {
    			return true;
    		} else
    			return false;
    	}

    It works, thannkss!
    And thanks for not giving me the answer straight away!

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,899
    Thanks
    52
    Thanked 2,335 Times in 2,291 Posts

    Default Re: Where to begin?

    The code doesn't do what it is supposed to do. It should NOT be looking at the p1 and p2 variables.
    It should use the local value of x and y and compare them against the values in p22.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    107
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Where to begin?

    Quote Originally Posted by Norm View Post
    The code doesn't do what it is supposed to do. It should NOT be looking at the p1 and p2 variables.
    It should use the local value of x and y and compare them against the values in p22.
    I see.. didn't see your post in the end. Going to try what you said here.
    Thanks.

Similar Threads

  1. Where do I begin?
    By Coherent in forum The Cafe
    Replies: 9
    Last Post: September 22nd, 2014, 07:36 AM
  2. Where do I begin?
    By Coherent in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2014, 05:45 AM
  3. Where do I begin?
    By Coherent in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: June 30th, 2014, 05:05 AM
  4. Begin of the Java
    By mehboob110233 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2014, 04:38 AM