CS 150 Lab 1
Lab 4 -- Programming With Loops
Principles of Computer Science
Spring, 2005
In this lab you will write two programs from scratch. In each program you will
use David Barnes' SimpleInput class to implement a loop that reads and processes
input until some terminal condition is met. The following jar
file contains everything you need for this lab, along with the two examples
that appear at the end of this document.
Program 1: Write a program that repeatedly asks the user to enter a year; the program prints the number of days in that year. The input loop should terminate when the user supplies 0 as the year.
Here is a typical run of the program:Enter a year; to exit, enter 0
>>> 1952
Year 1952 had 366 days.Enter a year; to exit, enter 0
>>> 2005
Year 2005 had 365 days.Enter a year; to exit,enter 0
>>> 0
Goodbye.
Your program will need to have an input loop that prints prompts and reads input; it terminates when the user enters 0. This will look something like this:
do {
print a prompt
year = keyboard.nextInt();
if (year != 0) {
days = number of days in year
print a message about days
}
} while (year != 0);
To find the number of days in a year you just need to determine whether it is a leap year. Remember the leap year algorithm: a year is a leap year if it is divisible by 4, but not by 100 unless it is also divisible by 400. You want to check these conditions by checking from the most specific to the most general: first try dividing by 400, then 100, then 4. One particularly clear way to structure the program would be to have the input loop in the main() method, a method called isLeapYear() that returns true or false, and a method daysInYear() that takes a year for its argument and returns 366 if the year is a leap year, and returns 365 if the year is not a leap year.
Program 2: Write a program that repeatedly asks the user to enter a number. The program responds with the prime factorization of the number. This continues until the user supplies number 0, at which time the program terminates.
Here is a typical run:
Enter a number; enter 0 to exit
>>> 34
The prime factorization of 34 is: 1 2 17Enter a number; enter 0 to exit
>>> 24
The prime factorization of 24 is: 1 2 2 2 3Enter a number; enter 0 to exit
>>> 23
The prime factorization of 23is: 1 23Enter a number; enter 0 to exit
>>> 0
Goodbye
Here again you will need an input loop in your main() method. This should send each non-zero value it gets to a factorization method. This method won't return anything (we don't know how to work with lists of numbers yet); it just prints the factors. Getting the prime factors of a number n is easier than you might think. You always start by printing 1, with variable factor set to 2. Each time factor divides evenly into n you print the factor and replace n by n/factor. Each time factor does not divide evenly into n you increase factor by 1. This continues until factor > n, at which time you stop. Note that you don't increment factor when it divides evenly into n; this lets a given number be a factor multiple times (such as 2 in the factorization of 8: 1 2 2 2. Before you code this algorithm make sure that you can execute it yourself on paper.
----------------------------------------------------------------------------------------------
Here are two example program that achieve functionality that is similar to the programs you need to write. . Both are contained in the lab's jar file, along with the SimpleInput class.
Example 1. This prompts the user to enter a list of numbers. This is terminated when the user enters a 0, and the average of the list is then printed out.
class Average {
public double avg() {
SimpleInput keyboard = new SimpleInput();
int x;
int sum = 0;
int count = 0;
do {
System.out.print( ">>> " );
x = keyboard.nextInt();
if (x != 0) {
sum = sum + x;
count = count + 1;
}
} while (x != 0);
return (double)sum/count;
}public static void main(String args[]) {
Average a = new Average();
System.out.println( "The average is " + a.avg() );
}}
There are two elements to pay special attention to in this example. The first is the use of the SimpleInput class. To use this class you must have the file SimpleInput.java in the same directory as the rest of your program. An object of the SimpleInput class is a reader for an input stream, which can either be the keyboard (the default constructor) or a file (a different constructor, which we are not going to use). So the line
SimpleInput keyboard = new SimpleInput();
makes variable "keyboard" a reader for keyboard input. Once this is constructed we can use the nextInt() method to read integer values. There are other methods of the SimpleInput class, such as nextDouble(), and nextChar(), but nextInt() is the only one we will use in this lab.
The second element to note about this program is the input loop in method avg(). This has the following basic structure:
do {
print a prompt
x = keyboard.nextInt();
if (x != 0) {
make use of x
}
} while (x != 0);
This is a very common structure for an input loop; it will cycle around getting values for variable x until it finally gets value 0, at which point the loop terminates. By varying what you do with each value of x when you get it you can make this loop perform a wide variety of activities. You can use this for a model for the input loop for both of the programs you will write for this lab.
-----------------------------------------------------------------------------------------
Example 2. This repeatedly prompts the user for a date in the form m d y. The date is reprinted with the numeric value for the month replaced by a string: 2 16 1952 is printed as February 16, 1952,
class Date {
public String monthName(int m) {
if (m == 1)
return "January";
else if (m == 2)
return "February";
else if (m == 3)
return "March";
else if (m == 4)
return "April";
else if (m == 5)
return "May";
else if (m == 6)
return "June";
else if (m == 7)
return "July";
else if (m == 8)
return "August";
else if (m == 9)
return "September";
else if (m == 10)
return "October";
else if (m == 11)
return "November";
else if (m == 12)
return "December";
else
return "Bad Month";
}public static void main(String args[]) {
SimpleInput keyboard = new SimpleInput();
int month, day, year;
Date d = new Date();;do {
System.out.print( ">>> " );
month = keyboard.nextInt();
if (month > 0) {
day = keyboard.nextInt();
year = keyboard.nextInt();
System.out.println( d.monthName(month) + " " + day + ", " + year);
}
} while (month > 0);
}
}
Class Date contains two methods. The main() method has an input loop that reads dates such as "7 4 2005". The first of these numbers represents a month, the second a day, and the third a year. The method sends the month to a new method, method monthName(), which returns a string version of the month name, such as "July". The structure of monthName() is simple, just a sequence of if-then-else statements that decode the integer month. The input loop in main() is very similar to the loop in the first example:
do {
print a prompt;
month = keyboard.nextInt();
if (month > 0) {
read the rest of the date;
get the string version of the month;
print the date in the new format
}
} while (month > 0);