Question

Design a class named MyPoint to represent a point with x and y-coordinates. The class contains: Two data fields x and y that
0 0
Add a comment Improve this question Transcribed image text
Answer #1
class MyPoint {
    // The data fields x and y that represent the coordinates with getter methods.
    private double x, y;

    // A no-arg constructor that creates a point (0,0)
    public MyPoint() {
        this.x = 0;
        this.y = 0;
    }

    // A constructor that constructs a point with specified coordinates.
    public MyPoint(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // getter methods.
    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    // A method named distance that returns the distance from this point to a specified point of the MyPoint type.
    public double distance(MyPoint point) {
        return Math.sqrt(Math.pow(x-point.x, 2) + Math.pow(y-point.y, 2));
    }

    // A method named distance that returns the distance from this point to another point with specified x- and y-coordinates.
    public double distance(double otherX, double otherY) {
        return Math.sqrt(Math.pow(x-otherX, 2) + Math.pow(y-otherY, 2));
    }

    // A static method named distance that returns the distance from two MyPoint objects.
    public static double distance(MyPoint that, MyPoint point) {
        return Math.sqrt(Math.pow(that.x-point.x, 2) + Math.pow(that.y-point.y, 2));
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
}

class PointTest {

    public static void main(String[] args) {
        MyPoint first = new MyPoint(0, 0);
        MyPoint second = new MyPoint(10, 30.5);
        System.out.printf("The distance between " + first + " and " + second + " is %.2f\n", first.distance(second));
    }
}

The distance between (0.0, 0.0) and (10.0, 30.5) is 32.10 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Design a class named MyPoint to represent a point with x and y-coordinates. The class contains:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Inheritance Problem: (C++) (The MyPoint class) Design a class named MyPoint to represent a point with...

    Inheritance Problem: (C++) (The MyPoint class) 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. A no-arg constructor that creates a point (0, 0). A constructor that constructs a point with specified coordinates using passed in arguments. Two get functions for data fields x and y, respectively. A method named distance that returns the distance from this point to another point of the MyPoint...

  • Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains:...

    Create a MyPoint class to model a point in a two-dimensional space. The MyPoint class contains: • Two data fields x and y that represent the coordinates. • A no-arg constructor that creates a point (0,0). • A constructor that constructs a point with specified coordinates. • Two get functions for data fields x and y, respectively. • Two set functions for data fields x and y, respectively. • A function named distance that returns the distance from this point...

  • NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a...

    NEEDS TO BE IN PYTHON: (The Point class) Design a class named Point to represent a point with x- and y-coordinates. The class contains: - Two private data fields x and y that represent the coordinates with get methods. - A constructor that constructs a point with specified coordinates, with default point (0, 0). - A method named distance that returns the distance from this point to another point of the Point type. - A method named isNearBy(p1) that returns...

  • In Python - Design a class named Time. The class contains: -Data fields hour, minute, and...

    In Python - Design a class named Time. The class contains: -Data fields hour, minute, and second that represent a time. -A no-arg constructor that creates a Time object for the current time.(the values of the data fields will respresent the current time.) -A constructor that constructs a Time object with a specified hour, minute, and second, respectively. -A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds. (The values of...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The...

    Use "Eclipse IDE for Java EE Developers" Design a class named MyDate. The class contains: The Date fields year, month, and day that represent a date. month is 0-based, i.e., 0 is for January. A no argument constructor that creates a MyDate object for the current date. A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. A constructor that constructs a MyDate object with the specified year, month, and day....

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1) Two double data members named width and height which specifies the width and height of the rectangle . (2) A no-arg constructor that creates a rectangle with width 1 and height 1. (3) A constructor that creates a rectangle with the specified width and height . (4) A function named getArea() that returns the area of this rectangle . (5) A function named getPerimeter()...

  • Write a class called Point that contains two doubles that represent its x- and y-coordinates. It...

    Write a class called Point that contains two doubles that represent its x- and y-coordinates. It should have get and set methods for both fields. It should have a constructor that takes two double parameters and initializes its coordinates with those values. It should have a default constructor that initializes both coordinates to zero. It should also contain a method called distanceTo that takes as a parameter another Point and returns the distance from the Point that was passed as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT