## CPSC 124, Winter 1998

Sample Answers to Lab 4

This page contains sample answers to the exercises from Lab #4 in

CPSC 124: Introductory Programming,Winter 1998. See the information page for that course for more information.

Exercise 1:The problem was to add a number of subroutines, and a few other changes, to an existing program. A sample completed program is:public class ThreeNumberCalculator { // The program allows the user to enter a command such as "sum" // followed by three numbers. It then applies the command to // the three numbers and outputs the result. This is repeated // until the user enters "end". // // Supported commands are: sum, mul, max, min, and mid. // // by David Eck static Console console; // Console window for input/output public static void main(String[] args) { console = new Console("Three Number Calculator"); console.putln("Welcome to a simple calculator program that can apply"); console.putln("Several basic operations to three input numbers."); console.putln("This program understands commands consisting of a word"); console.putln("followed by three numbers. For example: sum 3.5 -6 4.87"); console.putln("Commands include:"); console.putln(" sum -- find the sum of three numbers"); console.putln(" prod -- find the product number"); console.putln(" min -- find the smallest number"); console.putln(" max -- find the largest number"); console.putln(" mid -- find the middle number"); console.putln(" end -- quit the program"); while (true) { console.putln(); console.put("COMMAND>> "); String command = console.getWord(); // get command word entered by user if (command.equalsIgnoreCase("end")) break; double firstNum = console.getDouble(); // get three numbers entered by user double secondNum = console.getDouble(); double thirdNum = console.getDouble(); console.getln(); // read the end-of-line if (command.equalsIgnoreCase("sum")) { // do "sum" command printSum(firstNum, secondNum, thirdNum); } else if (command.equalsIgnoreCase("prod")) { // do "prod" command printProduct(firstNum, secondNum, thirdNum); } else if (command.equalsIgnoreCase("min")) { // do "min" command printMin(firstNum, secondNum, thirdNum); } else if (command.equalsIgnoreCase("max")) { // do "max" command printMax(firstNum, secondNum, thirdNum); } else if (command.equalsIgnoreCase("mid")) { // do "mid" command printMiddle(firstNum, secondNum, thirdNum); } else { console.putln("Sorry, I can't understand \"" + command + "\"."); } } // end of while loop console.putln(); console.putln("Bye!"); console.close(); } // end of main() static void printSum(double x, double y, double z) { // This method computes the sum of its three parameters, // x, y, and z. It outputs the answer to the console. double sum; // The sum of the three parameters. sum = x + y + z; console.putln("The sum of " + x + ", " + y + ", and " + z + " is " + sum); } // end of printSum() static void printProduct(double x, double y, double z) { // This method computes the product of its three parameters, // x, y, and z. It outputs the answer to the console. double prod; // The product of the three parameters. prod = x * y * z; console.putln("The product of " + x + ", " + y + ", and " + z + " is " + prod); } // end of printProduct() static void printMax(double x, double y, double z) { // This method finds the largest of its three parameters, // x, y, and z. It outputs the answer to the console. double max; // The largest of the three parameters. max = x; if (y > max) max = y; if (z > max) max = z; console.putln("The maximum of " + x + ", " + y + ", and " + z + " is " + max); } // end of printMax() static void printMin(double x, double y, double z) { // This method finds the smallest of its three parameters, // x, y, and z. It outputs the answer to the console. double min; // The smallest of the three parameters. min = x; if (y < min) min = y; if (z < min) min = z; console.putln("The minimum of " + x + ", " + y + ", and " + z + " is " + min); } // end of printMin() static void printMiddle(double x, double y, double z) { // This method finds the median of its three parameters, // x, y, and z. It outputs the answer to the console. double mid; // The median of the three parameters. if ( (x <= y && y <= z) || (x >= y && y >= z) ) mid = y; else if ( (y <= x && x <= z) || (y >= x && x >= z) ) mid = x; else mid = z; console.putln("The median of " + x + ", " + y + ", and " + z + " is " + mid); } // end of printMiddle() } // end of class ThreeNumberCalculator

Exercise 2:The exercise was to write a program to find the maximum number of divisors for any number between 1 and 10000, and to print outallthe numbers that have that maximum number of divisors. The answer is that the number of divisors is 64. The numbers in the range 1 to 10000 that have 64 divisors are 7560 and 9240. Here is a sample solution:public class ConsoleApplication { // This program will count the number of divisors for each integer // between 1 and MAX (where MAX is the constant given below). It will // find the maximum number of divisors. It will then print out the // maximum and all the numbers, N, between 1 and MAX that have that // maximum number of divisors. // // by David Eck static final int MAX = 10000; // upper limit on numbers for which // divisors will be counted static Console console = new Console(); // window for I/O public static void main(String[] args) { int maxDivisors = 1; // maximum number of divisors seen for (int N = 2; N < MAX; N++) { // check number of divisors of N int divisors = numberOfDivisors(N); if (divisors > maxDivisors) maxDivisors = divisors; } // at this point, maxDivisors is the maximum number of divisors for // any number between 1 and MAX. console.putln("For numbers between 1 and " + MAX + ","); console.putln("the maximum number of divisors is " + maxDivisors); console.putln("It occurs for:"); // Now go through all the numbers, N, between 1 and MAX, and output // N if the number of divisors of N is equal to the maximum number for (int N = 2; N < MAX; N++) { int d = numberOfDivisors(N); if (d == maxDivisors) { console.putln(" N = " + N); } } console.close(); } // end main() static int numberOfDivisors(int N) { // This routine counts the number of integers of integers, D, between // 1 and N such that D evenly divides N. int ct = 0; // The number of divisors found. for (int D = 1; D <= N; D++) if (N % D == 0) ct++; return ct; } // end numberOfDivisors() } // end class ConsoleApplication

Exercise 3:"Subroutines are components of programs that can be developed, written, and tested separately."Because a subroutine is a black box, it interacts with the rest of the program only though its "interface," that is, its name and parameter list. The inside of the subroutine, its "implementation," is dependent of the main program. The subroutine has a certain task to perform. As long as it accomplishes that task correctly, the particulars of how it is written don't really matter.

This means that the problem of writing the main program is separate from the problem of writing the subroutines used by the main program. And each subroutine is its own, independent problem. The overall design of the main program tells you what the subroutine has to do. You can write a subroutine to perform that test, and test the subroutine by calling it from a very simple main program whose only purpose is to test the subroutine. Once the subroutine is written, it can be incorporated into the rest of the program.

The importance of this is that in place of one large, complex problem (writing the whole program), you have a number of smaller, less complex problems that can be tackled independently. For example, if you try to test an entire program, it might be hard to pin down exactly what part of the program is responsible for any error that occurs. If you test a small subroutine independently, it should be easier to figure out the cause of any error.

David Eck, 10 February 1998