-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathStudent.java
More file actions
129 lines (104 loc) · 3.58 KB
/
Student.java
File metadata and controls
129 lines (104 loc) · 3.58 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
package org.launchcode;
public class Student {
private static int nextStudentId = 1;
private String name;
private int studentId;
private int numberOfCredits = 0;
private double gpa = 0.0;
public Student (String name, int studentId, int numberOfCredits, double gpa) {
this.name = name;
this.studentId = studentId;
this.numberOfCredits = numberOfCredits;
this.gpa = gpa;
}
public Student(String name, int studentId) {
this(name, studentId, 0, 0);
}
public Student(String name) {
this(name, nextStudentId);
nextStudentId++;
}
public String studentInfo() {
return (this.name + " has a GPA of: " + this.gpa);
}
//TODO: Uncomment and complete the getGradeLevel method here:
public String getGradeLevel() {
public static String getGradeLevel(int credits) {
if (credits <= 29){
return "freshman";
} else if (credits <= 59){
return "sophomore";
} else if (credits <= 89) {
return "junior";
} else {
return "senior";
}
}
}
// // Determine the grade level of the student based on numberOfCredits
// }
// TODO: Complete the addGrade method.
public void addGrade(int courseCredits, double grade) {
// Update the appropriate fields: numberOfCredits, gpa
public void addGrade(int courseCredits, double grade) {
double totalQualityScore = this.gpa * this.numberOfCredits;
totalQualityScore += courseCredits * grade;
this.numberOfCredits += courseCredits;
this.gpa = totalQualityScore/this.numberOfCredits;
}
}
// TODO: Add your custom 'toString' method here. Make sure it returns a well-formatted String rather
// than just the class fields.
// TODO: Add your custom 'equals' method here. Consider which fields should match in order to call two
// Student objects equal.
public String toString() {
String studentReport = String.format("%s is a %s with %d credits and a GPA of %.2f", this.name, this.getGradeLevel(this.numberOfCredits), this.getNumberOfCredits(), this.getGpa());
return studentReport;
}
public boolean equals(Object toBeCompared) {
if (toBeCompared == this) {
return true;
}
if (toBeCompared == null) {
return false;
}
if (toBeCompared.getClass() != getClass()) {
return false;
}
Student theStudent = (Student) toBeCompared;
return theStudent.getStudentId() == getStudentId();
}
public String getName() {
return name;
}
public int getStudentId() {
return studentId;
}
public int getNumberOfCredits() {
return numberOfCredits;
}
public double getGpa() {
return gpa;
}
public void setName(String name) {
this.name = name;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
private void setNumberOfCredits(int numberOfCredits) {
this.numberOfCredits = numberOfCredits;
}
public static void main(String[] args) {
Student sally = new Student("Sally",1,1,4.0);
System.out.println("The Student class works! " + sally.getName() + " is a student!");
System.out.println(sally);
sally.addGrade(12, 3.5);
System.out.println(sally);
sally.addGrade(25, 3.8);
System.out.println(sally);
}
}