Q:

(The MyPoint class) Design a class named MyPoint to represent a point with x- and y-coordinates. The class contains:

0

(The MyPoint class) Design a class named MyPoint to represent a point with x- and y-coordinates. The class contains:

The data fields x and y that represent the coordinates with getter methods.
A no-arg constructor that creates a point (0, 0).
A constructor that constructs a point with specified coordinates.
A method named distance that returns the distance from this point to a specified 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 and then implement the class. Write a test program that creates the two points (0, 0) and (10, 30.5) and displays the distance between them.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (The MyPoint class) Design a class named MyPoint to represent a point with     *
* x- and y-coordinates. The class contains:                                      *
*                                                                                *
* ■ The data fields x and y that represent the coordinates with getter           *
*   methods.                                                                     *
* ■ A no-arg constructor that creates a point (0, 0).                            *
* ■ A constructor that constructs a point with specified coordinates.            *
* ■ A method named distance that returns the distance from this point to a       *
*   specified 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 and then implement the class. Write a test  *
* program that creates the two points (0, 0) and (10, 30.5) and displays the     *
* distance between them.                                                         *
*********************************************************************************/
public class Exercise_10_04 {
	/** Main method */
	public static void main(String[] args) {
		// Create two points
		MyPoint point1 = new MyPoint();
		MyPoint point2 = new MyPoint(10, 30.5);

		// Display the distance between point1 and point2
		System.out.println("The distance between (" 
			+ point1.getX() + ", " + point1.getY() + ") and ("
			+ point2.getX() + ", " + point2.getY() + ") is: " + 
			point1.distance(point2));
	}
}

MyPoint.java 

/**********************************
*            MyPoint              *
*---------------------------------*
* -x: double                      *
* -y: double                      *
* +MyPoint()                      *
* +MyPoint(x: double, y: double)  *
* +getX(): double                 *
* +getY(): double                 *
* +distance(myPoint: MyPoint)     *
* +distance(x: double, y: double) *
**********************************/

// Implement MyPoint class
public class MyPoint {
	// Data fields
	private double x;
	private double y;

	/** Constructor that creates a point (0, 0) */
	MyPoint() {
		this(0, 0);
	}

	/** Constructs a point with specified coordinates */
	MyPoint(double x, double y) {
		this.x = x;
		this.y = y;
	}

	/** Return x */
	public double getX() {
		return x;
	}

	/** Return y */
	public double getY() {
		return y;
	}

	/** Returns the distance from this point to 
   *   a specified point of the MyPoint type */
   public double distance(MyPoint myPoint) {
   	return Math.sqrt(Math.pow(myPoint.getX() - x, 2) + 
   			 Math.pow(myPoint.getY() - y, 2));
   }

   /** Returns the distance from this point to another
 	*   point with specified x- and y-coordinates. */
 	public double distance(double x, double y) {
 		return distance(new MyPoint(x, y));
 	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now