Question 2 : Write the below program in Java. Design a class named Point that meets the following requirements: Two data fields w and z
Question 2 : Write the below program in Java.
Design a class named Point that meets the following requirements:
Two data fields w and z 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 w and z values.
Override the equals method. Point p1 is said to be greater than point p2 if p1.w = = p2.w and p1.z = = p2.z.
Implement the Comparable interface and the compareTo method. Point p1 is said to be greater than point p2 if p1.w > p2.w or if p1.w = = p2.w and p1.z > p2.z.
Override the toString() method to return a string as [w value, z value].
Implement the Cloneable interface and clone method.
Complete and run your program using the following code:
public class Ques_02 {
public static void main(String[] args) {
Point1 p1 = new Point1(3, 4);
Point1 p2 = new Point1(3.4, 1.4);
System.out.println(p1.equals(p2));
System.out.println(p1.equals(p1));
System.out.println(p1.compareTo(p2));
System.out.println(p2.compareTo(p1));
Point1 p3 = (Point1)(p1.clone());
System.out.println(p3.equals(p1));
System.out.println(p3); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started