Search code examples
javagetter-setter

Getters and setters not working correctly?


Im trying to make box objects using polymorphism and inheritance, my getters and setters for setting the width work perfectly fine, but for the length i have it written the same format as the getters and setters for width, if the width or length set doesnt fall inbetween the MIN, which is 3, and the MAX, which is 20, set the variable to whatever it is closest to, (eg if length is 24 then set to 20, if length is 2 set to 3) Anyways, this works perfectly fine for the width, but not the length? and im so confused i have no clue why. heres my code.

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

BoxDriver.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

the length of box2 should be set to 3 since it is 2, but its not working? i really have no idea why.

Output

=========================
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)

Solution

  • If you want the variable to go to closest of (MIN/MAX) given it's outside of boundaries, the functions should change to:

    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;
    }