I need help on this computer science assignment. I understand most of the part b
ID: 3879360 • Letter: I
Question
I need help on this computer science assignment. I understand most of the part but the course grading calculator.
The first class is a Student class (Student.java) that has the following properties and behaviors:Members (all should be declared private)
Firstname (String firstName)
Lastname (String lastNname)
An array of Courses (Course [] courses)
A counter for the number of courses actually stored in courses (int numCourses)
MethodsConstructor
Student(String firstName, String lastName, int maxNumCourses)
The first 2 parameters are assigned to the corresponding members that belong to the object (You learned how to do this in Chap 9).
The final parameter is used to initialize courses (You learned how to initialize arrays in Chapter 7).
numCourses is initialized to 0
createCourse(String courseName, int creditHours, char letterGrade)
This method creates a new Course and adds it to courses
Create a new course and store it at index numCourses in the courses array. Then increment numCourses.1
computeGPA()
This method computes the GPA for all courses in courses using the credit hours and grade information.
The getQualityPoints method in class Course tells you how many points a course is worth in the GPA
Refer to Computing GPA for a reminder of how the GPA is computed
toString()
insert the following line of code inside this method
Example output: Student Jane Doe has a 3.4 GPA
The second class is a Main class (Main.java) that creates 3 students (3 separate Student objects) with the following informationName: Jane Doe (Note: number of courses is 5)List of Courses
Course Name: Math, Credit Hours: 4, Letter Grade: ‘A’
Course Name: English, Credit Hours: 3, Letter Grade: ‘A’
Course Name: CS, Credit Hours: 4, Letter Grade: ‘A’
Course Name: Chemistry, Credit Hours: 3, Letter Grade: ‘B;
Course Name: Health, Credit Hours: 2, Letter Grade: ‘B’
Name: John Smith (Note: number of courses is 5)List of Courses
Course Name: Math, Credit Hours: 4, Letter Grade: ‘B’
Course Name: English, Credit Hours: 3, Letter Grade: ‘B’
Course Name: CS, Credit Hours: 4, Letter Grade: ‘C’
Course Name: Chemistry, Credit Hours: 3, Letter Grade: ‘C;
Course Name: Health, Credit Hours: 2, Letter Grade: ‘B’
Name: David Hill (Note: number of courses is 3)List of Courses
Course Name: Math, Credit Hours: 4, Letter Grade: ‘C’
Course Name: English, Credit Hours: 3, Letter Grade: ‘A’
Course Name: Health, Credit Hours: 2, Letter Grade: ‘A’
Note: You will be calling the createCourse method that you defined inside the Student class inside Main.java, print each object (i.e., invoke the System.out.println that calls the toString method).
THIS IS THE COURSE.JAVA:
Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
//Course.java class, no changes made
public class Course {
private final String courseName;
private final int creditHours;
private final char letterGrade;
public Course(String courseName, int creditHours, char letterGrade) {
this.courseName = courseName;
this.creditHours = creditHours;
this.letterGrade = letterGrade;
}
public String getCourseName() {
return courseName;
}
public int getCreditHours() {
return creditHours;
}
public char getLetterGrade() {
return letterGrade;
}
public int getQualityPoints() {
switch (letterGrade) {
case 'A': return 4;
case 'B': return 3;
case 'C': return 2;
case 'D': return 1;
default: return 0;
}
}
}
//Student.java class
public class Student {
private String firstName;
private String lastName;
private Course[] courses;
private int numCourses;
public Student(String firstName, String lastName, int maxNumCourses) {
this.firstName = firstName;
this.lastName = lastName;
courses=new Course[maxNumCourses];
numCourses=0;
}
public void createCourse(String courseName, int creditHours, char letterGrade){
Course c=new Course(courseName, creditHours, letterGrade);
if(numCourses<courses.length){
courses[numCourses]=c;
numCourses++;
}else{
System.out.println("Course array is full!");
}
}
/**
* method to compute and return the GPA
* @return
*/
public double computeGPA(){
double gpa=0;
int totalGradepoints=0;
int totalCreditHours=0;
for(int i=0;i<numCourses;i++){
/**
* Calculating the grade/quality points
*/
int gradePoints=courses[i].getCreditHours()*courses[i].getQualityPoints();
/**
* Adding to the total
*/
totalGradepoints+=gradePoints;
/**
* Adding credit hours to the total credit hours
*/
totalCreditHours+=courses[i].getCreditHours();
}
/**
* gpa= Total Quality points earned / total credit hours
*/
gpa=(double)totalGradepoints/totalCreditHours;
return gpa;
}
/**
* method which will return a string containing all data
* note that gpa has been rounded to one digit after decimal point
*/
public String toString() {
return "Student " + firstName + " " + lastName + " has a " + String.format("%.1f", computeGPA()) + " GPA";
}
}
//Main.java class
public class Main {
public static void main(String[] args) {
/**
* creating a student
*/
Student student1=new Student("Jane", "Doe", 5);
/**
* creating courses for student1
*/
student1.createCourse("Math", 4, 'A');
student1.createCourse("English", 3, 'A');
student1.createCourse("CS", 4, 'A');
student1.createCourse("Chemistry", 3, 'B');
student1.createCourse("Health", 2, 'B');
/**
* creating another student
*/
Student student2=new Student("John", "Smith", 5);
/**
* creating courses for student2
*/
student2.createCourse("Math", 4, 'B');
student2.createCourse("English", 3, 'B');
student2.createCourse("CS", 4, 'C');
student2.createCourse("Chemistry", 3, 'C');
student2.createCourse("Health", 2, 'B');
/**
* creating another student
*/
Student student3=new Student("David", "Hill", 3);
/**
* creating courses for student2
*/
student3.createCourse("Math", 4, 'C');
student3.createCourse("English", 3, 'A');
student3.createCourse("Health", 2, 'A');
/**
* Displaying each student's data
*/
System.out.println(student1);
System.out.println(student2);
System.out.println(student3);
}
}
/*OUTPUT*/
Student Jane Doe has a 3.7 GPA
Student John Smith has a 2.6 GPA
Student David Hill has a 3.1 GPA
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.