Results 1 to 14 of 14
  1. #1
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default Boolean confusion

    Hey there, I'm trying to print out a line so when I have full set to false, it prints out "Box is empty" and when full is set to true, it prints out "Box is full". I'll show you what I have atm, for some reason it just won't work, it's always printing box is empty even when full is set to true!

    Any help is much appreciated :)

    Java Code:
    /**
     * 
     */
    package q4;
    
    /**
     * @author Your Name Here
     *
     */
    public class BoxTest {
    
        public static void main(String[] args) {
            
            Box firstBox = new Box(10, 15, 20, false);
            Box secondBox = new Box(23, 9, 14, false);
            Box thirdBox = new Box(54, 67, 98, true);
    
            System.out.println("First Box: " + firstBox);
            System.out.println();
            System.out.println("Second Box: " + secondBox);
            System.out.println();
            System.out.println("Third Box: " + thirdBox);
            
        }
    
    }

    Java Code:
    package q4;
    
    public class Box {
    
        private double height, width, depth;
    
        private boolean full;
    
        public Box(double height, double width, double depth, boolean full) {
            this.height = height;
            this.width = width;
            this.depth = depth;
            this.full = false;
        }
    
        public double getHeight() {
            return height;
        }
    
        public double getWidth() {
            return width;
        }
    
        public double getDepth() {
            return depth;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public void setWidth(double width) {
            this.width = width;
        }
    
        public void setDepth(double depth) {
            this.depth = depth;
        }
    
        public boolean isFull() {
            return full;
        }
    
        public void setFull(boolean full) {
            this.full = full;
        }
    
        private String Status() {
            String status = null;
    
            if (full == false) {
                status = "empty";
            }
    
            if (full == true) {
                status = "full";
            }
            
            return status;
        }
    
        public String toString() {
            return "Depth = " + depth + ", Height = " + height + ", Width = "
                    + width + ", Box is " + Status();
        }
    
    }

  2. #2
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Try changing the two if's to an if and an else if in the method Status.

  3. #3
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by bubbless View Post
    Try changing the two if's to an if and an else if in the method Status.
    I tried that and nothing changed. I'm curious though, what is the difference between two if's and an if and else if? My book doesn't mention else if anywhere.

    I changed
    Java Code:
    this.full = false;
    to
    Java Code:
    this.full = full;
    in my constructor, and it works now. but the question says I should initialize full to false so I think i'm cheating when I do it this way?

    Each newly created box is empty (the constructor should initialize full to false).

  4. #4
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    You never used the method setFull, so it keeps the value used in the constructor.

  5. #5
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by bubbless View Post
    You never used the method setFull, so it keeps the value used in the constructor.
    Don't setters have to be of type void though? I tried putting
    Java Code:
    String status = null;
    
            if (full == false) {
                status = "empty";
            }
    
            if (full == true) {
                status = "full";
            }
            
            return status;
    in setFull but void methods can't return anything...

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by KM88 View Post
    I changed
    Java Code:
    this.full = false;
    to
    Java Code:
    this.full = full;
    in my constructor, and it works now. but the question says I should initialize full to false so I think i'm cheating when I do it this way?
    I think we'll be guessing here unless we see your exact assignment requirements.

  7. #7
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I think we'll be guessing here unless we see your exact assignment requirements.
    oh I see, ok here is the whole description. :)

    Design and implement a class called Box that contains instance data that represents the height, width, and depth of the box. Also include a boolean variable called full as instance data that represents whether the box is full or not. Define a Box constructor to accept and initialize the height, width, and depth of the box. Each newly created box is empty (the constructor should initialize full to false). Include getter and setter methods for all instance data. Include a toString method that retuns a one-line description of the box. Create a driver, called BoxTest, whose main method instantiates and updates two Box objects. BoxTest should call each Box method at least once and print enough information to indicate that the methods worked correctly.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    My interpretation of this is that the constructor shouldn't even take a boolean parameter and that in the body of the constructor you'll set full to false as the default value. Then if you wish to change full to true, you would make use of the setter method to set full after constructing your Box object.

    In other words, change your constructor from this:
    Java Code:
        public Box(double height, double width, double depth, boolean full) {
            this.height = height;
            this.width = width;
            this.depth = depth;
            this.full = false;
            // this.full = full;  // definitely don't want this
        }
    to this:
    Java Code:
        public Box(double height, double width, double depth) {
            this.height = height;
            this.width = width;
            this.depth = depth;
            this.full = false;
        }
    Best of luck.

  9. #9
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Java Code:
        public Box(double height, double width, double depth) {
            this.height = height;
            this.width = width;
            this.depth = depth;
            this.full = false;
        }
    Best of luck.
    this is what I originally had, but changed it when I couldn't figure out how to do the printing part if it was like this.

    Like, atm I have -- Box firstBox = new Box(10, 15, 20, false);

    if I no longer have that false part in there, how can I make it so that it prints out that the box is empty?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by KM88 View Post
    this is what I originally had, but changed it when I couldn't figure out how to do the printing part if it was like this.
    This confuses me since the constructor has nothing to do with the printing.

    Like, atm I have -- Box firstBox = new Box(10, 15, 20, false);
    Then change this so that there is no "false" in your constructor call. Again, you are restrained by your instructor's condition. In other words, you don't have a choice in this matter.


    if I no longer have that false part in there, how can I make it so that it prints out that the box is empty?
    Again, this makes no sense.

  11. #11
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Java Code:
    this.full = false;
    Only there u used full.
    You need to call firstbox.setFull(false);

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by bubbless View Post
    Java Code:
    this.full = false;
    Only there u used full.
    You need to call firstbox.setFull(false);
    I politely disagree. Since all boxes will default to full = false on construction, you only need to call this method initially if you want to change full to true. Later on, if you want to change a full box to not-fall, then of course you'll need to call this method passing a false parameter.

  13. #13
    KM88 is offline Member
    Join Date
    Oct 2009
    Location
    Canada
    Posts
    14
    Rep Power
    0

    Default

    thanks bubbless and Fubar, I think I understand now and got it working properly :) here is what I have, does it all look right?

    Java Code:
    public class BoxTest {
    
        public static void main(String[] args) {
    
            Box firstBox = new Box(10, 15, 20);
            Box secondBox = new Box(23, 9, 14);
            Box thirdBox = new Box(54, 67, 98);
            Box fourthBox = new Box(2, 17, 37);
    
            firstBox.setFull(true);
            thirdBox.setFull(true);
    
            System.out.println("First Box: " + firstBox);
            System.out.println();
            System.out.println("Second Box: " + secondBox);
            System.out.println();
            System.out.println("Third Box: " + thirdBox);
            System.out.println();
            System.out.println("Fourth Box: " + fourthBox);
    
        }

    Java Code:
    public class Box {
    
        private double height, width, depth;
    
        private boolean full;
    
        public Box(double height, double width, double depth) {
            this.height = height;
            this.width = width;
            this.depth = depth;
            this.full = false;
        }
    
        public double getHeight() {
            return height;
        }
    
        public double getWidth() {
            return width;
        }
    
        public double getDepth() {
            return depth;
        }
    
        public void setHeight(double height) {
            this.height = height;
        }
    
        public void setWidth(double width) {
            this.width = width;
        }
    
        public void setDepth(double depth) {
            this.depth = depth;
        }
    
        public boolean isFull() {
            return full;
        }
    
        public void setFull(boolean full) {
            this.full = full;
    
        }
    
        private String Status() {
            String status = null;
    
            if (full == false) {
                status = "empty";
            }
    
            if (full == true) {
                status = "full";
            }
            return status;
        }
    
        public String toString() {
            return "Height = " + height + ", Width = " + width + ", Depth = "
                    + depth + ", Box is " + Status();
        }

  14. #14
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    Does it do what it wants you to do?
    If yes, then it works.

Similar Threads

  1. Confusion in line
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 06-13-2009, 10:46 PM
  2. confusion in paragraph
    By JavaJunkie in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-19-2009, 03:02 PM
  3. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM
  4. Confusion over arrays
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 09:38 PM
  5. [SOLVED] calling a boolean method, confusion!!
    By AngrYkIdzrUlE in forum New To Java
    Replies: 18
    Last Post: 03-15-2009, 10:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •