07-Week.docx

advertisement
Week 07 assignment
We will create a program that demonstrates how to use arrays.
The program will have a class name of “Week7”
The main should contain these lines:
int[] myArray = getRandomIntArray(20,200);
displayIntArray(myArray);
int max,min,sum;
double average;
max = myArray[findMax(myArray)];
min = getMin(myArray);
sum = calcSum(myArray);
…
The first thing your program should display is your name. Your main program should display the values
of max, min, sum and average with appropriate labels. Your main program should calculate the average
by dividing the sum by the number. You should use the “.length” to count the members of myArray.
So you are going to need to write several methods for this assignment:
1. getRandomIntArray should have two int arguments. The first will specify the size of the
int array to return. The second will specify the size of the numbers to return. See the
description of nextInt(int n) on p 222 of the book.
The declaration should look like:
public static int[] getRandomArray(int length, int maxvalue){
2. displayIntArray should display the contents of the integer array. You can use console or
GUI I/O for this. Minimum: display the numbers. Full credit: label each of the numbers with its
index.
The declaration should look like:
public static void displayIntArray(int[] array) {
3. findMax should return an int, the location in the array of the largest value. If the largest
value occurs in more than one place, any of its locations is OK.
The declaration should look like:
public static int findMax(int[] array) {
4. getMin should return the value of the smallest number in the array.
The declaration will be very similar to findMax. Note: this returns a value from the array, not an
index within the array.
5. calcSum should return an int that is the sum of all of the numbers in the array.
public static int calcSum(int[] array) {
Download