In: Computer Science
Write a java program using the following information
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.
//Implementation of Land Tract class..
public class LandTract
{
//declare two variable length and width as double..
private double length;
private double width;
//Getter and setter method for length and width..
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;
}
//A constructor that accepts arguments for the two fields
public LandTract(double length, double width) {
super();
this.length = length;
this.width = width;
}
//Method that calculate tract area..
public double tractArea(double length,double width)
{
double area=length*width;
return area;
}
//Equals method that checks two objects are equal or not..
public boolean equals(LandTract obj1, LandTract obj2)
{
boolean b=false;
if(obj1.getLength()==obj2.getLength() && obj1.getWidth()==obj2.getWidth())
{
b=true;
}
else
{
b=false;
}
return b;
}
//toString method that return the string about the information...
public String toString()
{
return "Length="+length+"\nwidth="+width+"\nArea=" + width*length;
}
}
//Implementation of program by using main class...
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
//create an array of class LandTrat type of size 3..
LandTract[] arr=new LandTract[3];
//To take the input for length and width for tract1..
System.out.println("Enter the length of Tract1: ");
int l1=sc.nextInt();
System.out.println("Enter the width of Tract1: ");
int w1=sc.nextInt();
//create first object Landtract Type by passing l1 and w1..
arr[0]=new LandTract(l1,w1);
//To take the input for length and width for tract2..
System.out.println("Enter the length of Tract2: ");
int l2=sc.nextInt();
System.out.println("Enter the width of Tract2: ");
int w2=sc.nextInt();
//create second object Landtract Type by passing l2 and w2..
arr[1]=new LandTract(l2,w2);
//To take the input for length and width for tract3..
System.out.println("Enter the length of Tract3: ");
int l3=sc.nextInt();
System.out.println("Enter the width of Tract3: ");
int w3=sc.nextInt();
//create third object Landtract Type by passing l3 and w3..
arr[2]=new LandTract(l3,w3);
//Print the information for LandTract 1..
System.out.println("Info of LandTract 1:");
System.out.println(arr[0].toString());
//Print the information for LandTract 2..
System.out.println("Info of LandTract 2:");
System.out.println(arr[1].toString());
//Print the information for LandTract 3..
System.out.println("Info of LandTract 3:");
System.out.println(arr[2].toString());
//check two objects landtract1 and landtrack2 are equals or not..
boolean b1=arr[0].equals(arr[0],arr[1]);
//check two objects landtract2 and landtrack3 are equals or not..
boolean b2=arr[1].equals(arr[1],arr[2]);
//check two objects landtract1 and landtrack3 are equals or not..
boolean b3=arr[2].equals(arr[0],arr[2]);
//Print the information about which objects are equal...
if(b1 && b2 && b3)
{
System.out.print("All the three tracts are of same size.");
}
else if(b1)
{
System.out.print("The first and second tracts are the same size.");
}
else if(b2)
{
System.out.print("The second and third tracts are the same size.");
}
else if(b3)
{
System.out.print("The first and third tracts are the same size.");
}
else
{
System.out.print("None of the tracts are of same size.");
}
}
}