equals method :::::HELP:::::
Hi,
I have two classes :
Rectangle1:
Code:
import java.util.*;
public class Rectangle1
{
/**
*Private variables:
* #RecLength is a variable to store the length of the rectangle that the user will enter.
* #RecWidth is a variable to store the width of the rectangle that the user will enter.
* #RecArea is a variable to store the area of the rectangle after the calculation.
* #RecParimeter is a variable to store the perimeter of the rectangle after the calculation.*/
private int RecLength;
private int RecWidth;
private int RecArea;
private int RecPerimeter;
Scanner keyboard = new Scanner(System.in);
public void rectangleLength()
{
/**
*Method's function:
* #To get a number greater than 0 from the user.
* #The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
* #Void method to set the length of the rectangle. It will not return any value.*/
while (RecLength < 1)
{
System.out.println("Please, enter the length of the first rectangle: ");
RecLength = keyboard.nextInt();
}
}
public void rectangleWidth()
{
/**
*Method's function:
* #To get a number greater than 0 from the user.
* #The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
* #Void method to set the width of the rectangle. It will not return any value.*/
while (RecWidth < 1)
{
System.out.println("Please, enter the width of the first rectangle: ");
RecWidth = keyboard.nextInt();
}
}
public int rectangleArea()
{
/**
*Method's function:
* #To calculate the area of the rectangle by using the inputs "The length and the width".
* #Accessor method to get the area of the rectangle. It will return the value of the area.*/
RecArea = RecLength * RecWidth;
return RecArea;
}
public int rectanglePerimeter()
{
/**
*Method's function:
* #To calculate the Perimeter of the rectangle by using the inputs "The length and the width".
* #Accessor method to get the Perimeter of the rectangle. It will return the value of the Perimeter.*/
RecPerimeter = (2 * (RecLength + RecWidth));
return RecPerimeter;
}
public boolean equals (Rectangle1 otherObject)
{
return
(this.RecArea == otherObject.RecArea &&
this.RecPerimeter == otherObject.RecPerimeter);
}
}
Rectanlge2:
Code:
import java.util.*;
public class Rectangle2
{
/**
*Private variables:
* #RecLength is a variable to store the length of the rectangle that the user will enter.
* #RecWidth is a variable to store the width of the rectangle that the user will enter.
* #RecArea is a variable to store the area of the rectangle after the calculation.
* #RecParimeter is a variable to store the perimeter of the rectangle after the calculation.*/
private int RecLength;
private int RecWidth;
private int RecArea;
private int RecPerimeter;
Scanner keyboard = new Scanner(System.in);
public void rectangleLength()
{
/**
*Method's function:
* #To get a number greater than 0 from the user.
* #The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
* #Void method to set the length of the rectangle. It will not return any value.*/
while (RecLength < 1)
{
System.out.println("Please, enter the length of the second rectangle: ");
RecLength = keyboard.nextInt();
}
}
public void rectangleWidth()
{
/**
*Method's function:
* #To get a number greater than 0 from the user.
* #The loop used to: re-asking the user to enter a number greater than 0 if the user enter any number less than 1.
* #Void method to set the width of the rectangle. It will not return any value.*/
while (RecWidth < 1)
{
System.out.println("Please, enter the width of the second rectangle: ");
RecWidth = keyboard.nextInt();
}
}
public int rectangleArea()
{
/**
*Method's function:
* #To calculate the area of the rectangle by using the inputs "The length and the width".
* #Accessor method to get the area of the rectangle. It will return the value of the area.*/
RecArea = RecLength * RecWidth;
return RecArea;
}
public int rectanglePerimeter()
{
/**
*Method's function:
* #To calculate the Perimeter of the rectangle by using the inputs "The length and the width".
* #Accessor method to get the Perimeter of the rectangle. It will return the value of the Perimeter.*/
RecPerimeter = (2 * (RecLength + RecWidth));
return RecPerimeter;
}
public boolean equals (Rectangle2 otherObject)
{
return
(this.RecArea == otherObject.RecArea &&
this.RecPerimeter == otherObject.RecPerimeter);
}
}
And a demo Class with a main method to run the program:
Code:
import java.util.*;
public class RectangleDemo
{
public static void main(String[] args)
{
/**
*Making an object of Recangle1 class and Rectanlge2 class "AreaAndPerimeter1"&"AreaAndPerimeter2".
*Calling "rectangleLength" & "rectangleWidth" methods to the main method to ask the user to enter the length and the width to start the calculation.
*Using "println" method to display the results of the area and the perimeter.*/
Rectangle1 AreaAndPerimeter1 = new Rectangle1();
Rectangle2 AreaAndPerimeter2 = new Rectangle2();
System.out.println(":::::Note::::: The length and the width must be greater than 0.");
AreaAndPerimeter1.rectangleLength();
AreaAndPerimeter1.rectangleWidth();
System.out.println("The area of the first rectangle is: " + AreaAndPerimeter1.rectangleArea());
System.out.println("The perimeter of the first rectangle is: " + AreaAndPerimeter1.rectanglePerimeter());
System.out.println("***************************");
AreaAndPerimeter2.rectangleLength();
AreaAndPerimeter2.rectangleWidth();
System.out.println("The area of the first rectangle is: " + AreaAndPerimeter2.rectangleArea());
System.out.println("The perimeter of the first rectangle is: " + AreaAndPerimeter2.rectanglePerimeter());
System.out.println("***************************");
System.out.println("***************************");
if (AreaAndPerimeter1.equals(AreaAndPerimeter2))
System.out.println ("The area & the perimeter of the rectangles match.");
else
System.out.println ("The area & the perimeter of the rectangles don't match.");
}
}
I don't know why the equal method doesn't work correctly?? can anybody help me, please??
Regards,