-
Notifications
You must be signed in to change notification settings - Fork 686
Expand file tree
/
Copy pathStudent.java
More file actions
121 lines (98 loc) · 3.3 KB
/
Student.java
File metadata and controls
121 lines (98 loc) · 3.3 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
package org.launchcode;
import java.sql.SQLOutput;
import java.util.Objects;
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(int numberOfCredits) {
// Determine the grade level of the student based on numberOfCredits
if(this.numberOfCredits < 30) {
return "Freshman";
} else if(this.numberOfCredits < 60) {
return "Sophomore";
} else if(this.numberOfCredits < 90) {
return "Junior";
} else {
return "Senior";
}
}
// TODO: Complete the addGrade method.
public void addGrade(int courseCredits, double grade) {
// Update the appropriate fields: numberOfCredits, gpa
double qualityScore = this.gpa * this.numberOfCredits;
qualityScore += courseCredits * grade;
this.numberOfCredits += courseCredits;
this.gpa = qualityScore/this.numberOfCredits;
}
public String toString(){
return String.format("%s is a %s with %d credits and a GPA of %.2f", this.name, this.getGradeLevel(this.numberOfCredits), this.getNumberOfCredits(), this.getGpa());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return studentId == student.studentId;
}
@Override
public int hashCode() {
return Objects.hash(studentId);
}
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);
Student sal = new Student("Sal", 2, 3, 4);
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);
System.out.println(sally.equals(sal));
Course trig = new Course("Trigonometry", 3, "Mater");
System.out.println(trig.toString());
}
}