1. Write code to swap two values:

i n t

x = 5 ;

i n t

y = 1 0 ;

// your code here

// now x should be 10 and y should be 5

(Note that “ x = 10; y = 5;” works for these particular values, but is not a correct swap implementation in general.)

2. We saw that floating point types like double can have small inaccuracies. Write a doubleEquals function that determines whether two doubles are within a certain tolerance of each other (generally 1e-6 (that is, 0.000001) is used).

3. Write a program that simulates rolling two 6-sided dice and prints the resulting total. (Note that this is not the same as creating a random number from 2 to 12, since rolling a 2 is much less likely than rolling a 7.)

4) Implement a game of rock-paper-scissors. Ask for two players’ choices and print out who wins. Remember the difference between == and .equals() for strings. Does your program still work if the user enters “ROCK” / “Rock” / “roCK” instead of “rock”?

5. Write a schedule program that tells the user what they should be doing right now. Ask the user for the current time in 24-hour format and output their scheduled task according to this schedule:

0:00 – 8:29 sleep

8:30 – 8:59 breakfast

9:00 – 17:29 work on CS 111

17:30 – 18:29 dinner

18:30 – 23:59 catch Pokemon



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *