Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We want to specify the coordinates (i.e., x and y coordinates) of a point in the

ID: 3641417 • Letter: W

Question

We want to specify the coordinates (i.e., x and y coordinates) of a point in the two dimensional space (i.e., x-y coordinate system).

Point
- x : double
- y : double
+ Point()
+ Point(xPos : double, yPos : double)
+ setX(xPos : double) : void
+ setY(yPos : double) : void
+ setPoint(xPos : double, yPos : double) : void
+ getX() : double
+ getY() : double
+ distance(pt: Point) : double
+ toString(xPos : double, yPos : double) : String
+ toString() : String


The attributes (i.e. data fields) for Point are declared as private for information hiding (i.e., encapsulation), however all the methods are declared as public. This class has the two constructors (i.e., Point() and Point(double, double)), the three mutators (i.e., setX(double), setY(double), and setPoint(double, double)), and the two accessors (i.e., getX() and getY()). In addition, the distance method computes the Euclidean distance between this Point and another point. The two toString() methods return the x- and y-coordinates in a string (e.g., Point pt = new Point(3, 5); The string representation should look like "(3, 5)".
(NOTE 01) The first toString(x, y) and a simple test driver, main(), will be defined as class methods.
You need to test the functionality of the Point class that you have created. Write a driver TestPoint to test all of the methods, which will include the following statements.





public class TestPoint
{
Public static void main(String[] args)
{
// (TASK 01) Create an object aPoint1 using the default constructor.

// (TASK 02) Create an object aPoint2 at location (0.0, 3.0).

// (TASK 03) Creat an object aPoint3 at location (3.0, 4.0).

// (TASK 04) Print out the coordinates of aPoint1, aPoint2, and aPoint3
// using getX, getY, and toString(double, double).

// (TASK 05) Print out the coordinates of aPoint1, aPoint2, and aPoint3
// using toString().

// (TASK 06) Print out the coordinates of aPoint1, aPoint2, and aPoint3
// without explicitly using toString() methods.

// (TASK 07) Print out the distance between aPoint1 and aPoint2
// and the distance between aPoint1 and aPoint3 using distance(double, double).

// (TASK 08) Set aPoint1 to be located at the same coordinates of aPoint2
// and aPoint2 to be (0.0, 30.0) with setPoint(double, double)
// and aPoint3 to be be (30.0, 40.0) using setPoint(double, double).
// Then, print out the distance between aPoint1 and aPoint3.
}
}

Explanation / Answer


public class Point {

    private double x;
    private double y;

Point() {
    }

    Point(double xPos, double yPos) {
        this.x = xPos;
        this.y = yPos;
    }

    void setX(double x) {
        this.x = x;
    }

    void setY(double y) {
        this.y = y;
    }

    void setPoint(double xPos, double yPos) {
        setX(xPos);
        setY(yPos);
    }

    double getX() {
        return x;
    }

    double getY() {
        return y;
    }

    double distance(Point pt) {
        return Math.sqrt(Math.pow(x - pt.x, 2) + Math.pow(y - pt.y, 2));
    }

    public String toString() {
        return "x: " + this.x + " y: " + this.y;
    }

    String toString(double xPos, double yPos) {
        return "x: " + xPos + " y: " + yPos;
    }

    public static void main(String[] args) {
// (TASK 01) Create an object aPoint1 using the default constructor.
        Point aPoint1 = new Point();

// (TASK 02) Create an object aPoint2 at location (0.0, 3.0).
        Point aPoint2 = new Point(0.0, 3.0);

// (TASK 03) Creat an object aPoint3 at location (3.0, 4.0).
        Point aPoint3 = new Point(3.0, 4.0);

// (TASK 04) Print out the coordinates of aPoint1, aPoint2, and aPoint3
        System.out.println(aPoint1.toString());
        System.out.println(aPoint2.toString());
        System.out.println(aPoint3.toString());

// using getX, getY, and toString(double, double).     
// (TASK 05) Print out the coordinates of aPoint1, aPoint2, and aPoint3
        System.out.println(aPoint1.toString(aPoint1.getX(), aPoint1.getY()));
        System.out.println(aPoint2.toString(aPoint2.getX(), aPoint2.getY()));
        System.out.println(aPoint3.toString(aPoint3.getX(), aPoint3.getY()));

// using toString().
// (TASK 06) Print out the coordinates of aPoint1, aPoint2, and aPoint3
// without explicitly using toString() methods.
        System.out.println("X : " + aPoint1.getX() + " Y : " + aPoint1.getY());
        System.out.println("X : " + aPoint2.getX() + " Y : " + aPoint2.getY());
        System.out.println("X : " + aPoint3.getX() + " Y : " + aPoint3.getY());
// (TASK 07) Print out the distance between aPoint1 and aPoint2
// and the distance between aPoint1 and aPoint3 using distance(double, double).
        System.out.println("Distance between Point 1 & 2 : " + aPoint1.distance(aPoint2));

// (TASK 08) Set aPoint1 to be located at the same coordinates of aPoint2
// and aPoint2 to be (0.0, 30.0) with setPoint(double, double)
// and aPoint3 to be be (30.0, 40.0) using setPoint(double, double).
// Then, print out the distance between aPoint1 and aPoint3.
        aPoint1.setX(aPoint2.getX());
        aPoint1.setY(aPoint2.getY());
        aPoint2.setPoint(0.0, 30.0);
        aPoint3.setPoint(30.0, 40.0);
        System.out.println("Distance between Point 1 & 3 : " + aPoint1.distance(aPoint3));

    }
}
public class Point {

    private double x;
    private double y;

Point() {
    }

    Point(double xPos, double yPos) {
        this.x = xPos;
        this.y = yPos;
    }

    void setX(double x) {
        this.x = x;
    }

    void setY(double y) {
        this.y = y;
    }

    void setPoint(double xPos, double yPos) {
        setX(xPos);
        setY(yPos);
    }

    double getX() {
        return x;
    }

    double getY() {
        return y;
    }

    double distance(Point pt) {
        return Math.sqrt(Math.pow(x - pt.x, 2) + Math.pow(y - pt.y, 2));
    }

    public String toString() {
        return "x: " + this.x + " y: " + this.y;
    }

    String toString(double xPos, double yPos) {
        return "x: " + xPos + " y: " + yPos;
    }

    public static void main(String[] args) {
// (TASK 01) Create an object aPoint1 using the default constructor.
        Point aPoint1 = new Point();

// (TASK 02) Create an object aPoint2 at location (0.0, 3.0).
        Point aPoint2 = new Point(0.0, 3.0);

// (TASK 03) Creat an object aPoint3 at location (3.0, 4.0).
        Point aPoint3 = new Point(3.0, 4.0);

// (TASK 04) Print out the coordinates of aPoint1, aPoint2, and aPoint3
        System.out.println(aPoint1.toString());
        System.out.println(aPoint2.toString());
        System.out.println(aPoint3.toString());

// using getX, getY, and toString(double, double).     
// (TASK 05) Print out the coordinates of aPoint1, aPoint2, and aPoint3
        System.out.println(aPoint1.toString(aPoint1.getX(), aPoint1.getY()));
        System.out.println(aPoint2.toString(aPoint2.getX(), aPoint2.getY()));
        System.out.println(aPoint3.toString(aPoint3.getX(), aPoint3.getY()));

// using toString().
// (TASK 06) Print out the coordinates of aPoint1, aPoint2, and aPoint3
// without explicitly using toString() methods.
        System.out.println("X : " + aPoint1.getX() + " Y : " + aPoint1.getY());
        System.out.println("X : " + aPoint2.getX() + " Y : " + aPoint2.getY());
        System.out.println("X : " + aPoint3.getX() + " Y : " + aPoint3.getY());
// (TASK 07) Print out the distance between aPoint1 and aPoint2
// and the distance between aPoint1 and aPoint3 using distance(double, double).
        System.out.println("Distance between Point 1 & 2 : " + aPoint1.distance(aPoint2));

// (TASK 08) Set aPoint1 to be located at the same coordinates of aPoint2
// and aPoint2 to be (0.0, 30.0) with setPoint(double, double)
// and aPoint3 to be be (30.0, 40.0) using setPoint(double, double).
// Then, print out the distance between aPoint1 and aPoint3.
        aPoint1.setX(aPoint2.getX());
        aPoint1.setY(aPoint2.getY());
        aPoint2.setPoint(0.0, 30.0);
        aPoint3.setPoint(30.0, 40.0);
        System.out.println("Distance between Point 1 & 3 : " + aPoint1.distance(aPoint3));

    }
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote