Radford University ITEC

ITEC120

lab07:
exercising if

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))
reads as “choice is at least thirty, and less than 33”. Similarly,
((newFace < 1) || (newFace > NUM_SIDES))
tests for newFace being less than 1, or bigger than 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 and expr2 can be any other boolean expression—including more-deeply-nested and/or expressions!)
Complementing &&, an entire || expression evaluates to true if (and only if) one side or the other evaluates to true:
expr1 expr2 (expr1||expr2)
false false false
false true true
true false true
true true true
Really, you can think of && as a function with the signature boolean &&( boolean b1, boolean b2 ) (although, like arithmetic3, we write them in infix notation, with the function-name coming inbetween its two inputs, rather than in-front-of-them-with-parentheses).

Exercise: What is the value of:

( ((3 <= 7) || (4 == (2+3))) && (Math.sqrt(4) > 0.0) )
This is just like solving an arithmetic problem, except that we are dealing with true and false instead of all the integers4.

While we're on the topic, there is one last boolean operator, ! (“not”):

expr !expr
false true
true false
So (2+2 == 4) is true, but !(2+2 == 4) is false. This could also have been written directly using the not-equals operator: (2+2 != 4).


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

  1. Create a class PairODice, which contains two six-sided Dice, and a method roll which rolls both of them. (So far, that's just like class Player, which we've already seen.)
  2. Write a method nameOfRoll which looks at the two dice and returns a String: Hint: write just a couple of these cases and test, before writing all of them. Remember the if-else-if code from lecture.
  3. 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.)

  4. Finally, have PairODice's toString include a picture of each die, subtitled with a text description. Here are two samples:
    1. 
        .
        
        
      . . .
      
      . . . 
      
      Lucky 7
      
    2. .   .
        . 
      .   .
       
      .   
        .  
          .
      8 (from a 5 and a 3)
      
  5. Optional — if you have time:
    We would like to know whether or not a roll is good, and whether or not a roll is bad. (This information might be used by other methods to decide, say, whether to print a smiley-face or perhaps play a certain sound file.)

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
Powered by PLT Scheme