• 01-13-2014, 02:16 PM
Propinquity
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:
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:
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();                 System.out.println("Enter your first point.");                 Scanner input1 = new Scanner(input.nextLine());                 input1.useDelimiter(",");                 p1.x = input1.nextInt();                 p1.y = input1.nextInt();                                 point p2 = new point();                 System.out.println("Enter your second 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));         } }```
• 01-13-2014, 03:46 PM
KevinWorkman
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.
• 01-14-2014, 10:11 AM
Propinquity
```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 + ")";                         } }```