![](/img/trans.png)
[英]Passing Scanner object as a constructor parameter to the UserInterface class
[英]Adding Scanner in object class
我有運行得很好的代碼,但是我希望用戶自己執行此操作,而不是為對象添加變量。 但是我不知道該怎么做。
在演示/驅動程序中演示該類,該程序要求用戶輸入兩塊土地的尺寸。 程序應演示toString方法以顯示每個土地區域的尺寸和面積,並使用equals方法指示這些區域的大小是否相等,即區域是否具有相同的面積。
對象類:
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;
}
主要方法:
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);
}
}
嘗試
import java.util.Scanner;
方法主要
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);
}
產量
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.