Learn how to use loops effectively by practicing with a number of small problems.
Concepts: Loops
Textbook: Chapter 6
There are two problem sets. The Challenge problems are optional. We will start work on each problem set in lab.
You will not be submitting your code for this assignment. You can write all your code in one main method if you want to, /* commenting out */ each solution once you have it.
You may work with a friend or in a group for this assignment, but only so long as you understand every solution well enough to reproduce it on your own.
For each of the following, use a loop to:
99 bottles of beer on the wall, 99 bottles of beer! Take one down, pass it around, 98 bottles of beer on the wall! 98 bottles of beer on the wall, 98 bottles of beer! Take one down, pass it around, 97 bottles of beer on the wall!Continue until you hit 0 bottles left.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
int letter = 'a'; letter++;
)
switch
)
For each of the following, use one or more loops to:
Ask the user to enter a width (integer between 1 and 50). Using that entered value:
*** *** ***
***** ***** ** ** ***** *****
* ** *** ****
* ** *** ****
**** *** ** *
**** *** ** *
****** ****** ** ** ** ** ****** ******
0 1 2 3 4 .. 11 12 1 1 2 3 4 11 12 2 2 4 6 8 22 24 3 3 6 9 12 33 36 . . . . . . 11 11 22 33 44 .. 121 132 12 12 24 36 48 .. 132 144That is, each entry in the table takes up 3 characters and is right-aligned, with a space between entries on the same line.
Rather than turning in your code, you will just report which problems you solved. Copy and paste the following into a plain-text file named UsernameA11.txt
:
I hereby honestly affirm that I have found solutions to the following problems from the two A11 problem sets: P1-1 P1-2 C1-1 [Edit this list to include only those problems that you completed] P2-1 P2-3 C2-1 I also affirm that I understand each solution that I found well enough that I could reproduce it again on my own if necessary. Name: [your name here] Date: [current date here]
Upload your complete UsernameA11.txt
file to Tamarin.
if (input.equals("")) {Or see if the length of the string is 0:
if (input.length() == 0) {
In this class, please do not use String
's isEmpty()
method. It is not available in Java 5, so your code won't compile on Tamarin.
Scanner keybd = new Scanner(System.in); int sum = 0; int input = 1; while (input > 0) { try { System.out.print("Enter an integer (or 0 to quit): "); input = keybd.nextInt(); if (input > 0) { sum += input; } }catch (InputMismatchException e) { System.out.println("That was not an integer. Try again."); keybd.nextLine(); //clear the input stream } } System.out.println("Total: " + sum);
The important line here is the clearing of the input stream. Here's why:
When you call nextInt()
, it reads the next token (word) that the user typed and tries to convert it to an int
. Imagine the user typed in "three" instead of 3. In this case, "three" cannot be converted to an int
, so nextInt()
throws an exception. However, it also leaves the "three" in the input stream! So, although you dealt with the exception, "three" is still in the Scanner. So, after you print your error message, the while loop returns you to asking the user for input. But this just tries to read in the "three" again, causing another exception.
Calling nextLine()
in the catch block breaks this cycle because it reads everything out of the input stream. Then, when the loop repeats, nextInt()
will once again wait for the user to type in fresh data.
Make sure you try each problem on your own before looking at the solutions. If you were unable to produce your own solution before looking at the solution, try putting the solution away after understanding it and writing your own solution. Remember that you need to be able to produce your own solutions to claim credit for each problem.
To practice reading code as well as writing it, I recommend you look through the solutions even if you produced your own solutions without trouble.