Create the following classes in BlueJ: A Student class that has: A field to stor
ID: 3607041 • Letter: C
Question
Create the following classes in BlueJ:
A Student class that has:
A field to store the student’s first name.
A field to store the student’s last name.
A field to store the student’s unique student number.
Any constructors/setters/getters you feel are necessary.
A Course class that has:
A field for a unique title (for example CS101).
A field (string) that is the start time for the course (for example M15:30).
A field (string) that is the location for the course (for example ROB121).
A field that is a collection of the students registered for the course.
A field that is the capacity (maximum number of students) for the course.
Any constructors/setters/getters you feel are necessary.
A method to add a student to a course.
A method to remove a student from a course.
A method to print a list students in the course.
A method to search for a student in the course by student number.
A Registrar class that has:
A field that is a collection of all courses offered.
A constructor to initialize an empty collection of courses.
A method to add a course to the offered courses.
A method to remove a course from the offered courses.
A method to print a list of all offered courses.
A method to print a list of only the offered courses that are still available (not at capacity).
A (private) method to search the courses offered by title.
A method to register a student for a course.
A method to unregister a student from a course.
Print a list of students registered for a course.
Print a given student’s schedule.
Explanation / Answer
The required classes are written bellow. The names of methods and their functions are self explainatory.
Student Class:
package course;
public class Student {
//All the required variable
private String sid;
private String fname;
private String lname;
//constructor
public Student(String sid, String fname, String lname) {
super();
this.sid = sid;
this.fname = fname;
this.lname = lname;
}
@Override
public String toString() {
return "["+sid+" "+fname+" "+lname+"]";
}
// getter and setter methods
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
Course Class:
package course;
import java.util.ArrayList;
public class Course {
// all the variables
private String title;
private String startTime;
private String location;
private int capacity;
ArrayList<Student> registeredStudents = new ArrayList<Student>();
// constructor
public Course(String title, String startTime, String location, int capacity) {
super();
this.title = title;
this.startTime = startTime;
this.location = location;
this.capacity = capacity;
}
@Override
public String toString() {
return "["+title+" "+startTime+" "+location+" "+capacity+"]";
}
//getter and setter methods
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public void addStudent(String sid){
Student s = getStudent(sid);
if(s != null)
registeredStudents.add(s);
}
public void addStudent(Student s){
registeredStudents.add(s);
}
public void removeStudent(String sid){
Student s = getStudent(sid);
if(s != null)
registeredStudents.remove(s);
}
public void removeStudent(Student s){
registeredStudents.remove(s);
}
public Student getStudent(String sid){
for(Student s: registeredStudents){
if(s.getSid().equals(sid)){
return s;
}
}
return null;
}
public void printStudentList(){
for(Student s: registeredStudents){
System.out.println(s);
}
}
public boolean isAvailable(){
return (capacity<registeredStudents.size());
}
}
Registrar Class:
package course;
import java.util.ArrayList;
public class Registrar {
private ArrayList<Course> offeredCourses;
public Registrar() {
offeredCourses = new ArrayList<Course>();
}
public void addCourse(Course c){
offeredCourses.add(c);
}
public void removeCourse(Course c){
offeredCourses.remove(c);
}
// a student is allowed to register is the course is available and the start time is not conflicting
public void registerStudent(Student s, Course c){
if(c.isAvailable()){
for(Course c1 : getCourses(s)){
if(c1.getStartTime().equals(c.getStartTime())){
return;
}
}
c.addStudent(s);
}
}
public void unregisterStudent(Student s, Course c){
c.removeStudent(s);
}
public void printRegisteredStudents(Course c){
c.printStudentList();
}
public void printStudentSchedule(Student s){
System.out.println("Schedule for student "+s.getFname()+" "+s.getLname());
for(Course c : offeredCourses){
if(c.registeredStudents.contains(s)){
System.out.println(c.getStartTime()+" "+c.getTitle());
}
}
}
private ArrayList<Course> getCourses(Student s){
ArrayList<Course> cources = new ArrayList<Course>();
for(Course c : offeredCourses){
if(c.registeredStudents.contains(s)){
cources.add(c);
}
}
return cources;
}
public void printCourseList(){
for(Course c: offeredCourses){
System.out.println(c);
}
}
}
NB: Hope it helps to understand the classes. Please let me know if anything else is needed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.