These labs are due by 11:59 pm on Wednesday 9/13 in your google drive folder. These labs will take a significant amount of time and will require some work outside of class.
Here is a video version of this lesson: https://www.youtube.com/watch?v=aVK5wWLkSdg and https://www.youtube.com/watch?v=YwuwYAnXhx8
1. Write a program (ForMilk.java - public class ForMilk) that will use a for loop to print out the lyrics of the song 99 bottles of milk on the wall. Specifically, the lyrics go like this:
99 bottles of milk on the wall, 99 bottles of milk. Take one down, pass it around, 98 bottles of milk on the wall!
98 bottles of milk on the wall, 98 bottles of milk. Take one down, pass it around, 97 bottles of milk on the wall!
[...]
1 bottle of milk on the wall, 1 bottle of milk. Take one down, pass it around, 0 bottles of milk on the wall!
Notice how there are two special cases at the end for when there is 1 bottle left!
Submit ForMilk.java
2. Write a program (ForSum.java) that will ask the user to input a non-negative integer (it should ask them to re-enter the number if they put in a negative). Your program will then use a for loop to calculate the sum of every int from 1 through that number. For instance, if they type in 6, it should print out 21 (1+2+3+4+5+6=21). If they type in 10, the program should print 55. When this works, submit ForSum.java. [HINT: to help you with this, create a variable before the loop to keep track of the sum so far. Every time through the loop, keep adding to this sum. Your WhileBig lab from last assignment may be useful.]
3. Write a program (ForFactorial.java) that will ask the user to input a non-negative integer (it should ask them to re-enter the number if they put in a negative) and then use a for loop to calculate the factorial (!) of their number. Recall that 6! = 6*5*4*3*2*1=720, and as a special case, 0! = 1.
HINT: this is pretty similar to number 2.
Submit ForFactorial.java. [when testing your program, it probably won't give answers larger than 10! or so accurately because they will be larger than Integer.MAX_VALUE.]
4. Create a program named ForDice. This program will ask the user for a number n and then, within a for loop, roll a six sided dice (so generate a random int from 1 to 6) n times. It should then print out the results of those n rolls, telling how many of each number were rolled and what percent that was of the overall results.
Here is an example output:
How many dice should I roll?
>4
[behind the scenes, my program rolls the die four times and counts them. It comes up with one 3, two 5s and one 6.]
Ones: 0 (0)
Twos: 0 (0)
Threes: 1 (.25)
Fours: 0 (0)
Fives: 2 (.5)
Six: 1 (.25)
//note, the numbers tell how many times each number was rolled, the numbers in parentheses tell the fraction of the n overall rolls. 1 out of 4 is .25.
[HINT: before your loop, declare a different variable for each of the six possible outcomes]
Submit ForDice.java
5. Write a program named ForVowels that will take a String in from the user and then print out a String containing only the lowercase vowels (a,e,i,o,u) from the input. It should not output any consonants, spaces or other symbols. Here is an example of how your program should run:
Type in your String:
>>Chris Car$on is a beast!!1!
The vowels are: iaoiaea
To accomplish this goal, you should create a blank String for your answer, then loop through the input string looking at one letter at a time (remember that .substring(i,i+1) will give you the letter in position i) and add the vowels one at a time to your answer. Submit ForVowels.java
6. Write an expanded Max/Min program (ForMaxMin.java) that will prompt the user for a number of entries, and then ask them to type in that many int entries. Your program should then calculate which of these is largest and which of these is smallest. Here is a sample output:
How many numbers to enter?
>>5
Enter your 5 numbers now:
>>3
>>5
>>6
>>7
>>5
The Maximum is 7, minimum is 3.
Make sure your code works for large positive and negative numbers. You will want to set up min and max variables before your loop and update them every time through. Think carefully about how to initialize min and max outside your for loop before you start comparing! Your code should do the math as you go, comparing each number typed in to the previously set values for min and max right after it is entered.
Submit ForMaxMin.java
7. ForWordCount.java - Write a program that will let the user input a sentence, and your program will then calculate the number of spaces in the sentence and use that to figure out the number of words in the sentence.
NOTE: if there are two or more spaces between words, your program should not count that as extra words. For instance, if you type in "My name is Mr. Borish", your program should recognize the 10 spaces in a row, and say that my string has 5 words in it. For a hint for as to how to do work with multiple spaces, remember that substring(i,i+1) will give you the character in position i. If you want the character in the next position (which is spot i+1), use substring(i+1,i+2). This will help you deal with the problem of two adjacent spaces.
You may also assume that the user will not type in a sentence that begins or ends with a space and that their sentence will have at least one word.
Submit ForWordCount.java
(you do not need to do lab 8 this year)
8. ForSarcasm.java - this program will prompt the user to type in a string, and then output that string in the "sarcasm font" with a random mix of upper and lower case letters. To complete this lab, loop through the String and randomly print each individual letter in either lower case or upper case (remember .toUpperCase() and .toLowerCase()). An example of this program might look like this:
Type in a String
> Sarcasm Font
sArCAsM fOnT //or SarCaSM FOnT, it should be random and different each time
9. Create a roulette game (you probably should use a while loop for this instead of a for loop) called ForRoulette.java that allows the player to bet on a Roulette game. In roulette, the wheel generates a random number from 1-36 each spin. The user may type in "odds" or "evens" to try to guess which number will come up, and how much he bets. If he bets "odds" and an odd number comes up (%), he should gain the money he bet, otherwise he should lose that much. If he bets "evens" and an even number comes up he wins, otherwise he loses. The user should start with 100 bucks and may quit at any time by typing in "walk away" at which time your program should tell him his final cash balance.
as an example:
You have $100. what would you like to bet on? Or would you like to "walk away"?
>> odds
how much?
>> 10
It came up as a 7. You win!
You have $110. what would you like to bet on? Or would you like to "walk away"?
>> odds
how much?
>> 20
It came up as 16, you lose!
You have $90. what would you like to bet on? Or would you like to "walk away"?
>> odds
how much?
>> 5
It came up 0, you lose!
You have $85, what would you like to bet on? Or would you like to "walk away"?
>>walk away
You left the casino with 85 bucks, thanks for playing!
NOTE FOR THIS LAB: there is a bug with using sc.nextLine() immediately after sc.nextInt() where the nextLine() seems like it gets skipped. If this happens for you, try putting two sc.nextLine() statements in a row after the sc.nextInt() (the first will be skipped)! Here is a short video I made on this topic: https://www.youtube.com/watch?v=O_f348LoDpw
Submit ForRoulette.java
Extra Credit: for variable amounts of extra credit, you may add more betting features to your roulette game! Feel free to research online for other options to include.