The objective of the assignment is to develop your understanding of class relati
ID: 3807807 • Letter: T
Question
The objective of the assignment is to develop your understanding of class relationship in java Topic of assignment: Given the UML, perform the following tasks: Define a class named course Define a class named Transcript which has course as an aggregation class. (you need to create an array of course for the transcript) Define a class named Student which has Transcript as an aggregation class. (you need to create an instance of Transcript for the student) write a test class to test the three classes. The main method should perform the following tasks: Create an instance of Student Add three courses to the transcript of the student Calculate and display the GPA of the student The UML diagram for the classes:Explanation / Answer
Please find the code with explanations:
public class Course {
String courseID;
char grade;
public Course(String csID, char g) {
this.courseID = csID;
this.grade = g;
}
public String getCourseID() {
return courseID;
}
public void setCourseID(String csID) {
this.courseID = csID;
}
public char getGrade() {
return grade;
}
public void setGrade(char g) {
this.grade = g;
}
}
public class Transcript {
Course[] enrolledCourse;
int numOfCourse;
public Transcript() {
enrolledCourse = new Course[1];
numOfCourse = 0;
}
public Transcript(Course[] enrolledCourse) {
this.enrolledCourse = enrolledCourse;
this.numOfCourse = enrolledCourse.length;
}
// when a course is found, return the corresponding grade
// when a course is not found, return blank
//
public char getCourseGrade(String csID) {
for(int i=0; i<numOfCourse; i++) {
if(enrolledCourse[i].getCourseID() == csID) {
return enrolledCourse[i].getGrade();
}
}
return 'f';
}
// no formula given to compute this
public float getGpa() {
float total = 0.0f;
for(int i=0; i<numOfCourse; i++) {
float score = 0.0f;
switch(enrolledCourse[i].getGrade()) {
case 'a':
score = 9.0f;
break;
case 'b':
score = 8.0f;
break;
case 'c':
score = 7.0f;
break;
case 'd':
score = 6.0f;
break;
case 'e':
score = 5.0f;
break;
case 'f':
score = 4.0f;
break;
default:
score = 1.0f;
}
total += score;
}
if(numOfCourse == 0){
return 0.0f;
}
return total/numOfCourse;
}
public int getNumOfCourse() {
numOfCourse = enrolledCourse.length;
return numOfCourse;
}
// Arrays are immutable in Java hence, we need to use a temproary
// array to store the resultant N+1 courses
// then assign the temporary array back to the original
// array
//
public boolean addCourse(String csID, char g) {
Course c = new Course(csID, g);
Course tempArray[] = new Course[numOfCourse+1];
for(int i=0; i<numOfCourse; i++) {
tempArray[i] = enrolledCourse[i];
}
tempArray[numOfCourse] = c;
enrolledCourse = tempArray;
numOfCourse++;
return true;
}
// find the course.. and then copy the last course to that position
// Then, set the size of the array to one less than what it is,
// this operation effectively erases the found course.
// Finally, return true.
// If the course was not found, return false.
//
public boolean removeCourse(String csID) {
for(int i=0; i<numOfCourse; i++) {
if(enrolledCourse[i].getCourseID() == csID) {
Course temp = enrolledCourse[i];
enrolledCourse[i] = enrolledCourse[numOfCourse-1];
numOfCourse--;
return true;
}
}
return false;
}
// find the course. if the course was found, change the grade.
// if the course was not found, then return false.
//
public boolean changeCourseGrade(String csID, char g) {
for(int i=0; i<numOfCourse; i++) {
if(enrolledCourse[i].getCourseID() == csID) {
enrolledCourse[i].setGrade(g);
return true;
}
}
return false;
}
}
public class Student {
String id;
String name;
Transcript transcript;
public Student() {
transcript = new Transcript();
}
public Student(String id, String name, Transcript transcript){
this.id = id;
this.name = name;
this.transcript = transcript;
}
public String getID() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Transcript getTranscript() {
return transcript;
}
public void setTranscript(Transcript transcript) {
this.transcript = transcript;
}
}
public class TestProgram {
public static void main(String args[]) {
Student s = new Student();
Transcript t = s.getTranscript();
t.addCourse("CS101", 'b');
t.addCourse("MA101", 'c');
t.addCourse("NW101", 'a');
s.setTranscript(t);
System.out.println(s.getTranscript().getGpa());
}
}
public class Course {
String courseID;
char grade;
public Course(String csID, char g) {
this.courseID = csID;
this.grade = g;
}
public String getCourseID() {
return courseID;
}
public void setCourseID(String csID) {
this.courseID = csID;
}
public char getGrade() {
return grade;
}
public void setGrade(char g) {
this.grade = g;
}
}
public class Transcript {
Course[] enrolledCourse;
int numOfCourse;
public Transcript() {
enrolledCourse = new Course[1];
numOfCourse = 0;
}
public Transcript(Course[] enrolledCourse) {
this.enrolledCourse = enrolledCourse;
this.numOfCourse = enrolledCourse.length;
}
// when a course is found, return the corresponding grade
// when a course is not found, return blank
//
public char getCourseGrade(String csID) {
for(int i=0; i<numOfCourse; i++) {
if(enrolledCourse[i].getCourseID() == csID) {
return enrolledCourse[i].getGrade();
}
}
return 'f';
}
// no formula given to compute this
public float getGpa() {
float total = 0.0f;
for(int i=0; i<numOfCourse; i++) {
float score = 0.0f;
switch(enrolledCourse[i].getGrade()) {
case 'a':
score = 9.0f;
break;
case 'b':
score = 8.0f;
break;
case 'c':
score = 7.0f;
break;
case 'd':
score = 6.0f;
break;
case 'e':
score = 5.0f;
break;
case 'f':
score = 4.0f;
break;
default:
score = 1.0f;
}
total += score;
}
if(numOfCourse == 0){
return 0.0f;
}
return total/numOfCourse;
}
public int getNumOfCourse() {
numOfCourse = enrolledCourse.length;
return numOfCourse;
}
// Arrays are immutable in Java hence, we need to use a temproary
// array to store the resultant N+1 courses
// then assign the temporary array back to the original
// array
//
public boolean addCourse(String csID, char g) {
Course c = new Course(csID, g);
Course tempArray[] = new Course[numOfCourse+1];
for(int i=0; i<numOfCourse; i++) {
tempArray[i] = enrolledCourse[i];
}
tempArray[numOfCourse] = c;
enrolledCourse = tempArray;
numOfCourse++;
return true;
}
// find the course.. and then copy the last course to that position
// Then, set the size of the array to one less than what it is,
// this operation effectively erases the found course.
// Finally, return true.
// If the course was not found, return false.
//
public boolean removeCourse(String csID) {
for(int i=0; i<numOfCourse; i++) {
if(enrolledCourse[i].getCourseID() == csID) {
Course temp = enrolledCourse[i];
enrolledCourse[i] = enrolledCourse[numOfCourse-1];
numOfCourse--;
return true;
}
}
return false;
}
// find the course. if the course was found, change the grade.
// if the course was not found, then return false.
//
public boolean changeCourseGrade(String csID, char g) {
for(int i=0; i<numOfCourse; i++) {
if(enrolledCourse[i].getCourseID() == csID) {
enrolledCourse[i].setGrade(g);
return true;
}
}
return false;
}
}
public class Student {
String id;
String name;
Transcript transcript;
public Student() {
transcript = new Transcript();
}
public Student(String id, String name, Transcript transcript){
this.id = id;
this.name = name;
this.transcript = transcript;
}
public String getID() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Transcript getTranscript() {
return transcript;
}
public void setTranscript(Transcript transcript) {
this.transcript = transcript;
}
}
public class TestProgram {
public static void main(String args[]) {
Student s = new Student();
Transcript t = s.getTranscript();
t.addCourse("CS101", 'b');
t.addCourse("MA101", 'c');
t.addCourse("NW101", 'a');
s.setTranscript(t);
System.out.println(s.getTranscript().getGpa());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.