Design a user defined class called Course. Each course object will hold data abo
ID: 3669096 • Letter: D
Question
Design a user defined class called Course. Each course object will hold data about a course. The data includes courseID, courseName, credits and instructorName. Define the instance variables required of appropriate type. Provide only get methods along with toString( ) and equals( ) methods. Use JavaDoc style comments.
Design another user defined class called Student. Each student object will hold data on studentID, name, GPA, gender, and courseList. Make sure to define studentID of int type, name as String, gender as a char type and courseList as an array of Course. Declare a static variable named nextID initialized to 1000. nextID will be incremented inside the constructor and it’s value would be assigned to studentID. Please note the constructor will not require a separate parameter to initialize studentID. Provide only get methods. Also implement the toString( ) and equals( ) methods. Use JavaDoc style comments.
Create a client program with main method. Start by creating Course objects for each of the courses you are taking this semester. Next create two Student objects. Use the course objects you created to populate the courseLists for the two student objects. Demonstrate the various methods of the Student class.
Explanation / Answer
public class Course {
private int courseID;
private String courseName;
private int credits;
private String instructorName;
public Course(int courseID, String courseName, int credits,
String instructorName) {
super();
this.courseID = courseID;
this.courseName = courseName;
this.credits = credits;
this.instructorName = instructorName;
}
public int getCourseID() {
return courseID;
}
public String getCourseName() {
return courseName;
}
public int getCredits() {
return credits;
}
public String getInstructorName() {
return instructorName;
}
@Override
public boolean equals(Object obj){
if(obj instanceof Course){
return this.getCourseID() == ((Course)obj).getCourseID();
}
return false;
}
@Override
public String toString() {
return "Course[ ID: "+courseID+", Name: "+courseName+", Credits: "+credits+", InstructorName: "+instructorName+"]";
}
}
class Student{
private int studentID;
private String name;
private double gpa;
private char gender;
private Course[] courseList;
private static int nextID = 1000;
/**
* @param name
* @param gpa
* @param gender
* @param courseList
*/
public Student(String name, double gpa, char gender, Course[] courseList) {
this.name = name;
this.gpa = gpa;
this.gender = gender;
this.courseList = courseList;
this.studentID = nextID;
nextID++;
}
/**
* @return the studentID
*/
public int getStudentID() {
return studentID;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the gpa
*/
public double getGpa() {
return gpa;
}
/**
* @return the gender
*/
public char getGender() {
return gender;
}
/**
* @return the courseList
*/
public Course[] getCourseList() {
return courseList;
}
/**
* @return the nextID
*/
public static int getNextID() {
return nextID;
}
@Override
public boolean equals(Object obj){
if(obj instanceof Student){
return this.getStudentID() == ((Student)obj).getStudentID();
}
return false;
}
@Override
public String toString() {
return "Student[ ID: "+studentID+", Name: "+name+", GPA: "+gpa+", Gender: "+gender+"]";
}
}
class TestStudentCourse{
public static void main(String[] args) {
Course course1 = new Course(1, "DS", 4, "Alen");
Course course2 = new Course(2,"Network",3,"Bob");
Course course3 = new Course(3,"Communication",4,"Alex");
Student student1 = new Student("Mital", 8, 'M', new Course[]{course1, course2});
Student student2 = new Student("Bhatiya", 7.6, 'M', new Course[]{course1, course3});
System.out.println(course1.toString());
System.out.println(course2.toString());
System.out.println(course3.toString());
System.out.println(student1.toString());
System.out.println(student2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.