You will be completing and writing classes that could be used as part of a cours
ID: 3742887 • Letter: Y
Question
You will be completing and writing classes that could be used as part of a course registration system.
Part A: The Student Class
Complete the class by writing an equals method to override the equals method inherited from Object. Two students should be considered logically equivalent if they have the same id, first name, last name, and tuition paid status.
Part B: The Course Class
You will write a Course class. A Course keeps track of Students enrolled in the Course (on the roster) and on the waitlist for the Course. Include:
Instance data variables
a course name
two Student[] objects- one for roster, one for waitlist
the maximum number of students allowed on the waitlist and roster
any other variables you think are helpful
A constructor
A Course object is initially created with an empty roster and empty waitlist and by specifying the maximum number of students allowed on the roster and waitlist
Getters and setters
Carefully consider which variables should have setters.
Include validity checks in setters when appropriate.
A toString method whose text representation includes
the name of the course
the number of students enrolled in the course and the maximum number that can be enrolled
the roster of enrolled students
the number of students on the waitlist and the maximum number that can be on the waitlist
the students on the waitlist
make sure that there are no "nulls" printed with the arrays
An addStudent method
method header: public boolean addStudent(Student student)
if the student has paid their tuition and are not already enrolled on the roster or waitlist, the student is eligible to be added to the class
if there is room on the roster, add the student to the roster
if the roster is full but there is room on the waitlist, add the student to the waitlist
if there is no room on the roster or waitlist, do not add the student
if the student has not paid their tuition or is already on the roster or waitlist, do not add the student
return true or false based on whether the student is added or not
A dropStudent method
method header: public boolean dropStudent(Student student)
if the student is not on the roster or waitlist, the student cannot be removed
if the student is on the roster, remove the student from the roster
since there is now one more space in the class, if the waitlist is not empty, take the first person off the waitlist and add them to the roster
if the student is on the waitlist, remove the student from the waitlist
return true or false based on whether the student is removed or not
Part C: Interactive Driver Program
Write an interactive driver program that creates a Course object (you can decide the name and roster/waitlist sizes). Then, use a loop to interactively allow the user to add students, drop students, or view the course.
Here are the files for this project: Project2Files.zip
Part D:
Write a class called CourseAL. This class has the same methods as Course (listed above). Instead of using a Student[] to store the roster, use an ArrayList. For full credit, take advantage of the methods in the ArrayList class to implement the methods. You might be able to significantly streamline your code!
must submit both the Course and CourseAL classes.
Submission
Zip all files together and upload that file.
.
.
.
public class Student {
private String firstName, lastName, id;
private boolean tuitionPaid;
public Student(String firstName, String lastName, String id, boolean tuitionPaid) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.tuitionPaid = tuitionPaid;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public boolean isTuitionPaid() {
return tuitionPaid;
}
public void setTuitionPaid(boolean tuitionPaid) {
this.tuitionPaid = tuitionPaid;
}
@Override
public String toString() {
return firstName + " " + lastName + " (" + id + ")";
}
}
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Student.java
public class Student {
private String firstName, lastName, id;
private boolean tuitionPaid;
public Student(String firstName, String lastName, String id,
boolean tuitionPaid) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.tuitionPaid = tuitionPaid;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public boolean isTuitionPaid() {
return tuitionPaid;
}
public void setTuitionPaid(boolean tuitionPaid) {
this.tuitionPaid = tuitionPaid;
}
@Override
public String toString() {
return firstName + " " + lastName + " (" + id + ")";
}
@Override
public boolean equals(Object other) {
if (other instanceof Student) {
Student s = (Student) other;
/**
* students are equal if id, names and tution paid status are equal
*/
if (this.id.equals(s.id)
&& this.firstName.equalsIgnoreCase(s.firstName)
&& this.lastName.equalsIgnoreCase(s.lastName)
&& this.tuitionPaid == s.tuitionPaid) {
return true;
}
}
return false;
}
}
// Course.java
import java.util.Arrays;
public class Course {
// attributes
private String courseName;
private Student[] roster;
private Student[] waitList;
private final int MAX_STUDENTS_ROSTER;
private final int MAX_STUDENTS_WAITLIST;
private int currentRosterSize;
private int currentWLSize;
/**
* constructor taking parameters to initialize maximum roster size and
* waitlist size
*/
public Course(int max_roster, int max_waitlist) {
MAX_STUDENTS_ROSTER = max_roster;
MAX_STUDENTS_WAITLIST = max_waitlist;
roster = new Student[MAX_STUDENTS_ROSTER];
waitList = new Student[MAX_STUDENTS_WAITLIST];
currentRosterSize = 0;
currentWLSize = 0;
}
//getter for course name
public String getCourseName() {
return courseName;
}
//setter for course name
public void setCourseName(String courseName) {
this.courseName = courseName;
}
//returns a non null list of students in roster
public Student[] getRoster() {
return Arrays.copyOf(roster, currentRosterSize);
}
//returns a non null list of students in waitlist
public Student[] getWaitList() {
return Arrays.copyOf(waitList, currentWLSize);
}
//returns the maximum roster size
public int getMaxStudentsRoster() {
return MAX_STUDENTS_ROSTER;
}
//returns the max waitlist size
public int getMaxStudentsWaitList() {
return MAX_STUDENTS_WAITLIST;
}
//returns current number of students enrolled
public int getCurrentRosterSize() {
return currentRosterSize;
}
//returns current number of students on waitlist
public int getCurrentWaitListSize() {
return currentWLSize;
}
@Override
public String toString() {
//returning properly formatted string containing whole info
String data = "Course name: " + courseName + " ";
data += "Number of students enrolled: " + getCurrentRosterSize() + " ";
data += "Maximum number of students that can be enrolled: "
+ getMaxStudentsRoster() + " ";
data += "Roster: " + Arrays.toString(getRoster()) + " ";
data += "Number of students on the waitlist: "
+ getCurrentWaitListSize() + " ";
data += "Maximum number of students that can be on the waitlist: "
+ getMaxStudentsWaitList() + " ";
data += "WaitList: " + Arrays.toString(getWaitList()) + " ";
return data;
}
//helper method to check if a student is in roster
private boolean isInRoster(Student student) {
for (Student s : getRoster()) {
if (s.equals(student)) {
return true;
}
}
return false;
}
//helper method to check if a student is in wait list
private boolean isInWaitList(Student student) {
for (Student s : getWaitList()) {
if (s.equals(student)) {
return true;
}
}
return false;
}
//method to add a student
public boolean addStudent(Student student) {
if (student.isTuitionPaid() && !isInRoster(student)
&& !isInWaitList(student)) {
//tution paid, not added in either lists
if (currentRosterSize < MAX_STUDENTS_ROSTER) {
roster[currentRosterSize] = student;
currentRosterSize++;
return true;
}
//if the control reach here, it means that the roster is full,
//adding to waitlist
if (currentWLSize < MAX_STUDENTS_WAITLIST) {
waitList[currentWLSize] = student;
currentWLSize++;
return true;
}
}
//couldnt add
return false;
}
//method to drop a student
public boolean dropStudent(Student student) {
if (isInRoster(student)) {
/**
* removing from roster
*/
for (int i = 0; i < getCurrentRosterSize(); i++) {
if (roster[i].equals(student)) {
// shifting other students left to occupy the vacant space
for (int j = i; j < getCurrentRosterSize() - 1; j++) {
roster[j] = roster[j + 1];
}
break;
}
}
//checking if wait list is not empty
if (currentWLSize > 0) {
//adding first person from WL to roster
roster[currentRosterSize - 1] = waitList[0];
//shifting elements in waitlist to remove vacant space
for (int j = 0; j < currentWLSize - 1; j++) {
waitList[j] = waitList[j + 1];
}
//decreasing waitlist size
currentWLSize--;
} else {
//wait list is empty, so decrementing roster size
currentRosterSize--;
}
return true;
}
if (isInWaitList(student)) {
/**
* removing from waitlist
*/
for (int i = 0; i < currentWLSize; i++) {
if (waitList[i].equals(student)) {
for (int j = i; j < currentWLSize - 1; j++) {
waitList[j] = waitList[j + 1];
}
currentWLSize--;
return true;
}
}
}
// not found
return false;
}
}
// Driver.java
import java.util.Scanner;
public class Driver {
// scanner to read user input
static Scanner scanner = new Scanner(System.in);
// prompts the user to enter student info, return a student object
static Student getStudentInput() {
System.out.print("Enter student's first name: ");
String fname = scanner.nextLine();
System.out.print("Enter student's last name: ");
String lname = scanner.nextLine();
System.out.print("Enter student's ID: ");
String id = scanner.nextLine();
System.out.print("Tution paid? (y/n): ");
String tutionPaid = scanner.nextLine();
// below statement will assign true to paid variable if the input is y
// or Y,else false
boolean paid = (tutionPaid.equalsIgnoreCase("y")) ? true : false;
Student s = new Student(fname, lname, id, paid);
return s;
}
// displays the menu and return the choice
static int showMenuReturnChoice() {
System.out.println("1. Add student");
System.out.println("2. Drop student");
System.out.println("3. View the course");
System.out.println("4. Exit");
int choice = Integer.parseInt(scanner.nextLine());
return choice;
}
public static void main(String[] args) {
// creating a course with max size of 5 for roster, 15 for waitlist
Course course = new Course(5, 15);
course.setCourseName("Computer Science");
int choice = 0;
// looping until user quits
while (choice != 4) {
// getting choice
choice = showMenuReturnChoice();
// performing actions based on choice
switch (choice) {
case 1:
Student s = getStudentInput();
if (course.addStudent(s)) {
System.out.println("Student added!");
} else {
System.out
.println("Student not added! "
+ "[already exists/not paid the tution fee/list is full]");
}
break;
case 2:
s = getStudentInput();
if (course.dropStudent(s)) {
System.out.println("Student dropped!");
} else {
System.out.println("Couldn't find the student to drop!");
}
break;
case 3:
System.out.println(course);
break;
case 4:
System.out.println("Bye!");
break;
default:
System.out.println("Invalid choice, try again!");
break;
}
}
}
}
// CourseAL.java
import java.util.ArrayList;
/**
* class similar to Course.java except that here we are using array lists instead of arrays
*/
public class CourseAL {
// attributes
private String courseName;
private ArrayList<Student> roster;
private ArrayList<Student> waitList;
private final int MAX_STUDENTS_ROSTER;
private final int MAX_STUDENTS_WAITLIST;
/**
* constructor taking parameters to initialize maximum roster size and
* waitlist size
*/
public CourseAL(int max_roster, int max_waitlist) {
MAX_STUDENTS_ROSTER = max_roster;
MAX_STUDENTS_WAITLIST = max_waitlist;
roster = new ArrayList<Student>();
waitList = new ArrayList<Student>();
}
// getter for course name
public String getCourseName() {
return courseName;
}
// setter for course name
public void setCourseName(String courseName) {
this.courseName = courseName;
}
// returns a non null list of students in roster
public ArrayList<Student> getRoster() {
return roster;
}
// returns a non null list of students in waitlist
public ArrayList<Student> getWaitList() {
return waitList;
}
// returns the maximum roster size
public int getMaxStudentsRoster() {
return MAX_STUDENTS_ROSTER;
}
// returns the max waitlist size
public int getMaxStudentsWaitList() {
return MAX_STUDENTS_WAITLIST;
}
// returns current number of students enrolled
public int getCurrentRosterSize() {
return roster.size();
}
// returns current number of students on waitlist
public int getCurrentWaitListSize() {
return waitList.size();
}
@Override
public String toString() {
// returning properly formatted string containing whole info
String data = "Course name: " + courseName + " ";
data += "Number of students enrolled: " + getCurrentRosterSize() + " ";
data += "Maximum number of students that can be enrolled: "
+ getMaxStudentsRoster() + " ";
data += "Roster: " + getRoster() + " ";
data += "Number of students on the waitlist: "
+ getCurrentWaitListSize() + " ";
data += "Maximum number of students that can be on the waitlist: "
+ getMaxStudentsWaitList() + " ";
data += "WaitList: " + getWaitList() + " ";
return data;
}
// method to add a student
public boolean addStudent(Student student) {
if (student.isTuitionPaid() && !roster.contains(student)
&& !waitList.contains(student)) {
// tution paid, not added in either lists
if (roster.size() < MAX_STUDENTS_ROSTER) {
roster.add(student);
return true;
}
// if the control reach here, it means that the roster is full,
// adding to waitlist
if (waitList.size() < MAX_STUDENTS_WAITLIST) {
waitList.add(student);
return true;
}
}
// couldnt add
return false;
}
// method to drop a student
public boolean dropStudent(Student student) {
if (roster.contains(student)) {
roster.remove(student);
if (!waitList.isEmpty()) {
roster.add(waitList.remove(0));
}
return true;
}
if (waitList.contains(student)) {
waitList.remove(student);
}
// not found
return false;
}
}
/*OUTPUT*/
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: Oliver
Enter student's last name: Queen
Enter student's ID: 100
Tution paid? (y/n): y
Student added!
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: Thea
Enter student's last name: Queen
Enter student's ID: 101
Tution paid? (y/n): y
Student added!
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: John
Enter student's last name: Diggle
Enter student's ID: 104
Tution paid? (y/n): y
Student added!
1. Add student
2. Drop student
3. View the course
4. Exit
3
Course name: Computer Science
Number of students enrolled: 3
Maximum number of students that can be enrolled: 5
Roster: [Oliver Queen (100), Thea Queen (101), John Diggle (104)]
Number of students on the waitlist: 0
Maximum number of students that can be on the waitlist: 15
WaitList: []
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: Iris
Enter student's last name: West
Enter student's ID: 109
Tution paid? (y/n): n
Student not added! [already exists/not paid the tution fee/list is full]
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: Felicity
Enter student's last name: Smoak
Enter student's ID: 107
Tution paid? (y/n): y
Student added!
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: Barry
Enter student's last name: Allen
Enter student's ID: 123
Tution paid? (y/n): y
Student added!
1. Add student
2. Drop student
3. View the course
4. Exit
3
Course name: Computer Science
Number of students enrolled: 5
Maximum number of students that can be enrolled: 5
Roster: [Oliver Queen (100), Thea Queen (101), John Diggle (104), Felicity Smoak (107), Barry Allen (123)]
Number of students on the waitlist: 0
Maximum number of students that can be on the waitlist: 15
WaitList: []
1. Add student
2. Drop student
3. View the course
4. Exit
1
Enter student's first name: Catyln
Enter student's last name: Snow
Enter student's ID: 189
Tution paid? (y/n): y
Student added!
1. Add student
2. Drop student
3. View the course
4. Exit
3
Course name: Computer Science
Number of students enrolled: 5
Maximum number of students that can be enrolled: 5
Roster: [Oliver Queen (100), Thea Queen (101), John Diggle (104), Felicity Smoak (107), Barry Allen (123)]
Number of students on the waitlist: 1
Maximum number of students that can be on the waitlist: 15
WaitList: [Catyln Snow (189)]
1. Add student
2. Drop student
3. View the course
4. Exit
2
Enter student's first name: Thea
Enter student's last name: Queen
Enter student's ID: 101
Tution paid? (y/n): y
Student dropped!
1. Add student
2. Drop student
3. View the course
4. Exit
3
Course name: Computer Science
Number of students enrolled: 5
Maximum number of students that can be enrolled: 5
Roster: [Oliver Queen (100), John Diggle (104), Felicity Smoak (107), Barry Allen (123), Catyln Snow (189)]
Number of students on the waitlist: 0
Maximum number of students that can be on the waitlist: 15
WaitList: []
1. Add student
2. Drop student
3. View the course
4. Exit
4
Bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.