These 4 assignments are due Thursday 9/7 by 11:59pm in your google drive folder. Video version of this lesson can be found here: https://www.youtube.com/watch?v=Y3lFUtqQSw8 and https://www.youtube.com/watch?v=e9LNKjF_Wn4
Note: there are many ways to do while loops and solve these problems. Feel free to solve them however you like!
1. Create a password program (WhilePW.java) similar to what we did in class. Store a String password in your code as a final variable (I don't care what your password actually is) and prompt the user to enter a password. If the user (who cannot see your password unless they look at your actual code) enters the correct password, your program should print out some kind of congratulatory message (keep it clean). If, however, they enter an incorrect password 5 times in a row (keep track of the attempts with a variable), they should not get any more chances and should NOT see the congratulatory message.
Submit WhilePW.java
2. Write a program (WhileBig.java) that allows the user to enter in as many numbers (ints) as they want until they enter 0 (this is the the indication to stop). The program should then output how many numbers were entered (not counting 0), the sum of the numbers (you will update this with each number they type in), and the average (as a double). Here is an example output:
Type a number:
>2
Type a number:
>4
Type a number:
>-5
Type a number:
>0
You entered 3 numbers, the sum is 1, the average is 0.333333333333333
Submit WhileBig.java
3. Create a number guessing program (WhileNumber.java) where the computer will randomly generate an integer from 1 to 100, and then the user will repeatedly guess the number until he/she gets it right. If your guess is too high or too low, the program should tell that to the user. Keep track of how many guesses the user takes (as a separate variable). Here is an example output:
(assume that the computer generated random number 17)
guess? 36
too high
guess? 3
too low
guess? 12
too low
guess? 17
Correct!
That took you 4 guesses.
Submit WhileNumber.java
4. Write a program called WhileTolerance.java. This program will repeatedly ask users to type in two doubles each time through the loop. It will keep doing this until the user types in two numbers that are within 0.1 or less of each other (for instance, 2.1 and 2.12 are within .1 so it should leave the loop. 8.4 and 9.3 are not so it should ask again) and the program will then end.
To help you check that the two numbers are within 0.1 of each other, make sure that their difference is between -0.1 and 0.1. NOTE: if the numbers are exactly .1 away, your program may nor may not stop due to the floating point roundoff issue discussed earlier. Please do not worry about this case, I will not check it.
Here is an example of what your code's output could look like:
Type in two numbers:
>4.5
>6.7
Type in two numbers:
>-2.3
>2.5
Type in two numbers:
>10
>9.94
Yay you did it!
NOTE: it should NOT matter in which order the user types in the two numbers! So if he/she types in 2.1 and then 2.05 OR 2.05 and then 2.1, it should leave the loop in both cases. Absolute value will help, as |a-b| gives the difference between a and b.
Submit WhileTolerance.java
Want something else cool to do for a couple points of EC? Create a palindrome (reads the same forward and backwards) program called WhilePalindrome.java that will let the user enter a String, and will then check to see if it is a palindrome. Your program should disregard spaces (just remove them with replace), so "race car" is a palindrome as is "redivider" and "a man a plan a canal panama". Your program must use a while loop and should only use the code we have learned in class so far (you may not use reverse or charAt, for example).