In: Computer Science
please write the java code so it can run on jGRASP Thanks! 1 /** 2 * PassArray 3 * @Sherri Vaseashta 4 * @Version 1 5 * @see 6 */ 7 import java.util.Scanner; 8 9 /**
10 This program demonstrates passing an array
11 as an argument to a method
12 */13
14 public class PassArray 15 { 16 public static void main(String[] args) 17 { 18
19 final int ARRAY_SIZE = 4; //Size of the array
20 // Create an array.
21 int[] numbers = new int[ARRAY_SIZE];
22
23 // Pass the array to the getValues method
24 getValues(numbers);
25 26 System.out.println("Here are the numbers that you entered:"); 27 28 29 30 31 32 33 34 35 36 /**
37 The getValues method accepts a reference
38 to an array as its argument. The user is
39 asked to enter a value for each element.
40 @param array A reference to the array.
41 */ 42 43 private static void getValues(int[] array) 44 {
45 //Create a Scanner object for keyboard input
46 Scanner keyboard = new Scanner(System.in);
47
// Pass the array to the showArray method
showArray(numbers);
// Pass the array to the calcAverage method
calcAverage(numbers); }//end of main
48 System.out.println("Enter a series of " + array.length + " numbers:"); 49
50 //Read the values into the array
51 for (int index = 0; index < array.length; index++)
52 { 53 System.out.println("Enter number " + (index + 1) + ":");
54 array[index] = keyboard.nextInt(); 55 } 56 }//end of getValues() method 57
58 /**
59 The showArray method accepts an array as
60 an argument and displays its contents.
61 @param array A reference to the array
62 */ 63 64 public static void showArray(int[] array) 65 {
66 //Display the array elements.
67 for (int index = 0; index < array.length; index++)
68 { 69 System.out.println(array[index] + " "); 70 } 71 72 } //end of showArray() method 73 74 /**
75 The calcAverage method accepts an array as
76 an argument and calculates the sum of its
77 elements and then calculates the average of
78 its elements
79 @param array A reference to the array
80 */ 81 82 public static void calcAverage(int[] array) 83 {
84 int sum = 0;
85 double average = 0;
86 //Sum the array elements.
87 for (int index = 0; index < array.length; index++)
88 { 89 sum = sum + array[index]; 90 } 91 average = (double)sum / array.length; 92 93 System.out.println("Sum: " + sum);
94 System.out.println("Average: " + average); 95 }//end of calcAverage() method 96 97 }//end of class
98
CODE 2
1 /** 2 * TemperaturesMultidimensionalArrays 3 * @Sherri Vaseashta 4 * @Version1 5 * @see 6 */ 7 8 import java.util.Scanner; 9 public class TemperaturesMultidimensionalArrays
10 { 11 public static void main(String[] args) 12 { 13
14 Scanner keyboard = new Scanner(System.in);
15 int numDays = 0;
16 String[] dates;
17 double[][] temperatures;
18 int row = 0;
19 int col = 0;
20 21 System.out.println("This is the Temperature Stats Program."); 22 System.out.println("It allows you to analyze temperatures over a period of time."); 23 System.out.println("How many days do you want to track temperatures?");
24 numDays = keyboard.nextInt();
25 keyboard.nextLine(); //needed any time you input a
String or char after a double|int 26
27 dates = new String[numDays];
28 temperatures = new double[numDays][3];
29
30 //load (read) the data into the arrays
31 for (row = 0; row < numDays; row++)
32 { 33 System.out.println("Please enter the date: (ex. Oct. 12)");
34 dates[row] = keyboard.nextLine();
35 System.out.println("Please enter the low for " +
dates[row]);
36 temperatures[row][0] = keyboard.nextDouble();
37 System.out.println("Please enter the high for " +
dates[row]);
38 temperatures[row][1] = keyboard.nextDouble();
39 temperatures[row][2] = (temperatures[row][0] +
temperatures[row][1]) / 2; 40 keyboard.nextLine();
41 } 42 43 //print the contents of the arrays to see if the data is there. This is okay for a few columns 44 System.out.printf("%-20s%20s%20s%20s\n", "Dates", "Lows", "Highs", "Average Temps"); 45 for (row = 0; row < numDays; row++) 46 { 47 System.out.printf("%-20s%20.1f%20.1f%20.1f\n", dates[row], temperatures[row][0], temperatures[row][1], temperatures[row][2]); 48 } 49 50 //print the contents of the arrays to see if the data is there. This is how to do it at WORK. 51 //You have to use nested loops when you have more than a few columns 52 System.out.printf("\n\n%-20s%20s%20s%20s\n", "Dates", "Lows", "Highs", "Average Temps"); 53 for (row = 0; row < numDays; row++) 54 {
55 System.out.printf("%-20s", dates[row]);
56 for (col = 0; col < 3; col++)
57 { 58 System.out.printf("%20.1f", temperatures[row][col]); 59 } 60 System.out.println(); 61 } 62 63 64 65 }//end of main 66 }// end of class
Below is the solution:
public class PassArray {
public static void main(String[] args) {
final int ARRAY_SIZE = 4; // Size
of the array
// Create an array.
int[] numbers = new
int[ARRAY_SIZE];
// Pass the array to the getValues
method
getValues(numbers);
System.out.println("Here are the
numbers that youentered:");
// Pass the array to the showArray
method
showArray(numbers);
// Pass the array to the
calcAverage method
calcAverage(numbers);
} //end of main
private static void getValues(int[] array) {
// Create a Scanner object for
keyboard input
Scanner keyboard = new
Scanner(System.in);
System.out.println("Enter a series
of " + array.length + " numbers:");
// Read the values into the
array
for (int index = 0; index <
array.length; index++) {
System.out.println("Enter number " + (index + 1) + ":");
array[index] =
keyboard.nextInt();
}
}
// end of getValues() method
public static void showArray(int[] array) {
// Display the array
elements.
for (int index = 0; index <
array.length; index++) {
System.out.println(array[index] + " ");
}
} // end of showArray() method
/*
* 75 The calcAverage method accepts an array as 76 an
argument and calculates
* the sum of its 77 elements and then calculates the
average of 78 its elements
* 79 @param array A reference to the array
*/
public static void calcAverage(int[] array) {
int sum = 0;
double average = 0;
// Sum the array elements.
for (int index = 0; index <
array.length; index++) {
sum = sum +
array[index];
}
average = (double) sum /
array.length;
System.out.println("Sum: " +
sum);
System.out.println("Average: " +
average);
}// end of calcAverage() method
}// end of class
Sample output:
Enter a series of 4 numbers:
Enter number 1:
3452
Enter number 2:
4564
Enter number 3:
8756
Enter number 4:
3232
Here are the numbers that youentered:
3452
4564
8756
3232
Sum: 20004
Average: 5001.0
import java.util.Scanner;
public class TemperaturesMultidimensionalArrays
{
public static void main(String[] args) {
Scanner keyboard = new
Scanner(System.in);
int numDays = 0;
String[] dates;
double[][] temperatures;
int row = 0;
int col = 0;
System.out.println("This is the
Temperature StatsProgram.");
System.out.println("It allows you
to analyze temperatures over a period of time.");
System.out.println("How many days
do you want to track temperatures?");
numDays = keyboard.nextInt();
keyboard.nextLine(); // needed any
time you input a String or char after a double|int
dates = new String[numDays];
temperatures = new
double[numDays][3];
// load (read) the data into the
arrays
for (row = 0; row < numDays;
row++) {
System.out.println("Please enter the date: (ex. Oct.12)");
dates[row] =
keyboard.nextLine();
System.out.println("Please enter the low for " + dates[row]);
temperatures[row][0] = keyboard.nextDouble();
System.out.println("Please enter the high for " +
dates[row]);
temperatures[row][1] = keyboard.nextDouble();
temperatures[row][2] = (temperatures[row][0] +
temperatures[row][1]) / 2;
keyboard.nextLine();
}
// print the contents of the
arrays to see if the data is there. This is okay
// for a few columns
System.out.printf("%-20s%20s%20s%20s\n", "Dates", "Lows", "Highs",
"Average Temps");
for (row = 0; row < numDays;
row++) {
System.out.printf("%-20s%20.1f%20.1f%20.1f\n", dates[row],
temperatures[row][0], temperatures[row][1],
temperatures[row][2]);
}
// print the contents of the
arrays to see if the data is there. This is how to
// do it at WORK.
// You have to use nested loops
when you have more than a few columns
System.out.printf("\n\n%-20s%20s%20s%20s\n", "Dates", "Lows",
"Highs", "Average Temps");
for (row = 0; row < numDays;
row++) {
System.out.printf("%-20s", dates[row]);
for (col = 0;
col < 3; col++) {
System.out.printf("%20.1f",
temperatures[row][col]);
}
System.out.println();
}
}// end of main
}// end of class
Sample output:
This is the Temperature StatsProgram.
It allows you to analyze temperatures over a period of time.
How many days do you want to track temperatures?
3
Please enter the date: (ex. Oct.12)
oct.12
Please enter the low for oct.12
12
Please enter the high for oct.12
60
Please enter the date: (ex. Oct.12)
Jan.10
Please enter the low for Jan.10
10
Please enter the high for Jan.10
40
Please enter the date: (ex. Oct.12)
Feb.14
Please enter the low for Feb.14
19
Please enter the high for Feb.14
60
Dates
Lows
Highs Average Temps
oct.12
12.0
60.0
36.0
Jan.10
10.0
40.0
25.0
Feb.14
19.0
60.0
39.5
Dates
Lows
Highs Average Temps
oct.12
12.0
60.0
36.0
Jan.10
10.0
40.0
25.0
Feb.14
19.0
60.0
39.5