Assignment 3: Loops, I did it again!

Due Thursday, September 29, before midnight

The goals of this assignment are:

  • Work with loops

  • Work with if statements

  • Work with accumulator variables

1. Banner

Write a program,, that draws an ascii border around a phrase given by the user.

Two examples of the running program are shown below. User input is shown in bold.

$ java Banner
Enter a phrase: Welcome, Friends!
Enter a decorator character: +
+ Welcome, Friends! +

$ java Banner
Enter a phrase: Applesauce!
Enter a decorator character: $
$ Applesauce! $


  • Your border should fit around the entered phrase as in the examples.

  • You can read both the phrase and decorator character as Strings

  • You can assume the user will enter a single character as a decorator

2. Checkerboard

Write a program,, that asks the user for a size and uses a nested loop to print a two-dimensional size-by-size checkerboard pattern consisting of dashes and o’s.

$ javac
$ java Checkerboard
Enter a size: 11


Write a program,, that computes the checksum and prints the ISBN number for a given 9-digit number.

The ISBN number is 10-digit code that uniquely specifies a book. For example, the ISBN number for Harry Potter and the Socerer’s Stone is java ISBN 0747532745

The right-most digit is a checksum digit that can be computed using the other 9 digits. Suppose we have an ISBN number, defined as

\[d_{10} d_9 d_8 d_7 d_6 d_5 d_4 d_3 d_2 d_1\]

where each \(d_i\) is a digit with value between 0 and 9. For example, for Harry Potter's ISBN number, \(d_{10}\) is 0; \(d_9\) is 7; \(d_8\) is 4; etc.

The ISBN number has the property that the following expression must be a multiple of 11.

\[10d_{10} + 9d_9 + 8d_8 + 7d_7 + 6d_6 + 5d_5 + 4d_4 + 3d_3 + 2d_2 + 1d_1\]

Therefore, to find the checksum you should test each digit, from 0 to 9, to find a value for \(d_1\) that results in a number that is a multiple of 11. The value for \(d_1\) is called the checksum digit.

Below are examples of running this program. User input is in bold.

$ java ISBN 817525766
The checksum digit is 0
The full ISBN number is 8175257660

$ java ISBN 074753274
The checksum digit is 5
The full ISBN number is java ISBN 0747532745


  • Your program must use command line arguments!

  • Use % to determine whether the ISBN formula is a multiple of 11

  • You can assume that the input with be a 9 digit number

  • You should print the checksum along with the 10 digit ISBN number.


The input is the first 9 digits of the ISBN number. An easy way to parse this is to go through each character of the input string and convert each individually to a digit. One way to do this is use the fact that characters correspond to numbers "under the hood" according to the ASCII Table. If we subtract the character '0', we get the offset of the digit from zero. See below for an example. Alternatively, you can also use the function Character.getNumericValue(c);.
String example = "123456789";
for (int i = 0; i < example.length(); i++) {
    char c = example.charAt(i);
    int digit = c - '0';

4. Poll Predictor

Write a program,, which predicts a candidate’s likelihood of victory given its current polling number and margin of error. For example, if a poll predicts that a candidate has a 47% chance of winning a state with a 6% margin of error, can we make an overall prediction that the candidate will win a state?

One way to do this is to simulate an actual vote incorporating the margin of error. Let’s say, a poll (POLL) in a state shows that a candidate is likely to receive 47% of the vote in their favor with a margin of error +/- 6% (MOE). With this information, we can do a simulation of the state’s voting, and arrive at the % of votes received by the candidate in a poll:

SamplePoll = POLL + X * MOE

Where X is a number in the range [-1.0..1.0]. For example, if X is -0.5, then the candidate will receive 0.47 + -0.5 * 0.06 = 0.44. This means, in this instance, the candidate received only 44% of the votes (i.e. losing the state). Alternately, if X turns out to be 0.8 then the candidate will receive 0.47
0.8 * 0.06 = 0.518 or 51.8% of the votes cast thereby winning the state.

We can use Java’s Math.random() function to generate values of X in the range [-1.0..1.0) and perform a simulation of several sample elections based on POLL and MOE to see if a candidate is likely to win or lose a state. For example,

$ java-introcs Predictor Keystone 0.47 0.06 1000000
Candidate win likelihood for Keystone state is 25.02%

The above means that after performing 1 million randomized simulated polls based on POLL and MOE the candidate has a 25% likelihood of winning Keystone state. Here are some more examples.

$ java-introcs Predictor Keystone 0.53 0.06 1000000
Candidate win likelihood for Keystone state is 74.99%

$ java-introcs Predictor Golden 0.49 0.07 1000000
Candidate win likelihood for Golden state is 42.82%

$ java-introcs Predictor Nutmeg 0.51 0.02 1000000
Candidate win likelihood for Nutmeg state is 75.01%

You program,, should take the following command line arguments:

  • state: (String) state name. For example, Keystone, Golden, or Nutmeg

  • POLL: (double) polling number, a double in the range [0,1]. For example, 0.53, 0.49, or 0.51

  • MOE: (double) margin of error, a double in range [0,1]. For example, 0.06, 0.07, or 0.02

  • N: (int) number of simulation iterations. For example, 1000000

Your program,, should implement the following algorithm

wins = 0
Repeat N times
   Compute SamplePoll = POLL + randomNumber * MOE
   If SamplePoll > 0.5 then
       Increment wins by 1
   Compute and output overall win likelihood = (wins/N)
Table 1. Poll data



Margin of Error
















In your writeup, include the likelihoods computed by your program for all states, using N = 1000000.

5. What to hand-in

  1. The programs,,,, and

  2. Make sure each program has a header containing your name, date, and purpose of the program

  3. A brief write-up containing your name, assignment number, and a few sentences about how long you spent on the assignment and any interesting bugs you solved. Don’t forget to include the likelihoods computed by your Predictor!

5.1. How to hand-in

  1. Copy your programs and writeup to your dropbox, into the folder called A03.