Assigned: Tuesday, March 3, 1998
Due: Thursday, March 12, 1998
Purpose: In this assignment, you will attempt to develop more complex programs that take advantage of Java's arrays. Hopefully, you will also gain a better understanding of for loops.
Warning: At times, Java is incredibly verbose. At other times, it is confusingly terse. Why do we do things the way we do? Because the designers decided that we whould. (I.e., "it's just the way things are").
Computations.javaUseComputations.javaRecall that there are three basic steps to building a Java program (assuming that SamR has helped you make the change that allows this shorthand).
jc Program.java
ji Program
Of course, when creating a program from scratch, you should begin by sketching the design of the program: what it does and how it does it.
Here are a template for a simple class that computes various aspects of a set of numbers and a program that uses that class. Note that not all of the methods in the first class are correctly defined.
Make a copy of the
first program,
save it as Computations.java,
and compile it. Make a copy of the
second program, save it is
UseComputations.java, compile it, and execute it.
Here are the commands you'll use
% jc Computations.java % jc UseComputations.java % ji UseComputations.java
See what happens when you change the values in the array. You might add values, delete values, or use different values. Note that after changing the values, you will need to recompile and reexecute the program.
This program is missing definitions for a number of methods, including
maximum, minimum, and average.
Fill in the instructions for those methods. It is likely that the
instructions will bear some similarity to those I used for
sum.
For extra credit, extend
UseComputations.java
to read in the array, rather than predefining it.
Email me your version of the program, using a sequence of commands like
% elm rebelsky < Computations.java
% elm rebelsky < UseComputations.java
Computations.java
/**
* Perform a series of computations (sum, minimum, maximum, average)
* on an array of numbers.
*
* @author Samuel A. Rebelsky
* @author YOUR NAME HERE
* @version 1.1 of March 1998
*/
public class Computations
{
// +---------+-----------------------------------------------------------
// | Methods |
// +---------+
/**
* Compute the average value in an array of numbers.
*/
public double average(double[] stuff)
{
// The instructions for this method need to be filled in
return 0;
} // average(double[])
/**
* Compute the maximum value in an array of numbers.
*/
public double maximum(double[] stuff)
{
// The instructions for this method need to be filled in
return 0;
} // maximum(double[])
/**
* Compute the minimum value in an array of numbers.
*/
public double minimum(double[] stuff)
{
// The instructions for this method need to be filled in
return 0;
} // minimum(double[])
/**
* Compute the sum of values in an array of numbers.
*/
public double sum(double[] stuff)
{
// The total of the values. Initially 0, since we haven't
// seen any values.
double total = 0;
// A counter to step through the array.
int i;
// Step through the array, updating the total as we go.
for(i = 0; i < stuff.length; i = i + 1) {
total = total + stuff[i];
}
// That's it, we're done.
return total;
} // sum(double[])
} // Computations
UseComputations.java
import rebelsky.io.SimpleOutput; // We'll be printing results
import Computations; // We'll be computing
/**
* Perform a series of computations (sum, minimum, maximum, average)
* on an array of numbers. To change the array of numbers, you'll need
* to change the code for this program and recompile.
*
* @author Samuel A. Rebelsky
* @author YOUR NAME HERE
* @version 1.1 of March 1998
*/
public class UseComputations
{
// +------+--------------------------------------------------------------
// | Main |
// +------+
public static void main(String[] args)
{
// Prepare for writing output
SimpleOutput out = new SimpleOutput();
// Create an array to compute with
double[] values = { 3.2, 4.4, 5, 2, 1, 9, 0, -4 };
// Create an object of this type so that we can do some
// computations.
Computations comp = new Computations();
// Print the array.
out.print("Working with the array: ");
out.println(values);
// Print the various computed values
out.print("The average of those values is: ");
out.println(comp.average(values));
out.print("The largest of those values is: ");
out.println(comp.maximum(values));
out.print("The smallest of those values is: ");
out.println(comp.minimum(values));
out.print("The sum of those values is: ");
out.println(comp.sum(values));
// That's it. We're done.
System.exit(0);
} // main
} // UseComputations
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Tue Mar 3 13:10:05 1998.
This page generated on Thu Apr 2 13:50:04 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu