In: Computer Science
Discover classes for generating a student report card that lists all classes, grades, and the grade point average
for a semester. Create a UML diagram with constructors and methods. Implement the code including Javadoc comments.
The classes that are needed for Student report card are : Course , Grade , Report, ReportTester , Student
Course.java
public class Course {
private String name;
private String courseNum;
private Grade grade;
/**
* default constructor for Course object - sets name
and course number to N/A and grade to null
*/
public Course()
{
this.name = "N/A";
this.courseNum = "N/A";
this.grade = null;
}
/**
* overloaded constructor for Course object
* @param n - String to set as Course name
* @param c - String to set as Course number
* @param s - Grade object to set as course grade
*/
public Course(String n, String c, Grade g)
{
this.name = n;
this.courseNum = c;
this.grade = g;
}
/**
* gets the Course name
* @return - Course name as String object
*/
public String getName()
{
return name;
}
/**
* gets the course number
* @return - Course number as String object
*/
public String getCourseNum()
{
return courseNum;
}
/**
* gets the course letter grade
* @return - letter grade as a String
*/
public String getLetterGrade()
{
return grade.getLetter();
}
/**
* gets the numerical grade value of Course
* @return - grade value as a double
*/
public double getGradeValue()
{
return grade.getNumber();
}
/**
* sets the course name to a given string
* @param s - String to set as course name
*/
public void setName(String s)
{
name = s;
}
/**
* sets the course number to a given string
* @param s - String to set as course number
*/
public void setCourseNum(String s)
{
courseNum = s;
}
/**
* sets the course section number to a given int
* @param s - integer to set as course number
*/
public void setGrade(Grade s)
{
grade = s;
}
}
Grade.java
public class Grade {
private String letter;
private double number;
public Grade()
{
this.letter = "N/A";
}
public Grade(String l)
{
this.letter = l;
}
public String getLetter()
{
return letter;
}
public void setLetter(String l)
{
letter = l;
}
public double getNumber()
{
if(letter == "A")
{
return
4.0;
}
else if(letter == "B")
{
return
3.0;
}
else if(letter == "C")
{
return
2.0;
}
else if(letter == "D")
{
return
1.0;
}
else
{
return
0.0;
}
}
}
ReportCard.java
import java.util.ArrayList;
public class ReportCard {
private Student stu;
private ArrayList<Course> courses;
public ReportCard()
{
this.stu = null;
this.courses = null;
}
public ReportCard(Student s, ArrayList<Course>
c)
{
this.stu = s;
this.courses = c;
}
public double calculateGPA()
{
double totalPts = 0.0;
for(Course c : courses)
{
totalPts +=
c.getGradeValue();
}
double gpa =
totalPts/courses.size();
return Math.round(gpa * 100.0) /
100.0;
}
public String toString()
{
String str = "";
str += "NAME: " + stu.getName() +
"\nID: " + stu.getID() + "\n";
str += "\nCOURSES: \n";
for(Course c : courses)
{
str +=
c.getName() + "\t\t\t" + c.getLetterGrade() + "\n";
}
str += "\nGPA: " +
calculateGPA();
return str;
}
}
ReportCardTester.java
import java.util.ArrayList;
public class ReportCardTester {
public static void main(String[] args) {
Student test = new Student("Mariel
T", "012345");
ArrayList<Course> myCourses =
new ArrayList<Course>();
Course c1 = new Course("CECS 100",
"8109", new Grade("A"));
Course c2 = new Course("CECS 277",
"1243", new Grade("A"));
Course c3 = new Course("PHYS 151",
"4718", new Grade("B"));
myCourses.add(c1);
myCourses.add(c2);
myCourses.add(c3);
ReportCard myReport = new
ReportCard(test, myCourses);
System.out.print(myReport);
}
}
Student.java
public class Student {
private String name;
private String id;
/**
* default constructor for Student object - sets
Student name & id to "N/A"
*/
public Student()
{
this.name = "N/A";
this.id = "N/A";
}
/**
* overloaded constructor for Student object
* @param n - String to set as name attribute
* @param i - String to set as id attribute
*/
public Student(String n, String i)
{
this.name = n;
this.id = i;
}
/**
* gets the Student's name
* @return - name attribute as String
*/
public String getName()
{
return name;
}
/**
* gets the Student's ID
* @return - id attribute as String
*/
public String getID()
{
return id;
}
/**
* sets the Student name attribute to a given
String
* @param n - String to set as the name
*/
public void setName(String n)
{
name = n;
}
/**
* sets the Student id attribute to a given
String
* @param n - String to set as the id
*/
public void setID(String i)
{
id = i;
}
}