-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase2.java
More file actions
142 lines (117 loc) · 2.6 KB
/
case2.java
File metadata and controls
142 lines (117 loc) · 2.6 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Student
{
String name;
double[] grades = new double[10];
int pos = 0;
double avg = 0;
Student(String n)
{
name = n;
}
void addGrade(double grade)
{
if(pos < 10)
{
grades[pos] = grade;
pos++;
}
else
{
System.out.println("Grade limit reached");
}
}
void calcAvg()
{
if(pos == 0)
{
System.out.println("No grades available");
return;
}
avg = 0;
for(int i = 0; i < pos; i++)
{
avg = avg + grades[i];
}
avg = avg / pos;
System.out.println("Average grade: " + avg);
}
void display()
{
System.out.println("Name: " + name);
System.out.println("Grades:");
for(int i = 0; i < pos; i++)
{
System.out.println("Subject " + (i+1) + ": " + grades[i]);
}
if(pos == 0)
{
System.out.println("No grades added");
}
else
{
System.out.println("Average: " + avg);
}
}
}
class ClassMan
{
int studs = 0;
Student[] arr = new Student[100];
void addStudent(Student student)
{
if(studs < 100)
{
arr[studs] = student;
studs++;
}
else
{
System.out.println("Student limit reached");
}
}
void findStudent(String name)
{
for(int i = 0; i < studs; i++)
{
if(arr[i].name.equals(name))
{
System.out.println("Student found");
arr[i].display();
return;
}
}
System.out.println("Student not found");
}
void display()
{
if(studs == 0)
{
System.out.println("No students available");
return;
}
for(int i = 0; i < studs; i++)
{
arr[i].display();
}
}
}
public class case2 {
public static void main(String[] args) {
ClassMan manager = new ClassMan();
Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
s1.addGrade(85);
s1.addGrade(90);
s1.addGrade(80);
s1.calcAvg();
manager.addStudent(s1);
s2.addGrade(70);
s2.addGrade(75);
s2.addGrade(65);
s2.calcAvg();
manager.addStudent(s2);
manager.display();
manager.findStudent("Alice");
manager.findStudent("Charlie");
}
}