Lab 11 -- Boolean Quiz -- Ifs in a Loop, Ternary Op, Boolean Logic

Lab 10 Extension: add a running average

Objectives

·       Write branching statement(s): a switch or if will work.

·       Use a loop.

·       Use randomness, i.e. Math.random()

·       Use the ternary/conditional operator

·       Review Boolean operations

·       parse boolean input with Boolean.parseBoolean()

 

Specifications(Directions) – Old Lab 10

1.       Read these specifications/requirements entirely before starting

2.       Write a program to randomly generate problems to test boolean logic:

3.       Your questions should be random. Use Math.random().

a.       Your operations ( &&, &, |, ||, !, ^ ) should be random.

b.      ^ is XOR, exclusive or.

                                                                           i.      XOR is true if only one operand is true, otherwise false

                                                                         ii.      stated differently, xor is "Or but not And" for two operands

c.       Your operands to the operation should be random as well (just true or false).

d.      Math.random() returns a floating point number range [0-1), from zero to just less than one.

e.       (int)(Math.random()*3) is an expression that returns a number randomly from {0,1,2}

4.       You may want to consider your previous lab for the loop.

a.       You may want to make the loop condition infinite but exit() in the loop.

b.      It should loop forever until the user enters "quit"

c.       You don't need the num variable.

5.       The conditional/ternary operator is a good idea here for the simpler logic. Use it somewhere appropriate.

6.       Make sure you actually check the answer given and return an appropriate message.

7.       You can parse a string to get a boolean value with Boolean.parseBoolean(ans) or just use String.equals()

Extension – Add variables and output to keep track of running average percent correct

Make the necessary changes to allow your code to keep track of the ratio of correct answers to total answers and then output the current ratio correct to total, as a percent. It should output after every answer. See following ouput:

 

Report Questions

1.       Give a flowchart for your solution, including the while loop.