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

1. ## 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. ## 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.

3. ## Re: Where to begin?

Originally Posted by Norm
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. ## 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. ## 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.

6. ## Re: Where to begin?

Originally Posted by Norm
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. ## 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() {```

8. ## Re: Where to begin?

Originally Posted by Norm
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. ## 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. ## 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;`

11. ## Re: Where to begin?

Originally Posted by jim829
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

Originally Posted by Norm
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. ## Re: Where to begin?

i am still getting a "error exist"
Please copy the full text of the error message and paste it here.

13. ## Re: Where to begin?

Originally Posted by Norm
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. ## 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.

15. ## Re: Where to begin?

Originally Posted by Norm
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. ## Re: Where to begin?

Please describe in English (no code) what needs to be done to test if two Point objects are equal.

17. ## Re: Where to begin?

Originally Posted by Norm
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. ## Re: Where to begin?

Given two points, what needs to be tested to see if they are equal?
Describe in English, not with code.

19. ## Re: Where to begin?

Originally Posted by Norm
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. ## 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?

21. ## Re: Where to begin?

Originally Posted by Norm
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. ## 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.

23. ## Re: Where to begin?

Originally Posted by Norm
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.