Results 1 to 14 of 14
Thread: Boolean confusion
- 11-01-2009, 07:38 PM #1
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
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(); } }
- 11-01-2009, 07:44 PM #2
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Try changing the two if's to an if and an else if in the method Status.
- 11-01-2009, 07:50 PM #3
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
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 changedtoJava Code:this.full = false;
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?Java Code:this.full = full;
Each newly created box is empty (the constructor should initialize full to false).
- 11-01-2009, 07:52 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
You never used the method setFull, so it keeps the value used in the constructor.
- 11-01-2009, 08:00 PM #5
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
-
- 11-01-2009, 08:38 PM #7
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
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.
-
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:
to 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 }
Best of luck.Java Code:public Box(double height, double width, double depth) { this.height = height; this.width = width; this.depth = depth; this.full = false; }
- 11-01-2009, 08:57 PM #9
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
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?
-
This confuses me since the constructor has nothing to do with the printing.
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.Like, atm I have -- Box firstBox = new Box(10, 15, 20, false);
Again, this makes no sense.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?
- 11-01-2009, 09:46 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Only there u used full.Java Code:this.full = false;
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.
- 11-01-2009, 10:11 PM #13
Member
- Join Date
- Oct 2009
- Location
- Canada
- Posts
- 14
- Rep Power
- 0
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(); }
- 11-02-2009, 12:44 PM #14
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Similar Threads
-
Confusion in line
By JavaJunkie in forum New To JavaReplies: 1Last Post: 06-13-2009, 10:46 PM -
confusion in paragraph
By JavaJunkie in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 05-19-2009, 03:02 PM -
Tic Tac Toe confusion
By jigglywiggly in forum New To JavaReplies: 15Last Post: 04-12-2009, 01:47 AM -
Confusion over arrays
By dbashby in forum New To JavaReplies: 5Last Post: 04-04-2009, 09:38 PM -
[SOLVED] calling a boolean method, confusion!!
By AngrYkIdzrUlE in forum New To JavaReplies: 18Last Post: 03-15-2009, 10:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks