class X9{
public static void main( String argv[]
) {
boolean flip;
if(.5>Math.random())
flip=true;
else
flip=false;
if (flip)
System.out.println("\n" + "Heads");
else
System.out.println("\n" + "Tails");
System.out.println("\n");
//*********** a switch example **************
int i;
for (i = 4; i > 0; i--) {
switch ( i ){
case 4:
System.out.println("Ready");
break;
case 3:
System.out.println("On your marks");
break;
case 2:
System.out.println("Get set");
break;
case 1:
System.out.println("Go");
break;
default:
System.out.println("Restart");
}
try{
Thread.sleep(1000);
}
catch(InterruptedException
io){
}
}
}
}
Q1 Modify the above code to do the following. Create a boolean value,
with the identifier
Sunday. The code should print out to
console, 'Rest' if the condition of the if...else
statement is true and 'Work' if it is
false. // there's an example
like this in the note
Considering the switch example in the code above . . .
Q2 What happens (in example 2) if the condition in the for loop of the
switch above is
changed to i > -1? What is going on
here?
Q3 Comment out the 'break' keywords in the above switch. What happens? Why?
Q4 Adapt the above switch code to print out a count down for a rocket
take off.
i.e. ten, nine eight ... blastoff! Make the thread
sleep for 1000 milliseconds
between counts.
Q5 Write a for loop that loops 10 times. Put in a print statement
that prints the index i
at each loop. Includen an 'if' statement
whose 'else' part causes the the 5th iteration's
index not to be printed out. (Printing
continues on the 6th iteration)
Q6 Copy the loop you created in Q5 and change it so it stops printing after the 5th iteration.
Q7 Put the int value 99 into an instance of Integer. Look in the JDK
API, in a text or in your
notes to get a method to return it to
an identifier with type double. Print this value to screen.