Question

(Java) Write the missing equals method needed for the Point class shown on the screen to...

(Java) Write the missing equals method needed for the Point class shown on the screen to work with this program. The missing method would be added to the Point class.

class PointTestP3 {  

public static void main(String[] args) {   

Point p1 = new Point(5, 6);    

Point p2 = new Point(5, 6);       

System.out.println(p1.equals(p2)); // prints "true"  

}

}

Homework Answers

Answer #1

Here, the point class is not provided. But, it is easy to assume that it represents (x, y) point.

So, I have written the Point class as per me.

class Point {

private int x;
private int y;

Point() {
x = y = 0;
}

Point(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public boolean equals(Object o) {
Point p = (Point) o;

if (x == p.x && y == p.y) {
return true;
}
  
return false;
}

}

You just need to change the member variables name as per your code if they are not x and y.

Now comes the equals() method. You can write it in 2 ways. First is, there is an equal method in the Object class so you can override it using @Override notation and this can be done as :

@Override
public boolean equals(Object o) {
Point p = (Point) o;

if (x == p.x && y == p.y) {
return true;
}
  
return false;
}

}

This is what I have mentioned in the above code I wrote in the point class. Here, in the default equals method, there is an Object passed and when we are overriding, we need to make the method signature as it is.

So, inside the method, I converted that object into Point object because I had passed the same object from the main().

To check whether 2 points are equal, we need to check both x and y are same. So, when you call this as p1.equals(p2); this means your p1 variable is passed via 'this' pointer. So, in the method I wrote x == p.x which means here x is referred as this.x and p is what we passed in parameter here it is p2. So, this means it checks p1.x == p2.x and if the result is true for both x and y, it returns true otherwise false.

If you find this little bit difficult or don't want to use @Override, you can do it in easier way as follows. Both are doing the same thing..:

public boolean equals(Point p) {
  
if (x == p.x && y == p.y) {
return true;
}
  
return false;
}

You can directly pass the point object which you will get in the function parameter and can compare it in the same way as above.

So, read both the methods along with the description and use the suitable one. Don't forget to apply the variable names as per your Point class.

I executed the code with both of these equals() method and the result being printed is "true".

Do comment if there is any query. Thank you. :)

Know the answer?
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
Similar Questions
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); }...
What is the output of the following Java program? class Food { Food() { System.out.println(flavor); } String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; } public class Lunch { public static void main(String[] args) { Food lunch = new Pepper(); } } Select one: a. spicy b. the program does not compile c. bland spicy d. no output e. bland
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
What is the output of the following Java program? class Food { String flavor = "bland";...
What is the output of the following Java program? class Food { String flavor = "bland"; } class Pepper extends Food { String flavor = "spicy"; Pepper(String flavor) { this.flavor = flavor; } } public class Lunch { public static void main(String[] args) { Pepper pepper = new Pepper("sweet"); System.out.println(pepper.flavor); } } Select one: a. bland b. the program does not compile c. no output d. spicy e. sweet
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object>...
THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program....
What is the output of the following Java program? interface Food {     public void printFlavor();...
What is the output of the following Java program? interface Food {     public void printFlavor(); } class Pepper implements Food {     public void printFlavor() { System.out.println("spicy"); } } public class Lunch {     public static void main(String[] args) {        Food pepper = new Pepper();         pepper.printFlavor();     } } Select one: a. spicy b. no output c. the program does not compile d. bland e. bland spicy
This is Java programing.Modify this Java code,so that each franchise can assign and print their own...
This is Java programing.Modify this Java code,so that each franchise can assign and print their own burger price.The programing have at least three franchises. (Use abstract) class newBurger { public newBurger() { } public void salmonBurger(){    System.out.println("salmonBurger $5.99");    System.out.println("Kcal: 294"); } public void clamBurger(){    System.out.println("clamBurger $4.99");    System.out.println("Kcal: 200"); } public void oysterBurger(){    System.out.println("oysterBurger $3.50");    System.out.println("Kcal: 125"); } } class franchise1 extends newBurger { String name = "franchise #1"; public franchise1() { } } public...
Rearrange the following lines to produce a program that prints an image of a truck facing...
Rearrange the following lines to produce a program that prints an image of a truck facing left. System.out.println("+~: | |"); System.out.println(" o oo oo "); public class Truck System.out.println( " /-+--------------+"); System.out.println("+---+--------------+"); public static void main(String[] args) { { } }
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program.");...
public class Programming { public static void main(String args[]) { System.out.println("I am learning how to program."); } } Compile this and execute this program then modify it to display I am learning how to program in Java. Change class name to Javaprogramming.java. compile and execute. Modify the Javaprogramming class so it prints two lines of output. Change name to Awesome. Add second output statement that displays That's awesome ! Save as awesome.java then compile and execute
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT