I have code that works really well, but instead of putting in the variables, for the object, I want the user to do it themselves. But I don't know how to do that.
Demonstrate the class in a demo/driver program that asks the user to enter the dimensions for two tracts of land. The program should demonstrate the toString method to display the dimensions and area for each tract of land and utilize the equals method to indicate whether the tracts are of equal size, ie, tracts have the same area.
Object class:
public class MitchellLandTract
{
private int length;
private int width;
private int area;
/**
* Constructor
* @param l length
* @param w width
*/
public MitchellLandTract(int l, int w)
{
length = l;
width = w;
}
/**
* toString for the land tract
* @param str the string describing the object
*/
public String toString()
{
// Create a string describing the land tract.
String str = "With the width, " + width +
", and the length, " + length +
". The area is " + area;
// Return the string.
return str;
}
/**
* getLength method
* @return area of land tract
*/
public double getArea()
{
area = length * width;
return area;
}
/**
* equals method
* @param object2 the object being compared to original
* @return if the areas are the same or different
*/
public boolean equals(MitchellLandTract object2)
{
boolean status;
// Determine whether this object's area field
// is equal to object2's area field
if (getArea()==object2.getArea())
status = true; // Yes, the objects are equal.
else
status = false; // No, the objects are not equal.
// Return the value in status.
return status;
}
Main method:
public class MitchellLab8
{
public static void main(String[] args)
{
// Create two MitchellLandTract objects with the same values
MitchellLandTract land1 = new MitchellLandTract(5, 10);
MitchellLandTract land2 = new MitchellLandTract(4, 16);
// Use the equals method to compare the objects
if (land1.equals(land2))
System.out.println("Both objects are the same.");
else
System.out.println("The objects are different.");
// Display the objects' values
System.out.println("For the first tract of land: " + land1);
System.out.println("For the second tract of land: " + land2);
}
}
Try
import java.util.Scanner;
Method main
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Input Land 1");
int land1_A_Input = scan.nextInt();
int land1_B_Input = scan.nextInt();
System.out.println("Input Land 1");
int land2_A_Input = scan.nextInt();
int land2_B_Input = scan.nextInt();
// Create two MitchellLandTract objects with the same values
MitchellLandTract land1 = new MitchellLandTract(land1_A_Input, land1_B_Input);
MitchellLandTract land2 = new MitchellLandTract(land2_A_Input, land2_B_Input);
// Use the equals method to compare the objects
if (land1.equals(land2))
System.out.println("Both objects are the same.");
else
System.out.println("The objects are different.");
// Display the objects' values
System.out.println("For the first tract of land: " + land1);
System.out.println("For the second tract of land: " + land2);
}
Output
Input Land 1
4 5
Input Land 1
10 5
The objects are different.
For the first tract of land: With the width, 5, and the length, 4. The area is 20
For the second tract of land: With the width, 5, and the length, 10. The area is 50
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.