Oct.04 (Wed) -- today we will actually have lecture in the classroom (discussing boolean values, and various flavors of if statements).
Oct.06 (Fri):
Chapter 5 mentions the boolean functions
&&
(“and”)
and
||
(“or”).
For instance,
((30 <= choice) && (choice < 33)) |
((newFace < 1) || (newFace > NUM_SIDES)) |
In general, && looks at the boolean expressions on each side, and the entire && will evaluate to true if (and only if) the left side and the right side evaluate to true:
expr1 | expr2 | (expr1&&expr2) |
false | false | false |
false | true | false |
true | false | false |
true | true | true |
expr1 | expr2 | (expr1||expr2) |
false | false | false |
false | true | true |
true | false | true |
true | true | true |
Exercise: What is the value of:
( ((3 <= 7) || (4 == (2+3))) && (Math.sqrt(4) > 0.0) ) |
While we're on the topic, there is one last boolean operator, ! (“not”):
expr | !expr |
false | true |
true | false |
For this assignment, work individually or with a partner. You should check this program off before you leave lab today; if you need to, you can check it off during the first five minutes of the next lab. (In that case, you may may want to be sure both partners have a copy of what's been written, as you leave lab.)
Optional: When writing this sum, have the bigger die is listed first (5, in this case). Hint: you can either have use an if statement to tests which die is bigger, or you can use Math.max and Math.min. Either way, don't have repeated code.
Next, change the
toString() method for Die
so that if it's a six-sided die,
it returns a String which represents
the die graphically,
using periods or lowercase os or whatever.
(Feel free to make yours look even better than the samples below.)
(If the die has more or fewer than six sides,
you can just return the same string as it did before.)
Note: To best see your formatted output,
print to the console window: something like
System.out.println( someDie.toString() );.
(While the important part is returning a String,
you can call this method any way you like,
the proper way to print a newline character is a bit philosophical;
if you look at a string from BlueJ's method-call window, you'll
see the newline printed as ‘\n’,
and if you call toString from the codepane, BlueJ adds
a set of quotation marks around the whole thing,
which disrupts your formatting.)
. . . . . . . Lucky 7
. . . . . . . . 8 (from a 5 and a 3)
To think about, for the future:
How might we be able to print out the face of a Die which
has (many) more than six sides?
3Just like we think of + and * as functions which take in two numbers, and return a number. back
4If you look at the tables and squint, thinking of false,true being like 0,1 and ||,&& being like +,* then these tables above really do look fairly similar to arithmetic. back
5Well, you would think not being greater than three is the same as being less than three, but actually there is a little glitch: try this with double x = Double.NaN, which stands for “not a number”. (For example, Math.log(-1) returns NaN.) NaN is not greater than three, nor is it less-than-or-equal to three. Heck, NaN isn't even equal to itself?!) back
©2006, Ian Barland, Radford University Last modified 2006.Oct.12 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |