Java. Course Registration Part 1- The Student Class (10 points) Write a class ca
ID: 3811431 • Letter: J
Question
Java. Course Registration
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 “X002")
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 and the student has paid their tuition, 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
also print the list of each student on a single line (use good object-oriented principles to access a text representation of each student!)
print an appropriate message if there are no students yet enrolled
Part 5- Driver Program (20 points)
For this project, you will not write an interactive driver. Instead, you will write a driver to demonstrate that your class works. To do this, write a driver program that does the following (in this order):
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 the 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 (it should succeed this time)
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. For full credit, take full advantage of the methods provided in the ArrayList class!
If you complete the extra credit, submit both Course and CourseAL.
Notes and Submission
You can add additional instance data variables or private, helper functions if necessary. If you include additional variables, include additional getters and setters as appropriate.
LOOK!!!!Here is the simple output from the course registration program
// PRINTS THE COURSE
Media Studies (5 student capacity)
// PRINTS THE ROSTER
--No students enrolled.--
//TRIES TO ADD THE SIXTH STUDENT AND PRINTS A MESSAGE IF ANY ADD FAILS
Farrah Fawcet (S956) could not be added.
//PRINTS THE ROSTER (BEFORE DROPPING 3RD STUDENT)
Enrollment: 5
Adam Ant (S925)
Bob Barker (S713)
Chevy Chase (S512)
Doris Day (S513)
Emilio Estevez (S516)
// PRINTS THE ROSTER (AFTER DROPPING 3RD STUDENT)
// NOTE: NO "null"s ARE PRINTED
Enrollment: 4
Adam Ant (S925)
Bob Barker (S713)
Doris Day (S513)
Emilio Estevez (S516)
// PRINTS THE ROSTER (AFTER ADDING 6TH STUDENT)
// NOTE: YOUR ORDER MIGHT BE DIFFERENT- THAT'S OKAY
Enrollment: 5
Adam Ant (S925)
Bob Barker (S713)
Doris Day (S513)
Emilio Estevez (S516)
Farrah Fawcet (S956)
//your output does not have to match this exactly in terms of wording, but the general output should be similar.
Explanation / Answer
public class Student {
private String m_Name;
private String m_ID;
boolean m_FeesPaid;
Student(String name, String id, boolean feespaid)
{
m_Name = name;
m_ID = id;
m_FeesPaid = feespaid;
}
public void setName(String name)
{
m_Name = name;
}
public void setID(String ID)
{
m_ID = ID;
}
public void setFeesPaid(boolean flag)
{
m_FeesPaid = flag;
}
public String getName()
{
return m_Name;
}
public String getID()
{
return m_ID;
}
public boolean getFeesPaid()
{
return m_FeesPaid;
}
public String toString()
{
return " Name is: "+m_Name + ", Id is: "+m_ID +" & Fees paid is: "+m_FeesPaid;
}
}
public class Course {
private String m_course_name;
private Student Roster[];
private int m_max_students;
private int m_index = 0;
private int count = 0;
Course(String course_name, int max_students, Student obj)
{
m_course_name = course_name;
m_max_students = max_students;
if(m_index==0)
Roster=new Student[max_students];
Roster[m_index++]=obj;
}
public boolean addStudent(Student s)
{
// if there is space and student has paid the fees, add the student to roster and return true
if(m_index<m_max_students && s.m_FeesPaid==true)
{
Roster[m_index++] = s;
count++;
return true;
}
else
return false;
}
public boolean dropStudent(Student s)
{
// if student is in roster, remove from roster
int index = 0;
while(index<m_max_students)
{
if(Roster[index]==s)
{
count--;
int left = index;
int right = index+1;
// remove object and restructure array elements copy index+1 to index [0,1,2,3,4,5,6]
while(left<count)
{
Roster[left] = Roster[right];
left++;
right++;
}
return true;
}
}
return false;
}
public void printRoster()
{
if(count==0)
{
System.out.println("Currently no student is enrolled in this course ....");
}
else{
// print students enrolled in course, iterate over Roster array and print name
System.out.println("Number of students enrolled in course are: " +count);
for(int i=0; i<count;i++)
{
System.out.print(Roster[i].getName());
}
}
}
public void setCourseName(String course_name)
{
m_course_name = course_name;
}
public void setMaxStudents(int max_students)
{
m_max_students = max_students;
}
public String getsetCourseName()
{
return m_course_name;
}
public int getMaxStudents()
{
return m_max_students;
}
public String toString()
{
return " Course Name is: "+m_course_name + " & Max student count is: "+m_max_students;
}
}
public class Driver {
public static void main(String agrs[])
{
Student obj = new Student("Manish", "E3001", true);
System.out.println(obj.toString());
Course obj2 = new Course("Maths", 100, obj);
System.out.println(obj2.toString());
System.out.println("Name is: "+obj.getName());
System.out.println("Id is: "+obj.getID());
System.out.println("Fees paid is: "+obj.getFeesPaid());
obj2.addStudent(obj);
obj2.printRoster();
obj2.getMaxStudents();
System.out.println(obj2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.