Assignment 11

Task

Learn how to use loops effectively by practicing with a number of small problems.

Concepts: Loops
Textbook: Chapter 6

Steps

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.

Problem Set #1 (Friday lab)

For each of the following, use a loop to:

  1. Parrot: echo the user's input until the user enters nothing.
  2. Print all the words to the song "99 Bottles of Beer".
    Example:
    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.
  3. Print all the powers of 2 that are less than 5000.
    So: 2, 4, 8, 16, 32, ... (Formatting is up to you.)
  4. Print backwards all the positive even integers starting with 100.
    So: 100, 98, 96, 94.... 2. (Formatting is up to you.)
  5. Print the numbers 1 through 10 on one line, with a comma after each except the last value, which should have a period after it.
    So, the list should look exactly like:
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
    Use a loop so that you can change a single number in your code to print this same list from 1 to 20 or 1 to 80 instead.
  6. Ask the user to enter an integer between 1 and 10. If the user enters a string or a number out of range, keep asking until the user enters valid input, no matter how many times they get it wrong or what sorts of mistakes they make. Then print the entered number. (Challenge: You print the prompt that asks the user to enter a number in only one place in your code.)

Challenge problems:

Problem Set #2 (Wednesday lab)

For each of the following, use one or more loops to:

  1. Print all the positive integers less than 100 that are divisible by 3.
    Once you have that done, change your code to print those numbers 5 per line.

Ask the user to enter a width (integer between 1 and 50). Using that entered value:

  1. Print a line of "width" asterisks (*). That is, if the user enters 3, print 3 asterisks on one line: ***
  2. Print a square box of *s with the given width (and height) That is, if the user enters 3, print:
     ***
     ***
     ***
    
  3. Print a box with a hole in its center. That is, print a box as in #3 but with the single * in the center (or a * close to the center for boxes with an even width) replaced with a space. For example, if the user inputs 5, print:
     *****
     *****
     ** **
     *****
     *****
    
  4. Print an equilateral triangle of *s with the given width. The right angle should be on the lower left. So, given a width of 4:
     *
     **
     ***
     ****
    
  5. Print an equilateral triangle of *s with the given width. The right angle should be on the lower right. So, given a width of 4:
        *
       **
      ***
     ****
    
Challenge:

What to Submit

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.

Grading [3+ points]

1.2 - Uploaded a "signed" file
You must include your name as a "signature".
1.8 - Required problems
0.15 for each of the 12 required problems listed: P1-1, P1-2, P1-3, P1-4, P1-5, P1-6, P2-1, P2-2, P2-3, P2-4, P2-5, P2-6.
+1 - Challenge problems
+0.1 extra credit for each Challenge problem listed: C1-1, C1-2, C1-3, C1-4, C2-1, C2-2, C2-3, C2-4.
+0.2 bonus for completing all 20 problems.

FAQs

How do I tell if the user entered nothing?
Compare the String that they entered to the empty String "". (That's a String with zero characters in it.)
  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.

I'm working on P1-2 (99 bottles of beer), but I can't scroll up high enough in the Command Prompt window to see the start of my program's output.
See the last Tips and Tricks point about increasing your buffer size on the Using the Windows Command Prompt page.
I want to safely ask the user to enter a number. I've got a loop with a try/catch in it, but when the user enters a String, it gives me an infinite loop!
Okay, here's a simple loop that totals all the positive integers the user enters. (This is an integer variation of the 6.3.1 example in your textbook, which does not include try/catch.)
  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.

Solutions
Here are solutions to some of the problems:

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.