Question

In JAVA Design a class named Point that meets the following requirements: Two data fields x...

In JAVA

Design a class named Point that meets the following requirements:

  • Two data fields x and y for representing a point with getter methods.
  • A no-arg constructor that constructs a point for (0, 0).
  • A constructor that constructs a point with the specified x and y values.
  • Override the equals method. Point p1 is said to be greater than point p2 if p1.x == p2.x and p1.y == p2.y.
  • Implement the Comparable<Point> interface and the compareTo method. Point p1 is said to be greater than point p2 if p1.x > p2.x or if p1.x == p2.x and p1.y > p2.y.
  • Override the toString() method to return a string as [x value, y value].

Write a test program that creates a Point object p1 at point (3,4) and another Point object p2 at point (3.4, 1.4). Call the p1’s equals method with p2 as its argument and print the result. Call the p2’s equals method with p1 as its argument and print the result. Call the p1’s compareTo method with p2 as its argument and print the result. Call the p2’s compareTo method with p1 as its argument and print the result.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. /************************** Point Class **********************************/

class Point implements Comparable<Point>
{
double x,y;
Point()
{
x=y=0;
}
Point(double x, double y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o)
{
Point p = (Point)o;
return this.x == p.x && this.y == p.y;
}
@Override
public int compareTo(Point p)
{
int result = Double.compare(this.x,p.x);
return result == 0 ? Double.compare(this.y,p.y) : result;
}
@Override
public String toString()
{
return "[x "+ this.x + ",y " + this.y + "]";
}
}

2. /********************************Test.java**********************************************/
public class Test
{
   public static void main(String[] args) {
      
       Point p1 = new Point(3,4);
       Point p2 = new Point(3.4,1.4);
      
       System.out.println(p1.toString());
       System.out.println(p2.toString());
      
       System.out.println(p1.equals(p2));
       System.out.println(p2.equals(p1));
      
       System.out.println(p1.compareTo(p2));
       System.out.println(p2.compareTo(p1));
   }
}

3. /***************************Output*************************************/

Add a comment
Know the answer?
Add Answer to:
In JAVA Design a class named Point that meets the following requirements: Two data fields x...
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
  • Java Help 2. Task: Create a client for the Point class. Be very thorough with your...

    Java Help 2. Task: Create a client for the Point class. Be very thorough with your testing (including invalid input) and have output similar to the sample output below: ---After declaration, constructors invoked--- Using toString(): First point is (0, 0) Second point is (7, 13) Third point is (7, 15) Second point (7, 13) lines up vertically with third point (7, 15) Second point (7, 13) doesn't line up horizontally with third point (7, 15) Enter the x-coordinate for first...

  • Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of...

    Using Python 3: Create a point p1 of coordinates (0; 0) and un point p2 of coordinates (1; 2). Print out the coordinates of the two points on the same line, by calling toString on the two points. Print the result of applying the method equals on point p1, using p2 as argument. Set the x coordinate of p2 equal to the x coordinate of p1, using the methods setX and getX. Set the y coordinate of p2 equal to...

  • 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...

  • 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...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

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

    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. Two get functions for data fields x and y, respectively. A function named distance that returns the distance from this point to another point of the MyPoint type Write a test program that creates two...

  • Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An...

    Hello! This is C++. Q3. Write a program Define a Super class named Point containing: An instance variable named x of type int. An instance variable named y of type int. Declare a method named toString() Returns a string representation of the point. Constructor that accepts values of all data members as arguments. Define a Sub class named Circle. A Circle object stores a radius (double) and inherit the (x, y) coordinates of its center from its super class Point....

  • USING JAVA, Class Design: Person The Person class is intended to be an abstract and simplified representation of a pers...

    USING JAVA, Class Design: Person The Person class is intended to be an abstract and simplified representation of a person. Each person will have an array of "friends" - which are other "Person" instances. Data to Store (2 Points) Name private instance variable with only private setter (2 Points) Friends private instance array with neither getter nor setter Actions Constructor o 1 Point) Take in as argument the name of the person (enforce invariants) o (1 Point) Initialize the array...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • 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...

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