Hi,

I have a bit of a problem of understanding of what I need to do to write this program. Could someone please provide me basic steps of what I should do? Or an example, I"m good at relating and following examples. I believe this is my last assignment for this semester so I wanted to do as well as I could. Any help will be appreciated. Thank you

Design a class named MyPoint to represent a point with x-and y- coordinates. The class contains: Two data fields x and y that represent the coordinates with get methods. A no- arg constructor that creates a point ( 0, 0). A constructor that constructs a point with specified coordinates. Two get methods for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint type. A method named distance that returns the distance from this point to another point with specified - x and y- coordinates. Draw the UML diagram for the class. Implement the class. Write a test program that creates two points ( 0, 0) and ( 10, 30.5) and displays the distance between them. .

Recommended Answers

All 11 Replies

What are you having trouble with exactly? I'm not going to spoonfeed you the entire Class but all you really need to get started is

public class MyPoint    {
    private int x, y;
}

Take one step at a time.
Now add a no arg constructor.

public class MyPoint {
private int x,y;
public MyPoint(){
x=0;
y=0;
}
public MyPoint(int x, int y){
this.x=x;
this.y=y;
}
/* now Two get methods for data fields x and y, respectively. */
.....
}

@tong1 If you are going to provide code for the OP, you should add comments explaining what the code does and how or why you wrote it the way that you did to help the OP to understand how to write programs, since he is not getting any good experience writing code by copying and pasting your code.

NormR1, thank you very much for your advise.
The code with explanation is written as follows.

public class MyPoint {
private int x,y;
/* The no-argument constructor creates the instance at the coordinate origin.*/
public MyPoint(){
x=0; // assign zero to the attribute x .
y=0; // assign zero to the attribute y.
}
/*The following constructor constructs a point with specified coordinates. */
public MyPoint(int x, int y){
this.x=x;
/* x is a local variable (an argument of the constructor method) while this.x represents the attribute x of the given instance. “this” is a handle representing the given instance while ‘.’ is the handle operator */
this.y=y;
/* y is another local variable (the second argument of the constructor method) while this.x is the attribute y of the given instance.  “this” is a handle representing the given instance while ‘.’ is the handle operator */
}
/* now Two get methods for data fields x and y, respectively. */
.....
}

Thanks.

Thank you for getting me started, appreciate it. Now, I know I'm going to have a "distance" method, what's the other method, the "main"?

No. The other methods are not the main which could be in another class called "driver", to test the class MyPoint.
The following methods you have to implement include:

/*The following nethods are shown in abstract format that you are going to implement. The abstract method has no method body. */
public int getX();
public int getY();
//Perhaps your teacher will ask you create another two methes:
public void setX(int x);
public void setY(int y);
public int distance(MyPoint otherP) //to calculate the distance from this point to another point of the MyPoint type.
public int distance(MyPoint p1, MyPoint p2);// to calculate the distance between two points

Then You should draw the class diagram of UML for the class MyPoint
Finally you may create a driver class MyPointTest where you may create two points ( 0, 0) and ( 10, 30.5) and displays the distance between them. .

Hello there,
Congratulations for ur forum.
I need your help
I have understand your explanation and the methods.
What will be included in the methods though?

I have written my UML diagram as follows.

-x : double
-y : double

+MyPoint()
+MyPoint(x:double,y:double)
+get X() :double
+getY() : double
+distance: (MyPoint1:MyPoint2):double
+distance(p1:MyPoint:p2:MyPoint):double

My code is the following and I am missing the last two methods

import java.lang.Math
public class MyPoint {
private double x,y, distance;
public MyPoint(){
x=0;
y=0; // assign zero to the attribute y.
}
public MyPoint(double x, double y){
this.x=x;
this.y=y;
}

public getX(double x){
return x;
}
public getY(double y){
return y;
}
public double distance (MyPoint1 , MyPoint2){
// distance =
}

public static void main(String []args)
{
MyPoint MyPoint1 = new MyPoint();
MyPoint MyPoint2 = new MyPoint(10, 30.5);

}

}//end of class


Can someone help me please
Thanks in advance

#
public int distance(MyPoint otherP) //to calculate the distance from this point to another point of the MyPoint type.
#
public int distance(MyPoint p1, MyPoint p2);// to calculate the distance between two points

Please start a new thread with your new topic instead of adding onto an old thread.

You need to look at a math formula for finding the distance between two points in 2 dimensional space. I don't have it handy right now but it includes using the differences between the x coords and the differences between the y coords and squaring and taking the square root.
An old Greek guy worked it out years ago.

some tell me who to find the distance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.