In: Computer Science
Make a LandTract class with the following fields:
• length - an int containing the tract's length
• width - an int containing the tract's width
The class should also have the following methods:
• area - returns an int representing the tract's area
• equals - takes another LandTract object as a parameter and
returns a boolean saying
whether or not the two tracts have the same dimensions (This
applies regardless of whether the dimensions match up. i.e., if the
length of the first is the same as the width of the other and vice
versa, that counts as having equal dimensions.)
• toString - returns a String with details about the LandTract
object in the format:
LandTract object with length 30 and width 40
(If, for example, the LandTract object had a length of 30 and a
width of 40.)
Write a separate program that asks the user to enter the dimensions
for the two tracts of
land (in the order length of the first, width of the first, length
of the second, width of the second). The program should print the
output of two tracts' toString methods followed by a sentence
stating whether or not the tracts have equal dimensions. (If the
tracts have the same dimensions, print, "The two tracts have the
same size." Otherwise, print, "The two tracts do not have the same
size.") Print all three statements on separate lines.
//LandTract.java
public class LandTract {
private int length;
private int width;
public LandTract(){
}
public LandTract(int length,int width){
this.length=length;
this.width=width;
}
public void setLength(int length){
this.length=length;
}
public void setWidth(int width){
this.width=width;
}
public int getLength(){
return
this.length;
}
public int getWidth(){
return this.width;
}
public int area()
{
return
this.length*this.width;
}
public boolean equals(LandTract t){
return
t.area()==this.area();
}
public String toString(){
return "LandTract object
with length "+this.length+" and width "+this.width;
}
}
//Tester.java
import java.util.Scanner;
public class Tester {
public static void main(String args[]) {
Scanner sc = new
Scanner(System.in);
int len,wid;
System.out.print("Enter length for
1st tract of land: ");
len=sc.nextInt();
System.out.print("Enter width for
1st tract of land: ");
wid=sc.nextInt();
LandTract tract1 = new
LandTract(len,wid);
System.out.print("Enter length for
2nd tract of land: ");
len=sc.nextInt();
System.out.print("Enter width for
2nd tract of land: ");
wid=sc.nextInt();
LandTract tract2 = new
LandTract(len,wid);
System.out.println(tract1);
System.out.println(tract2);
if (tract1.equals(tract2))
System.out.println("The two tracts
have the same size.");
else
System.out.println("The two tracts
do not have the same size.");
}
}