Write me another code slimier to this one and please add the comment like this o
ID: 3592689 • Letter: W
Question
Write me another code slimier to this one and please add the comment like this one class
Square { //a variable as the part of our class private int side; //here is the constructor public Square(int n1) { this.side = n1; } // write a function to override the JAVA's equals method //Here our function is overriding the JAVA's equals method @Override public boolean equals(Object sqr) { //check if the object is from same class if (!(sqr instanceof Square)) { return false; } // ensure both the objects are not the same (just an optimization) if (sqr == this) { return true; } // do type cast to compare the elemets easily Square another = (Square) sqr; // now lets compare the values and return the result return Integer.compare(side, another.side) == 0; //compare returns 0 when matched } } // Driver class to test the Complex class public class main { public static void main(String[] args) { Square first = new Square(20); Square second = new Square(15); //test the equals method if (first.equals(second)) System.out.println("Both the squares are SAME !!"); else System.out.println("Squares are Not SAME !!"); //another test case Square third = new Square(20); if (first.equals(third)) System.out.println("Both the squares are SAME !!"); else System.out.println("Squares are Not SAME !!"); } }
Explanation / Answer
Note: As u told u want similar code.So I took Rectangle classes and comparing ..Plz check this..If u want me to modify anything plz.tell me.So that I will change..Thank YOu
______________
Rectangle.java
public class Rectangle {
//Declaring instance variables
private int width;
private int height;
//Parameterized constructor
public Rectangle(int width, int height) {
super();
this.width = width;
this.height = height;
}
//getters and setters
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + "]";
}
// write a function to override the JAVA's equals method
//Here our function is overriding the JAVA's equals method @Override
public boolean equals(Object rect) {
//check if the object is from same class
if (!(rect instanceof Rectangle)) {
return false;
}
// ensure both the objects are not the same (just an optimization)
else {
// do type cast to compare the elements easily
Rectangle another = (Rectangle) rect;
// now lets compare the values and return the result
return (Integer.compare(width, another.width) == 0 && Integer.compare(height, another.height) == 0);
//compare returns 0 when matched
}
}
}
_________________
TestRectangle.java
public class TestRectangle {
public static void main(String[] args) {
//Creating two Rectangles Objects
Rectangle first = new Rectangle(3, 4);
Rectangle second = new Rectangle(3, 4);
// test the equals method
if (first.equals(second))
System.out.println("Both the Rectangles are SAME !!");
else
System.out.println("Rectangles are Not SAME !!");
//Creating Rectangle Object
Rectangle third = new Rectangle(3, 9);
if (first.equals(third))
System.out.println("Both the Rectangles are SAME !!");
else
System.out.println("Rectangles are Not SAME !!");
}
}
___________________
Output:
Both the Rectangles are SAME !!
Rectangles are Not SAME !!
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.