-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode 54.txt
79 lines (51 loc) · 1.91 KB
/
code 54.txt
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
package usingarrays25;
import javax.swing.JOptionPane;
public class UsingArrays25{
public static void main(String[] args){
// we are going to create an array of objects (of Students)
// load values from the user - including a mark for a test
// we are then going to work out the average mark
//String[] strNames = new String[5];
//int[] intTest1 = new int[5];
// inside 1 student - there is a name and a mark
Student[] arrStud = new Student[5];
//Student stud1 = new Student();
int intCount;
for (intCount = 0; intCount <= 4; intCount++){
arrStud[intCount] = new Student();
arrStud[intCount].setName(JOptionPane.showInputDialog(null, "Enter name " + (intCount + 1)));
arrStud[intCount].setT1Mark(Integer.parseInt(JOptionPane.showInputDialog(null, "Enter mark for " + arrStud [intCount].getName))));
}
double dblSum, dblAvg;
dblSum = 0;
for (intCount = 0; intCount <= 4; intCount++){
dblSum = dblSum + arrStud[intCount].getT1Mark();
}
dblAvg = dblSum / 5;
JOptionPane.showMessageDialog(null, "The average mark is " + dblAvg);
String strAns = "";
for (intCount = 0; intCount <= 4; intCount++){
if (arrStud[intCount].getT1Mark() > dblAvg)
strAns = strAns + arrStud[intCount].getName() + "\n";
}
JOptionPane.showMessageDialog(null, "The students who got above the average of " + dblAvg + "is \n" + strAns);
}
}
//----------------------------------------------------------------------------------
package usingarrays25;
public class Student{
private String strName;
private int intT1Mark;
public void setName(String strNewName){
strName = strNewName;
}
public String getName(){
return strName;
}
public void setT1Mark(int intNewT1Mark){
intT1Mark = intNewT1Mark;
}
public int getT1Mark(){
return intT1Mark;
}
}