我试图使用多态和继承制作盒子对象,我设置宽度的getter和setter工作得非常好,但是对于长度我写的格式与getter和setter的宽度相同,如果宽度或长度设置不下降在MIN(即3)和MAX(20)之间,将变量设置为最接近的变量(例如,如果长度为24,则设置为20,如果长度为2,则设置为3)无论如何,这完美地工作宽度很好,但不是长度?我很困惑,我不知道为什么。继承我的代码。
box.Java
package l08;
public class Box {
/** Minimum size for either side of the box */
public static final int MIN = 3;
/** Maximum size for either side of the box */
public static final int MAX = 20;
/** The width of the box */
private int width;
/** The length of the box */
private int length;
/** * A symbol to be used to draw a box */
private char symbol;
/** Boolean to indicate whether the box is filled or not */
private boolean filled;
/**
* Constructor
*
* @param startWidth
* @param startLength
* @param startSymbol
* @param startFilled
*/
public Box(int startWidth, int startLength, char startSymbol,
boolean startFilled) {
if (length < MIN) {
length = MIN;
}
if (length > MAX) {
length = MAX;
}
this.width = startWidth;
this.length = startLength;
this.symbol = startSymbol;
this.filled = startFilled;
if (width < MIN) {
width = MIN;
}
if (width > MAX) {
width = MAX;
}
}// end constructor
/**
* Draw this box.
*/
public void drawBox() {
for(int star = 3; star < getLength(); star++)
System.out.print(getSymbol());
System.out.print("\n"+getSymbol());
for(int space = 0; space < getLength()-2; space++) System.out.print("
");
System.out.print(getSymbol() + "\n");
for(int star = 0; star < getLength(); star++)
System.out.print(getSymbol());
System.out.println();
}
/**
* Get the value of filled
*
* @return the value of filled
*/
public boolean isFilled() {
return filled;
}
/**
* Set the value of filled
*
* @param newFilled new value of filled
*/
public void setFilled(boolean newFilled) {
this.filled = newFilled;
}
/**
* Get the value of symbol
*
* @return the value of symbol
*/
public char getSymbol() {
// Activity 3, implement the getters
return symbol;
}
/**
* Set the value of symbol
*
* @param newSymbol new value of symbol
*/
public void setSymbol(char newSymbol) {
this.symbol = newSymbol;
}
/**
* Get the value of length
*
* @return the value of length
*/
public int getLength() {
return length;
}
/**
* Set the value of length
*
* @param newLength new value of length
*/
public void setLength(int newLength) {
if (length >= MIN || length <= MAX) {
length = newLength;
}
}
/**
* Get the value of width
*
* @return the value of width
*/
public int getWidth() {
// Activity 3, implement the getters
return width;
}
/**
* Set the value of width
*
* @param newWidth new value of width
*/
public void setWidth(int newWidth) {
if (width >= MIN || width <= MAX) {
newWidth = width;
}
}
}// end class
box driver.Java
package l08;
import java.util.Scanner;
public class BoxDriver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
//Testing constructors, if everything is good, then nothing will be
//printed!
System.out.println("\n\n=========================");
System.out.println("Testing the constructor");
System.out.println("=========================");
Box b1 = new Box(30,15,'*',true);
Box b2 = new Box(-2,-2,'#',false);
Box b3 = new Box(2,5,'*',false);
System.out.println("\n(If nothing is printed, "
+ "there were no run-time errors!)");
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
//Testing the getters of the Box class
System.out.println("\n\n=========================");
System.out.println("Testing the getters");
System.out.println("=========================");
System.out.println("Box 1: width=" + b1.getWidth()
+ " and length=" + b1.getLength()
+ "\t\t(should be 20 and 15)");
System.out.println("Box 2: symbol=" + b2.getSymbol()
+ " and filled=" + b2.isFilled() + "\t(should be # and false)");
System.out.println("Box 3: width=" + b3.getWidth()
+ " and length=" + b3.getLength() + "\t\t(should be 3 and 5)");
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
//Testing the setters of the Box class
System.out.println("\n\n=========================");
System.out.println("Testing the setters");
System.out.println("=========================");
b1.setFilled(false);//changing it to non-filled box
b1.setWidth(500);
b1.setLength(14);
b1.setSymbol('A');
System.out.println("Box 1: width=" + b1.getWidth()
+ " and length=" + b1.getLength() + " "
+ "symbol=" + b1.getSymbol()
+ " filled=" + b1.isFilled()
+ "\n(should now be changed to 20, 14, A, and false)");
b2.setFilled(true);//changing it to non-filled box
b2.setWidth(2);
b2.setLength(2);
System.out.println("\nBox 2: width=" + b2.getWidth()
+ " and length=" + b2.getLength() + " "
+ "symbol=" + b2.getSymbol()
+ " filled=" + b2.isFilled()
+ "\n(should now be changed to 3, 3, #, and true)");
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
//Testing drawBox() method
System.out.println("\n\n=========================");
System.out.println("Testing drawBox() method");
System.out.println("=========================");
System.out.println("Drawing box #1 (" + b1.getWidth() + "x"
+ b1.getLength() + ", not filled)");
b1.drawBox();
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
System.out.println("\n\nDrawing box #2 (" + b2.getWidth() + "x"
+ b2.getLength() + ", filled)");
b2.drawBox();
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
System.out.println("\n\nDrawing box #3 (" + b3.getWidth() + "x"
+ b3.getLength() + ", not filled)");
b3.drawBox();
System.out.print("\nPress ENTER to continue...");
kb.nextLine();
}//end main()
}//end class
box2的长度应该设置为3,因为它是2,但它不起作用?我真的不知道为什么。
产量
=========================
Testing the constructor
=========================
(If nothing is printed, there were no run-time errors!)
Press ENTER to continue...
=========================
Testing the getters
=========================
Box 1: width=20 and length=15 (should be 20 and 15)
Box 2: symbol=# and filled=false (should be # and false)
Box 3: width=3 and length=5 (should be 3 and 5)
Press ENTER to continue...
=========================
Testing the setters
=========================
Box 1: width=20 and length=14 symbol=A filled=false
(should now be changed to 20, 14, A, and false)
Box 2: width=3 and length=2 symbol=# filled=true
(should now be changed to 3, 3, #, and true)
如果你想让变量最接近(MIN / MAX),如果它在边界之外,那么函数应该改为:
public void setLength(int newLength) {
if (newLength <= MIN)
length = MIN;
else if(newLength >= MAX)
length = MAX;
else
length = newLength;
}
public void setWidth(int newWidth) {
if (newWidth <= MIN)
width = MIN;
else if(newWidth >= MAX)
width = MAX;
else
width = newWidth;
}
您在setLength
和setWidth
中有相同的错误(只是变量名称不同);即,您已经测试了现有的length
(或width
)属性而不是新值。此外,你想要一个逻辑和。更改,
public void setLength(int newLength) {
if (length >= MIN || length <= MAX) {
length = newLength;
}
}
至
public void setLength(int newLength) {
if (newLength >= MIN && newLength <= MAX) {
length = newLength;
}
}
并改变
public void setWidth(int newWidth) {
if (width >= MIN || width <= MAX) {
newWidth = width;
}
}
实际更新width
...
public void setWidth(int newWidth) {
if (newWidth >= MIN && newWidth <= MAX) {
width = newWidth;
}
}