Need help with Java Part 1- The Student Class (10 points) Write a class called S
ID: 3853790 • Letter: N
Question
Need help with Java
Part 1- The Student Class (10 points)
Write a class called Student. A student is described by:
a name
an ID (text-based, such as "A146")
whether or not tuition is paid
Write a constructor, getters and setters, and a toString method for this class.
Part 2- The Course Class (10 points)
Write a class called Course to represent a college class. A course is described by:
a name
the maximum students that can be enrolled in the course
Write a constructor, getters and setters (using appropriate validity checks), and a toString method.
Part 3- The Roster (10 points)
Add a roster to the Course class. A roster is represented by an array of Student objects.
Add instance data and code to the constructor to represent the roster.
(You do not need to add a getter and setter. The methods below will allow an outside class to access the roster appropriately.)
Part 4- Course Methods (50 points)
Write the following 3 methods in the Course class:
public boolean addStudent(Student s)
if there is room for the student, add the student to the roster and return true
return false otherwise
public boolean dropStudent(Student s)
if the student is currently in the roster, remove them from the roster and return true
return false otherwise
Note: There are different approaches to implementing this method. Any approach is fine, but be sure to test your method thoroughly considering lots of different possible cases. (Use your driver program for this!)
public void printRoster()
print how many students are enrolled in the course; print an appropriate message if there are no students yet enrolled
print the list of each student on a single line (use good object-oriented principles to access a text representation of each student!)
Part 5- Driver Program (20 points)
Write a driver program that does the following (in this order). Note that your driver program is not interactive with the user.
creates six students
creates a course that can hold five students
prints the course
prints the roster
adds the first five students to the course
tries to add the sixth student and prints a message if any add fails
prints the roster
drops the third student from the course
prints the roster
tries again to add the sixth student to the course
prints the roster
Extra Credit (10 points)
Write a class called CourseAL. This class has the same methods as Course. But, instead of using a Student[] to store the roster, use an ArrayList<Student>. Re-write the Course methods to work with the ArrayList. Take advantage of the methods provided in the ArrayList class!
If you complete the extra credit, submit Course and CourseAL.
Explanation / Answer
Here is the code for the question , including extra credit. To test the extra credit CourseAL class, in the driver program, replace Course by CourseAL and rest of the code should work as usual.
Post a comment in case of any issues, I shall respond. If happy with the answer, please rate it. Thank you.
Student.java
public class Student {
private String name;
private String ID;
private boolean paidTuition;
public Student(String name)
{
this.name = name;
this.ID = "";
paidTuition = false;
}
public Student( String id, String name, boolean paid)
{
this.name = name;
this.ID = id;
this.paidTuition = paid;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
public boolean hasPaidTuition() {
return paidTuition;
}
public void setPaidTuition(boolean paidTuition) {
this.paidTuition = paidTuition;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString()
{
return "ID: " + ID + " Name: " + name + " Tuition Fee: " + paidTuition;
}
}
Course.java
public class Course {
private String name;
private int maxStudents;
private Student roaster[];
private int numEnrolled;
public Course(String name, int max)
{
this.name = name;
this.maxStudents = max;
roaster = new Student[maxStudents];
numEnrolled = 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMaxStudents() {
return maxStudents;
}
public void setMaxStudents(int maxStudents) {
this.maxStudents = maxStudents;
if(maxStudents < 0)
this.maxStudents = 0;
}
public boolean addStudent(Student s)
{
if(numEnrolled < maxStudents)
{
roaster[numEnrolled] = s;
numEnrolled++;
return true;
}
else
return false;
}
public boolean dropStudent(Student s1)
{
int index = -1;
for(int i = 0; i < numEnrolled; i++)
{
Student s2 = roaster[i];
if(s1.getID() == s2.getID()) //same ids , then its a match
{
index = i;
break;
}
}
if(index != -1) //i.e. student was found, index is not -1
{
//move up all the students
for(int i = index; i < numEnrolled - 1; i++)
{
roaster[i] = roaster[i+1];
}
numEnrolled --;
return true;
}
else
return false;
}
public void printRoaster()
{
if(numEnrolled == 0)
{
System.out.println(" No students enrolled yet for course " + name);
return;
}
System.out.println(" "+numEnrolled + " students are enrolled for course " + name);
System.out.printf(" %-10s %-20s %-10s", "ID", "Name", "Tuition Fee" );
System.out.printf(" %-10s %-20s %-10s", "==", "====", "===========" );
Student s;
for(int i = 0; i < numEnrolled; i++)
{
s = roaster[i];
System.out.printf(" %-10s %-20s %-10s", s.getID(), s.getName(),
s.hasPaidTuition()? "Paid" : "Not Paid");
}
}
public String toString()
{
return "Course Name: "+ name + " " + "Max : " + maxStudents;
}
}
Driver.java
public class Driver {
public static void main(String[] args) {
//create 6 students
Student s1 = new Student("A111", "John",true);
Student s2 = new Student("A222", "Alice",true);
Student s3 = new Student("A333", "Peter",false);
Student s4 = new Student("A444", "Henry",true);
Student s5 = new Student("A555", "Michael",false);
Student s6 = new Student("A666", "Bob",true);
//create a course that can hold 5 students
Course compSci = new Course("Computer Science", 5);
System.out.println(compSci);
compSci.printRoaster();
//add 5 students
compSci.addStudent(s1);
compSci.addStudent(s2);
compSci.addStudent(s3);
compSci.addStudent(s4);
compSci.addStudent(s5);
if(compSci.addStudent(s6))
System.out.println(s6.getName() + " was added to " + compSci.getName());
else
System.out.println(s6.getName() + " was not added to " + compSci.getName());
compSci.printRoaster();
//drop 3rd student and print roaster
compSci.dropStudent(s3);
compSci.printRoaster();
//add 6th student and print roaster
compSci.addStudent(s6);
compSci.printRoaster();
}
}
CourseAL.java (extra credit)
import java.util.ArrayList;
public class CourseAL {
private String name;
private int maxStudents;
private ArrayList<Student> roaster;
public CourseAL(String name, int max)
{
this.name = name;
this.maxStudents = max;
roaster = new ArrayList<Student>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMaxStudents() {
return maxStudents;
}
public void setMaxStudents(int maxStudents) {
this.maxStudents = maxStudents;
if(maxStudents < 0)
this.maxStudents = 0;
}
public boolean addStudent(Student s)
{
if(roaster.size() < maxStudents)
{
roaster.add(s);
return true;
}
else
return false;
}
public boolean dropStudent(Student s1)
{
for(int i = 0; i < roaster.size(); i++)
{
Student s2 = roaster.get(i);
if(s1.getID() == s2.getID()) //same ids , then its a match
{
roaster.remove(i);
return true;
}
}
return false;
}
public void printRoaster()
{
if(roaster.size() == 0)
{
System.out.println(" No students enrolled yet for course " + name);
return;
}
System.out.println(" "+roaster.size() + " students are enrolled for course " + name);
System.out.printf(" %-10s %-20s %-10s", "ID", "Name", "Tuition Fee" );
System.out.printf(" %-10s %-20s %-10s", "==", "====", "===========" );
Student s;
for(int i = 0; i < roaster.size(); i++)
{
s = roaster.get(i);
System.out.printf(" %-10s %-20s %-10s", s.getID(), s.getName(),
s.hasPaidTuition()? "Paid" : "Not Paid");
}
}
public String toString()
{
return "Course Name: "+ name + " " + "Max : " + maxStudents;
}
}
output
Course Name: Computer Science Max : 5
No students enrolled yet for course Computer Science
Bob was not added to Computer Science
5 students are enrolled for course Computer Science
ID Name Tuition Fee
== ==== ===========
A111 John Paid
A222 Alice Paid
A333 Peter Not Paid
A444 Henry Paid
A555 Michael Not Paid
4 students are enrolled for course Computer Science
ID Name Tuition Fee
== ==== ===========
A111 John Paid
A222 Alice Paid
A444 Henry Paid
A555 Michael Not Paid
5 students are enrolled for course Computer Science
ID Name Tuition Fee
== ==== ===========
A111 John Paid
A222 Alice Paid
A444 Henry Paid
A555 Michael Not Paid
A666 Bob Paid