CPSC 124 (Winter 1998): Lab 4
Subroutines (Part 2)
NO FANCY GRAPHICS THIS WEEK. In preparation for the test tomorrow, this fourth lab in Computer Science 124. concentrates on basic programming skills, especially subroutines. In this lab, instead of just using subroutines that have been given to you, you will be writing new subroutines and using them in programs.
You'll need the project folder named "Lab 4 Starter" from the cpsc124 folder on the network. Copy it into the java folder on your M drive before beginning the lab. For the second exercise in the lab, you'll need a copy of the "Console Starter Project" folder or some other basic starter project to use as a starting point for writing a new console application.
The exercises at the end of the lab are due in class on the Monday following the lab.
Outline of the Lab:
Some Simple Subs
Open the "Lab 4 Starter" folder and click on the .dsw file to open the project. You'll find a short program that lets the user type in simple commands to do computations with three input numbers. (You can also read a copy on-line here.) For example, if the user types in "sum 4 5.5 6.7", then the computer will print out that "The sum of 4, 5.5, and 6.7 is 16.2". The actual computation and output is done in a subroutine:
static void printSum(double x, double y, double z) { double sum; sum = x + y + z; console.putln( "The sum of " + x + ", " + y + ", and " + z + " is " + sum ); }As a matter of fact, the sum is the only computation that the program can do as it stands. Your assignment is to add other capabilities to the program by implementing the following commands:
- mul: Find and print the product, x*y*z, of the three numbers.
- min: Find and print the minimum, that is the smallest, of the three numbers.
- max: Find and print the maximum, that is the largest, of the three numbers.
- mid: Find and print the middle number among the three.
Write a separate subroutine for each of the operations, and call those subroutines from the main() routine. Note that the last three operations require some fancy work with if statements.
A Complete Program
For the second exercise in this lab, you will write a subroutine that returns a value, and you will use it in a complete program.
If N is a positive integer, then a divisor of N is an integer, D, in the range 1 <= D <= N, which evenly divides N. In class, we looked at prime numbers, which are numbers that have the minimum possible number of divisors. What if you look for a number with a lot of divisors Which number between 1 and 10,000 has the largest number of divisors? How many divisors does it have? Are there several numbers that have the same number of divisors? Your assignment is to write a program that finds out.
You should first write a subroutine to count the number of divisors of a given integer N. N will be a parameter to the subroutine, and the answer should be sent back as the return value of the subroutine. So the interface of the subroutine could be:
static int numberOfDivisors(int N)
Go through all the numbers from 1 to 10000, and get the number of divisors for each one. Keep track of the largest number of divisors observed. At the end of the process, print out the answer.
Now, you will have to make a second pass through the numbers, N, between 1 and 10000, to print out the values of N that give the maximum number of divisors.
Note: You'll be more likely to get this program right in a reasonable amount of time if you attack the problem one step at a time:
- First, write the numberOfDivisors subroutine, and make sure it is working. You can do this by temporarily including an output statement to print out all the divisors of N, not just count them. Test the subroutine with a short main program that simply calls the subroutine for some small value of N, say N = 60. Make sure it gives the correct result.
- Write the program at first to look at numbers between 1 and 10, instead of between 1 and 10000. Have it print out the number of divisors for each number, rather than just keep track of the maximum. Make sure it is working properly.
- After taking out all the extra output statements, try your program for maximum values of 100 and 1000. Does the result seem to make sense? Then do the actual run for all numbers between 1 and 10000. It will take a noticeable amount of time for the program to process so many numbers.
Exercises to Turn in
Exercise 1. Turn in a print-out of your three-number calculator program, as described above. Make sure that it follows all the rules of good programming style, including the rule that every subroutine must have a comment that describes its "contract."
Exercise 2. Turn in a print-out of your divisor counting program, as described above. Make sure it follows all the rules of good programming style. Also turn in a statement of the results of the program: What is the largest number of divisors for any number between 1 and 10,000? What number or numbers produce the maximum value?
Exercise 3. "Subroutines are components of programs that can be developed, written, and tested separately." Write a short essay discussing what this means and why it is important. How does it help make writing correct programs easier? What examples of this have you seen in this lab (or elsewhere)?