-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2DActivity.java
More file actions
96 lines (75 loc) · 2.48 KB
/
Array2DActivity.java
File metadata and controls
96 lines (75 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//Program Name: Array2DActivity.java
//Author: Patricia Baker and Josh Decker
//Date: 3/11/2022
//Description: Exam scores are entered into a 2d array. The total exam scores are calculated, the average is calculated, and the total number of exam scores
// above 70.0 is shown.
package Ch7;
import java.util.Random;
import java.text.DecimalFormat;
public class Array2DActivity
{
public static void main(String[] args)
{
double average = 0.0;
int countValues = 0;
double sum = 0.0;
Random gen = new Random();
DecimalFormat dFmt = new DecimalFormat("0.0");
// declare an array called myExams that holds 3 exams for 10 students
// each row represents a student
double[][] myExams = new double[10][3];
System.out.println("2-D Array Activity");
System.out.println("------------------\n");
// load myExams array with the random numbers 60 - 100 to represent exam scores
for (int i = 0; i < myExams.length; i++) {
for (int j = 0; j <myExams[i].length; j++) {
myExams[i][j] = gen.nextDouble() * (100-60) + 60;
}
}
//display all the components of the array
System.out.println("Display the array\n");
for (int i = 0; i < myExams.length; i++) {
for (int j = 0; j <myExams[i].length; j++) {
System.out.print(dFmt.format(myExams[i][j]) + "\t");
}
System.out.println();
}
//add up all the element values in the array and display the sum
for (int i = 0; i < myExams.length; i++) {
for (int j = 0; j <myExams[i].length; j++) {
sum = sum + myExams[i][j];
}
}
System.out.println("The sum of the array: " + dFmt.format(sum));
//calculate and display the average
average = sum / (myExams.length * myExams[0].length);
System.out.println("The average exam score is: " + dFmt.format(average));
//count and display how many exam scores are >= 70.0
for (int i = 0; i < myExams.length; i++) {
for (int j = 0; j < myExams[i].length; j++) {
if (myExams[i][j] >= 70.0) {
countValues++;
}
}
}
System.out.println("Number of exams that are a C (70.0) or better: " + countValues);
}
}
/*
2-D Array Activity
------------------
Display the array
72.0 77.1 62.0
81.1 99.1 92.5
93.5 89.2 78.0
61.8 94.7 78.5
64.6 94.8 78.4
94.4 93.6 99.0
77.7 98.8 86.8
96.5 70.2 99.1
74.6 92.2 77.0
78.3 92.5 80.1
The sum of the array: 2528.0
The average exam score is: 84.3
Number of exams that are a C (70.0) or better: 27
*/