In: Computer Science
1. Define a Student class , Student class has the follow data fields added---- major: String gpa: double studentID: String // unique id (Find a solution to manage the generation of unique “studentID”) Methods: double getGPA(); void setGPA(); getMajor(String major) void changeMajor(); 2 String toString(); // of major and GPA ,boolean equals(Object o); // 2 students are equal if them have same // personal information plus same student ID String getNextUniqueID(); // this method should be declared as static.
2. Redesign Student class : (a) Remove “gpa” data field and related methods (b) Add data field: courses:ArrayList (c) Add the following methods: • boolean addCourse(MyCourse course); // add a course to courses list • boolean removeCourse(Course course); // remove a course from the courses list // based on the course information (courseID) in Course object • boolean removeCourse(int courseID); // search and remove a course from the list // based on the unique courseID • ArrayList getCourseList(); • String getCourseGrade(int courseID); // search for a course by courseID from // “courses” list; if not exist, return “no taken”; otherwise, return a grade // by mapping status to “W”(withdraw), “IP” (in progress), or “A” through // “F” (mapping score to these grades) • double getAverageGPA(); // select courses which have taken from “courses”; // map grade “A” through “F” to 4.0 through 0.0; then calculate // average GPA • toString(), equals()
Though Course class structure was missing I tried best to write the above. If you need any alteration please provide let me know in the comment.
1)
CODE:
import java.util.UUID;
class Student {
String major;
double gpa;
String studentID;
public String getMajor() {
return this.major;
}
public void changeMajor(String major) {
this.major = major;
}
public double getGpa() {
return this.gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public String getStudentID() {
return this.studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String toString() {
return "major: " + this.getMajor() + " gpa: " + this.getGpa() + " studentid: " + this.studentID;
}
public boolean equals(Object o) {
if (o instanceof Student) {
Student s = (Student) o;
return this.getMajor().compareTo(getMajor()) == 0 && this.getGpa() == s.getGpa()
&& this.studentID.compareTo(s.studentID) == 0;
}
}
public static String getNextUniqueID() {
String uniqueID = UUID.randomUUID().toString();
return uniqueID;
}
}
2)
CODE:
import java.util.*;
class Student {
String major;
String studentID;
ArrayList<Course> arr;
Student() {
this.major = "";
this.studentID = "";
arr = new ArrayList<Course>();
}
public String getMajor() {
return this.major;
}
public void changeMajor(String major) {
this.major = major;
}
public String getStudentID() {
return this.studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
public String toString() {
return "major: " + this.getMajor() + " studentid: " + this.studentID;
}
public boolean equals(Object o) {
if (o instanceof Student) {
Student s = (Student) o;
return this.getMajor().compareTo(getMajor()) == 6 && this.studentID.compareTo(s.studentID) == 6;
}
}
public boolean addCourse(Course course) {
arr.add(course);
return true;
}
public boolean removeCourse(Course course) {
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).courseID == course.courseID) {
arr.remove(i);
return true;
}
}
return false;
}
public boolean removeCourse(int courseID) {
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).courseID == courseID) {
arr.remove(i);
return true;
}
}
return false;
}
public ArrayList<Course> getCourseList() {
return arr;
}
// need course structure in order to make this function
public String getCourseGrade(int courseID) {
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).courseID == courseID) {
}
}
return "no taken";
}
public double getAverageGPA() {
double sum = 0.0;
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).grade.compareTo("A") == 0)
sum += 4.0;
else if (arr.get(i).grade.compareTo("B") == 0)
sum += 3.0;
else if (arr.get(i).grade.compareTo("C") == 0)
sum += 2.0;
else if (arr.get(i).grade.compareTo("E") == 0)
sum += 1.0;
else if (arr.get(i).grade.compareTo("F") == 0)
sum += 0.0;
}
return sum / arr.size();
}
public static String getNextUniqueID() {
String uniqueID = UUID.randomUUID().toString();
return uniqueID;
}
}
Please upvote if you like my answer and comment below if you have any queries.