Write a program to create a class that will store the length and breadth of the rectangle.In addition, you need to check whether two objects of the class contain the same values or not.
ANSWER
public class rectangle {
double length;
double breadth;
public rectangle ( double len , double brd) {
length = len;
breadth = brd;
public boolen equals (object obj) {
rectangle rec = (rectangle) obj;
if (this. length == rec.length && this.breadth == rec.breadth) {
return true;
}
else {
return false;
}
}
public static void main(String [] arg) {
rectangle obj1 = new rectangle (10.5 , 23.6);
rectangle obj2 = new rectangle (10.5 , 33.6);
if (obj1.equals(obj2) ) {
system.out.println("obj1 and obj2 are equal");
}
else {
system.out.println ("obj1 aand obj2 are not equal");
}
}
}
ANSWER
public class rectangle {
double length;
double breadth;
public rectangle ( double len , double brd) {
length = len;
breadth = brd;
public boolen equals (object obj) {
rectangle rec = (rectangle) obj;
if (this. length == rec.length && this.breadth == rec.breadth) {
return true;
}
else {
return false;
}
}
public static void main(String [] arg) {
rectangle obj1 = new rectangle (10.5 , 23.6);
rectangle obj2 = new rectangle (10.5 , 33.6);
if (obj1.equals(obj2) ) {
system.out.println("obj1 and obj2 are equal");
}
else {
system.out.println ("obj1 aand obj2 are not equal");
}
}
}
No comments:
Post a Comment