The Question

The children's song Ten in a Bed has a verse whose structure is given by

There were <number> in a bed and the little one said,
Roll over!  Roll over!
So they all rolled over and one fell out;
Then there were <number-1> in a bed.

This verse is repeated starting with some <number>, with each verse having <number> replaced by <number-1>. The last verse is different.

There was 1 in a bed and the little one said,
That's fine!

Write a program called Q1.java that prompts for and reads in an integer n as the initial number of children in the bed. Check to make sure that n is a positive integer and then print out the complete lyrics to the song. Replace <number> and <number-1> in the appropriate verse with the current value of n. (As an added task, in the second last verse, try to say "... there was 1 in a bed." instead of "... there were 1 in a bed.")

The output should conform to that in the sample runs of a correct program shown below. Note that, in these sample runs, the % sign is not part of the program output. User input is shown on the same line as the prompt.

Sample Runs

% java Q1
Enter the starting number of children in the bed ... 4
There were 4 in a bed and the little one said
Roll over!  Roll over!
So they all rolled over and one fell out;
Then there were 3 in a bed.

There were 3 in a bed and the little one said
Roll over!  Roll over!
So they all rolled over and one fell out;
Then there were 2 in a bed.

There were 2 in a bed and the little one said
Roll over!  Roll over!
So they all rolled over and one fell out;
Then there was 1 in a bed.

There was 1 in a bed and the little one said,
That's fine!
%
%
%
% java Q1
Enter the starting number of children in the bed ... 1
There was 1 in a bed and the little one said,
That's fine!
%
%
% java Q1
Enter the starting number of children in the bed ... -2
Exception in thread "main" java.lang.RuntimeException: number of children must be positive
        at type.lib.ToolBox.crash(ToolBox.java:25)
        at Q1.main(Q1.java:15)
%

Useful APIs

Here (again) are the common APIs that you may access:

Java 1.5 API
Type API

Submitting Your Work

Here is the command to submit your work (further details regarding the submit command can also be obtained by typing man submit
Once again, you are encouraged to submit regularly. Newer submissions overwrite older ones.

submit 1020 midtermR7 Q1.java