-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPD4NamithGStudentArrayTester.java
More file actions
191 lines (160 loc) · 5.64 KB
/
PD4NamithGStudentArrayTester.java
File metadata and controls
191 lines (160 loc) · 5.64 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*****************************************************************************************************************
NAME: Namith Gangireddyvari
PERIOD: 4
DUE DATE: 1/30/22
PURPOSE: Review how to declare and initialize arrays of references. Review the sequential search algorithm.
WHAT I LEARNED: That if you close a scanner that uses System.in, it will prevent the program
from using System.in again
CREDITS (BE SPECIFIC: FRIENDS, PEERS, ONLINE WEBSITE):
****************************************************************************************************************/
import java.util.Scanner;
public class PD4NamithGStudentArrayTester{
public static void main (String [] args){
StudentBody students = new StudentBody();
students.init();
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter the id of the student you are looking for: ");
int studentID = keyboard.nextInt();
Student target =students.search(studentID);
if(target != null)
System.out.println(target); else
System.out.println ("Student not found ...");
System.out.println("Student Body Information:\n" + students);
Student[] honorsStudents = students.aboveAvgStudents();
System.out.println("The honors students are ");
for (Student s: honorsStudents){
System.out.println (s);
}
}
} // StudentArrayTester
class StudentBody {
private Student[] students;
public StudentBody() {
students = new Student[3];
students[0] = new Student();
students[1] = new Student();
students[2] = new Student();
}
//Prompts user to enter data
public void init() {
Scanner sc = new Scanner(System.in);
for(int i = 0; i < students.length; i++){
System.out.print("\n Enter Student " +(i+1)+ "s' Name: ");
String name = sc.next();
System.out.print("\n Enter ID: ");
int id = sc.nextInt();
System.out.print("\n Enter gPA: ");
double grade = sc.nextDouble();
students[i] = new Student(name, id, grade);
}
}
//returns null if Student with ID “id” is not found in students
public Student search(int id) {
for (int i = 0; i < students.length; i++){
if(id == students[i].getStudentID()){
return students[i];
}
}
return null;
}
//returns a double representing the average GPA of all students in the Student Body
public double calcAvgGPA() {
double avg = 0;
for (int i = 0; i < students.length; i++){
avg+= students[i].getGPA();
}
return avg/students.length;
}
/* returns an array of Students that contain first students in StudentBody
whose GPA is greater than the average GPA of the student body you must call the calcAvgGPA
method in this method to get credit
*/
public Student[] aboveAvgStudents() {
int count = 0;
for (int i = 0; i < students.length; i++){
if(students[i].getGPA() >= calcAvgGPA()){
count++;
}
}
Student[] abvAvgStu = new Student[count];
count = 0;
for (int i = 0; i < students.length; i++){
if(students[i].getGPA() >= calcAvgGPA()){
abvAvgStu[count] = students[i];
}
}
return abvAvgStu;
}
public String toString() {
String print = "";
for(int i = 0; i < students.length; i++){
print += students[i].toString();
}
return print;
}
}
class Student{
private String name;
private int studentID;
private double gPA;
/* precondition: none
postcondition: create a Student object with default instance variable values */
public Student()
{
name = "unknown";
studentID = 0;
gPA = 0.0;
}
/* precondition: method must be called with String representing name, int representing student ID,
and double from 0.0-5.0 representing GPA
postcondition: create a Student object with specified instance variable values */
public Student(String iName, int iStudentID, double iGPA){
name = iName;
studentID = iStudentID;
gPA = iGPA;
}
/* precondition: must be called with valid Student object
postcondition: returns the name, student ID, and GPA of a student in the form of a string */
public String toString(){
return "Name: "+name+"\tStudent ID: "+studentID+"\tGPA: "+gPA;
}
/* precondition: must be called with valid Student object, must be called with a double argument from 0.0-5.0
postcondition: reassign the value of the argument to a Student’s GPA*/
public void setGPA(double nGPA){
gPA=nGPA;
}
/* precondition: must be called with valid Student object
postcondition: return the name of the Student */
public String getName(){
return name;
}
/* precondition: must be called with valid Student object
postcondition: will return the ID of the Student*/
public int getStudentID(){
return studentID;
}
/* precondition: must be called with valid Student object
postcondition: will return the GPA of the Student*/
public double getGPA(){
return gPA;
}
}//end Student class
/*
Output:
Enter Student 1s' Name: Joe
Enter ID: 1578840
Enter gPA: 4.0
Enter Student 2s' Name: Bob
Enter ID: 157367
Enter gPA: 3.5
Enter Student 3s' Name: Cam
Enter ID: 139581
Enter gPA: 4.0
Enter the id of the student you are looking for:
139581
Name: Cam Student ID: 139581 GPA: 4.0
Student Body Information:Name: Joe Student ID: 1578840 GPA: 4.0Name: Bob Student ID: 157367 GPA: 3.5Name: Cam Student ID: 139581 GPA: 4.0
The honors students are
Name: Cam Student ID: 139581 GPA: 4.0
Name: Joe Student ID: 1578840 GPA: 4.0
*/