๐ฏ Unit 2 Activities
Table of Contents
๐ป ACTIVITY PROGRAM SETUP INSTRUCTIONS
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository - Specify the repository name:
CS2-Unit-2-ActivityReplace
#with the specific activity number. - Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositoriessection of GitHub! ๐ - Now on your repository, click and select the
Codespacestab - Click
Create Codespace on mainand wait for the environment to load, then youโre ready to code!
ACTIVITY #1: Magic 8 Ball ๐ฎ
Have you ever used a Magic 8 ball? You ask it a yes-no question, and then shake it to get a random answer like Signs point to yes!, Very doubtful, etc.
Try out this Magic 8 Ball Simulator
In this activity, we will:
- Generate and use random values correctly.
- Practice selection with
if-else if-elseblocks (chain structures) and conditions (booleanvalues or expressions).
PART A: Printing Randomly-Selected Messages
Write a branching program inside the main() method that does the following:
- Declare a variable called
numand assign it a random integer between 1 to 10, inclusive.To review generating random numbers, see ๐ 1.11: Math Class
- Use conditional statements to test the value of
numand print out a different message for each number.Come up with 10 creative responses to yes-no questions!
Review: if-else if-else
if ( condition ) {
statement1;
}
else if ( condition ) {
statement2;
}
else {
statement3;
}
This structure can accomodate more than 3 choices easily โ just add an
else ifblock for every possibility after the firstifstatement, and before the finalelseblock.
PART B: Coin Toss
- Generate another random number at the top of the program, this time make it a random double between 0โ1. Store it in a variable called
luck. - In a nested
ifstatement (inside one of your existing conditional blocks) test the value ofluckas a โfollow upโ after printing the Magic 8 Ball message. - If it is greater than or equal to 0.5, print the message
"LUCKY! ๐"
Review: nested if
if ( condition ) {
statement1;
if ( condition ) {
statement2;
}
}