In: Computer Science
Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:
a constructor that accepts arguments for the two fields
a method that returns the tract’s area(i.e. length * width)
an equals method that accepts a LandTract object as an argument. If the argument object
holds the same data (i.e. length and width) as the calling object, this method should return
true. Otherwise, it should return false.
a toString method (that returns a String representation of the tract’s length, width, and
area).
Demonstrate the class in a program by creating an array of three LandTract objects initialized with user input. Then, print the info of the LandTract objects stored in the array by invoking the toString method (implicitly or explicity). Also, use the equals method to compare at least two LandTract objects and print wether they are of equal size or not.
Sample Output:
Enter the length of Tract 1: 2
Enter the length of Tract 1: 3
Enter the length of Tract 2: 2
Enter the length of Tract 2: 3
Enter the length of Tract 3: 4
Enter the length of Tract 3: 5
Info of LandTract 1:
Length = 2.0
Width = 3.0
Area = 6.0
Info of LandTract 2:
Length = 2.0
Width = 3.0
Area = 6.0
Info of LandTract 3:
Length = 4.0
Width = 5.0
Area = 20.0
The first and second tracts are the same size.
Two classes LandTrack and Test are provided in two separate files.
Test class is the driver class with the main method
Instructions on running the program from a terminal:
- javac LandTrack.java Test.java
- java Test
LandTract.java
public class LandTract{
// Instance variables representing length and width of land tract
private double length;
private double width;
// Parameterized constructor
LandTract(double length, double width) {
this.length = length;
this.width = width;
}
// Method to get Area of land tract
public double getArea() {
return length * width;
}
// Getter method for private field length
public double getLength() {
return length;
}
// Setter method for private field length
public void setLength(double length) {
this.length = length;
}
// Getter method for private field width
public double getWidth() {
return width;
}
// Setter method for private field width
public void setWidth(double width) {
this.width = width;
}
// Comparing this object with another object
public boolean equals(LandTract obj) {
if (obj.getLength() == this.length && obj.getWidth() == this.width) {
return true;
}
return false;
}
// String representation of object
@Override
public String toString() {
return "Length = " + length + "\nWidth = " + width + "\nArea = " + getArea();
}
}
Test.java (Driver class)
import java.util.*;
// Driver class
public class Test {
public static void main(String[] args) {
// Using scanner to read user input
Scanner sc = new Scanner(System.in);
// Size of the array
int arrSize = 3;
// Array to store LandTract objects
LandTract[] landTractArr = new LandTract[arrSize];
// Initializing the landTractArr by user input
for(int i = 0; i < arrSize; i++) {
System.out.print("Enter the length of Tract " + (i+1) + ": ");
double length = sc.nextDouble();
System.out.print("Enter the width of Tract " + (i+1) + ": ");
double width = sc.nextDouble();
landTractArr[i] = new LandTract(length, width);
}
// Invoking toString method to print details of objects
for(int i = 0; i < arrSize; i++) {
System.out.println("\nInfo of LandTract " + (i+1) + ":");
System.out.println(landTractArr[i]);
}
// Comparing first and second objects
if(landTractArr[0].equals(landTractArr[1])) {
System.out.println("\nThe first and second tracts are the same size.");
} else {
System.out.println("\nThe first and second tracts are NOT the same size.");
}
}
}