Skip to content

Commit 48d90e7

Browse files
committed
Classes pt 2 Excercise
1 parent 9376e91 commit 48d90e7

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

classes-part-2/exercises/src/main/java/org/launchcode/Course.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,39 @@
33
import java.util.ArrayList;
44

55
public class Course {
6-
private String topic;
7-
private Teacher instructor;
8-
private ArrayList<Student> enrolledStudents;
6+
private final String topic;
7+
private final Teacher instructor;
8+
private final ArrayList<Student> enrolledStudents;
9+
10+
public Course(String topic, Teacher instructor, ArrayList<Student> enrolledStudents) {
11+
this.topic = topic;
12+
this.instructor = instructor;
13+
this.enrolledStudents = enrolledStudents;
14+
}
915

1016
// TODO: Add your custom 'toString' method here. Make sure it returns a well-formatted String rather than
1117
// just the class fields.
18+
public String toString() {
19+
return "Course = " + topic + " / Instructor = " + instructor +
20+
" / Students = " + enrolledStudents;
21+
}
1222

1323

1424
// TODO: Add your custom 'equals' method here. Consider which fields should match in order to call two
1525
// Course objects equal.
16-
26+
public boolean equals(Object toBeCompared) {
27+
if (toBeCompared == this) {
28+
return true;
29+
}
30+
if (toBeCompared == null) {
31+
return false;
32+
}
33+
if (toBeCompared.getClass() != getClass()) {
34+
return false;
35+
}
36+
Course course = (Course) toBeCompared;
37+
return topic.equals(course.topic) &&
38+
instructor.equals(course.instructor) &&
39+
enrolledStudents.equals(course.enrolledStudents);
40+
}
1741
}

classes-part-2/exercises/src/main/java/org/launchcode/Student.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,55 @@ public String studentInfo() {
3030

3131

3232
//TODO: Uncomment and complete the getGradeLevel method here:
33-
// public String getGradeLevel() {
34-
// // Determine the grade level of the student based on numberOfCredits
35-
// }
33+
public static String getGradeLevel(int credits) {
34+
if (credits <= 29){
35+
return "freshman";
36+
} else if (credits <= 59){
37+
return "sophomore";
38+
} else if (credits <= 89) {
39+
return "junior";
40+
} else {
41+
return "senior";
42+
}
43+
}
3644

3745
// TODO: Complete the addGrade method.
3846
public void addGrade(int courseCredits, double grade) {
39-
// Update the appropriate fields: numberOfCredits, gpa
47+
// Calculate quality score for the new course
48+
double qualityScore = grade * courseCredits;
49+
50+
// Update total quality score and number of credits
51+
double totalQualityScore = this.gpa * this.numberOfCredits;
52+
totalQualityScore += qualityScore;
53+
this.numberOfCredits += courseCredits;
54+
55+
// Calculate new GPA
56+
this.gpa = totalQualityScore / this.numberOfCredits;
4057
}
4158

4259
// TODO: Add your custom 'toString' method here. Make sure it returns a well-formatted String rather
4360
// than just the class fields.
61+
public String toString() {
62+
return "Student = " + name + " / ID = " + studentId +
63+
" / Credits = " + numberOfCredits + " / GPA = " + gpa;
64+
}
4465

4566
// TODO: Add your custom 'equals' method here. Consider which fields should match in order to call two
4667
// Student objects equal.
68+
public boolean equals(Object toBeCompared) {
69+
if (toBeCompared == this) {
70+
return true;
71+
}
72+
if (toBeCompared == null) {
73+
return false;
74+
}
75+
if (toBeCompared.getClass() != getClass()) {
76+
return false;
77+
}
78+
Student theStudent = (Student) toBeCompared;
79+
return theStudent.getStudentId() == getStudentId();
80+
}
81+
4782

4883
public String getName() {
4984
return name;

0 commit comments

Comments
 (0)