1. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

So, my initial exercise was to define a struct to represent a point in the Cartesian (xy) coordinate plane and write a client program that asks the user to type in two pairs of coordinates and finds the distance between them using a method. This I did with relative ease.
I am having difficulty updating my point class to use getters, setters and constructors, and changing the client program accordingly to achieve the same purpose.
I can't really figure out how to make a method that can process two different objects without defining additional fields in the point class definition, which rather defeats the purpose of having objects.

Could someone please clarify what information I am missing that is making this so difficult for me? I am reading my textbook over and over and I am not finding anything that clarifies this.

Class Definition:
Java Code:
```public class point {
//fields
public int x;
public int y;
//constructors
public point(){
this(0, 0);
}
public point(int x, int y){
setLocation(x,y);
}
public void setLocation(int x, int y){
this.x = x;
this.y = y;
}
//methods

//returns the distance between this point and another point
public double distanceBetween(double x1, double y1, double x2, double y2) {
double deltaX = (x1-x2);
double deltaY = (y1-y2);
return Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
}
public String toString() {
return "(" + x + ", " + y + ")";

}
}```
Client Program:
Java Code:
```import java.util.*;
import java.awt.*;

public class clientprogram {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

point p1 = new point();
Scanner input1 = new Scanner(input.nextLine());
input1.useDelimiter(",");
p1.x = input1.nextInt();
p1.y = input1.nextInt();

point p2 = new point();
Scanner input2 = new Scanner(input.nextLine());
input2.useDelimiter(",");
p2.x = input2.nextInt();
p2.y = input2.nextInt();

System.out.println("The distance between the two points is " + distanceBetween(p1.x, p1.y, p2.x, p2.y));
}
}```

Step 1: Use standard naming conventions. Classes start with an uppercase letter.
Step 2: Make x and y in your Point class private.
Step 3: Now that x and y are private, you need to add getters so your ClientProgram class can access them.
Step 4: Now that you have the getters, you can modify your ClientProgram class to use them.

3. Member
Join Date
Oct 2013
Posts
63
Rep Power
0

Okay, so this is what I changed with my point class. I'm sorry if I'm being colossally, royally stupid, but I don't really understand why my ClientProgram still can't use the variables.
I don't really know how to modify it to use the getters.
It says that the constructor for point() is undefined...when I really don't think it is?
Java Code:
```public class point {
//private fields
private int x;
private int y;

//constructor method
public point(int x, int y){
this.x = x;
this.y = y;
}
// accessor methods
public int getX(){
return x;
}
public int getY(){
return y;
}
//mutator methods
public void setX(int x){
x = this.x;
}
public void setY(int y){
y = this.y;
}
//returns the distance between this point and another point
public double distanceBetween(int x1, int y1, int x2, int y2) {
double deltaX = (x1-x2);
double deltaY = (y1-y2);
return Math.pow(deltaX, 2) + Math.pow(deltaY, 2);
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";

}
}```

Again, I beg of you, use standard naming conventions.

You haven't posted your updated ClientProgram class, so it's hard to tell you what's wrong with it. Sounds like you're trying to use a constructor that doesn't exist though. Compare the constructor you're calling in ClientProgram to the constructor you wrote in your Point class.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•