In: Computer Science
................................................
................................................
This programming lab assignment requires that you create a class and use an equals method to compare two or more objects. Your should use your QC5 as a reference.
…………………………...……..
…………………………...…….
Instructions
LAB5 Instructions
Using QC5 as a model, create a Rectangle class and a CompareUsingequalsMethod class that uses an equals Method to determine if two rectangles are equal if and only if their areas are equal. The Rectangle class should have two instance variables length and width.
Your submission to BB should be your “Rectangle.java” and “CompareUsingequalsMethod.java” files only, not included in a .zip file or a .gpj file.
..............................................................
.............................................................
QC5
Circle.java
/**circle class*/
public class Circle{
//fields
private int radius;
//constructor
public Circle(int radius){
this.radius = radius;
}
//methods
public int getRadius() {
return radius;
}
public boolean equals(Circle inC1, Circle inC2){
if(inC1.getRadius() == inC2.getRadius())
return true;
else
return false;
}
}
…………………………………………….
…………………………………………………………..
CompareUsingequalsMethod.java
/**Demos using the equals method*/
public class CompareUsingequalsMethod{
public static void main(String[] args){
Circle circle1 = new Circle(3);
Circle circle2 = new Circle(3);
Circle circle3 = new Circle(5);
if (circle1.equals(circle1,circle2)) {
System.out.println(" Circle1 with radius " + circle1.getRadius()
+
" is equal to circle2 with radius " + circle2.getRadius());
}
else{
System.out.println(" circle1 with radius " + circle1.getRadius() +
" Radius is not equal to circle2 with radius " +
circle2.getRadius());
}
if(circle3.equals(circle3,circle2)) {
System.out.println(" Circle3 with radius" + circle3.getRadius()
+
" is equal to circle2 with radius" + circle2.getRadius());
}
else{
System.out.println(" circle3 with radius " + circle3.getRadius() +
" Radius is not equal to circle2 with radius " +
circle2.getRadius());
}
}
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// Rectangle.java
public class Rectangle {
//Declaring instance variables
private double length;
private double width;
//Parameterized constructor
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// getters and setters
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double area() {
return length * width;
}
//This method will compare two Rectangle
Objects
public boolean equals(Rectangle r1, Rectangle r2)
{
if (r1.area() == r2.area())
return
true;
else
return
false;
}
//toString method is used to display the contents
of an object inside it
public String toString() {
return "Rectangle : Length=" +
length + ", Width=" + width;
}
}
______________________
// CompareUsingequalsMethod.java
public class CompareUsingequalsMethod {
public static void main(String[] args) {
//Creating an Instance of Rectangle
class obejcts
Rectangle r1 = new Rectangle(3,
4);
Rectangle r2 = new Rectangle(2,
6);
//Comparing two Rectangle class
Objects
if (r1.equals(r1, r2)) {
System.out.println("Rectangle#1 with area " + r1.area()
+ " is equal to Rectangle#2
with area " + r2.area());
} else {
System.out.println("Rectangle#1 with area " + r1.area()
+ " is not equal to
Rectangle#2 with area " + r2.area());
}
}
}
_____________________________
Output:
Rectangle#1 with area 12.0 is equal to Rectangle#2
with area 12.0
_______________Could you plz rate me well.Thank You