Using java --> Student Object: The Student object should store at least an Array
ID: 3568474 • Letter: U
Question
Using java -->
Student Object:
The Student object should store at least an ArrayList of Course objects. Each Student has a first name, last name, credits so far, and GPA. You should be able to get all variables stored by a Student object but only be able to set the names.
Progress() that we will return to later (no parameters, no return value). Constructors are up to you - this means, think about what you need a constructor to do!
Create addCourse and removeCourse methods: the first takes a CourseInfo object and adds it to the Student's ArrayList; the second takes an index and removes the specified item from the Student's ArrayList.
Course Object:
Each Course object has a name, discipline, number, credits and current grade (0-100). For example, "Programming", "CSC", 101, 3, 85 would be a set of data for someone in this course.
It should have getters and setters for all values.
It should also have a cloneWithoutGrade method that returns a new Course object with the same name, discipline, number, and credits as the object you call cloneWithoutGrade on.
Now, return to Progress () in the Student class. The function of this method is two-fold: (1) update the GPA and credit hours of this Student to their new GPA and credit hours after completing this semester and (2) empty the ArrayList of Course objects. Computing a new GPA of the current courses - note that, in college, because courses are worth differing amounts of credit, you do have to compute the Total Grade Points then divide by the Total Credit Hours. To account for the old GPA and old number of credits, add the old GPA times old # credits to your Total Grade Points and old number of credits to Total Credit Hours - then you have properly-weighted Total Grade Points and Total Credit Hours so you can compute the new GPA. Update the GPA and total credit hours according to these new values then erase all the courses stored in that Student ArrayList.
Write a test case for progress( ) in main method.
Explanation / Answer
import java.util.ArrayList;
class Student {
ArrayList<Course> courses = new ArrayList<Course>();
private String firstName;
private String lastName;
private int creditSoFar;
private int GPA;
public ArrayList<Course> getCourses() {
return courses;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getCreditSoFar() {
return creditSoFar;
}
public int getGPA() {
return GPA;
}
public Student(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
creditSoFar = 0;
GPA = 0;
}
void progress() {
int totalGradePoints=0;
int totalCredits=0;
for(Course c:this.courses){
totalGradePoints += c.getCurrentGrade();
totalCredits += c.getCredits();
}
totalGradePoints += this.GPA * this.creditSoFar;
totalCredits += this.creditSoFar;
this.GPA = totalGradePoints/totalCredits;
this.creditSoFar = totalCredits;
this.courses.clear();
}
void addCourse(Course c) {
this.courses.add(c);
}
void removeCourse(int index) {
this.courses.remove(index);
}
}
class Course {
private String name;
private String descipline;
private int number;
private int credits;
private int currentGrade;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescipline() {
return descipline;
}
public void setDescipline(String descipline) {
this.descipline = descipline;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCredits() {
return credits;
}
public void setCredits(int credits) {
this.credits = credits;
}
public int getCurrentGrade() {
return currentGrade;
}
public void setCurrentGrade(int currentGrade) {
this.currentGrade = currentGrade;
}
public Course(String name, String descipline, int number, int credits,
int currentGrade) {
super();
this.name = name;
this.descipline = descipline;
this.number = number;
this.credits = credits;
this.currentGrade = currentGrade;
}
Course cloneWithoutGrade() throws CloneNotSupportedException {
return (Course) this.clone();
}
}
public class Main {
public static void main(String[] args) {
Course programming = new Course("Programming", "CSC", 101, 3, 85 );
Student s1 = new Student("Rahul","Tiwari");
System.out.println("Before Calling process() Credits and GPA of s1 is "+ s1.getCreditSoFar() + " " + s1.getGPA());
s1.addCourse(programming);
s1.progress();
System.out.println("After Calling process() Credits and GPA of s1 is "+ s1.getCreditSoFar() + " " + s1.getGPA());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.