I\'m working on this program, but i have some troubles.Hope someone can help me.
ID: 3671932 • Letter: I
Question
I'm working on this program, but i have some troubles.Hope someone can help me.
Write a Student and Course class that might be part of a course registration system.
A student is described by:
first name
last name
student ID (as text)
whether or not tuition is paid
A course is described by:
a name
the maximum number of students that can be enrolled in the class
a roster of students enrolled in the course (stored as a Student[])
Student Class (10 points)
In your class, include:
instance data variables
a constructor
getters and setters
a toString method
Course Class (40 points)
In your class, include
instance data variables
a constructor
a course is initially created with no students on the roster
getters and setters
this about which variables should have setters
include validity checks where appropriate
a toString method
include the name, number of students enrolled in the course, maximum number that can be enrolled, and a printed roster as part of the text representation
make sure that the roster does not print any "nulls"
an addStudent method
method header: public boolean addStudent(Student student)
if there is room for a student and the student has paid their tuition, add the student to the roster
a dropStudent method
method header: public boolean dropStudent(Student student)
if the student is on the roster, remove them
Write a class called CourseAL. This class has the same methods as Course. Instead of using a Student[] to store the roster, use an ArrayList<Student>. For full credit, take advantage of the methods in the ArrayList class to implement the methods.
Test Code for these class:
public class CourseDriver {
public static void main(String[] args) {
Student[] studentsInSchool = new Student[7];
studentsInSchool[0] = new Student("Adam", "Ant", "S925", true);
studentsInSchool[1] = new Student("Bob", "Barker", "S713", false);
studentsInSchool[2] = new Student("Chevy", "Chase", "S512", true);
studentsInSchool[3] = new Student("Doris", "Day", "S513", true);
studentsInSchool[4] = new Student("Emilio", "Estevez", "S516", false);
studentsInSchool[5] = new Student("Farrah", "Fawcet", "S956", true);
studentsInSchool[6] = new Student("Greta", "Garbo", "S419", true);
Course course = new Course("Media Studies", 5);
/* note: to test the extra credit, replace the line above with the line below.
* the rest of the program should run exactly the same.
*
* CourseAL course = new CourseAL("Media Studies", 5);
*
*/
System.out.println("******Testing toString: should print name, max enrollment, " +
"and a message that the roster is empty.*****");
System.out.println(course);
System.out.println("******Testing add method: should add Adam, Chevy, Doris, Farrah, and Greta. " +
"Should not add Bob or Emilio.*****");
for(int i=0; i<studentsInSchool.length; i++) {
Student currentStudent = studentsInSchool[i];
if(course.addStudent(currentStudent)) {
System.out.println(currentStudent + " added.");
} else {
System.out.println(currentStudent + " was not added.");
}
}
System.out.println();
System.out.println("******Testing toString and add methods: should print name, max enrollment, " +
"number enrolled, and the roster.*****");
System.out.println(course);
System.out.println("******Testing drop and toString methods: no changes should be made when " +
" trying to drop a student not in the course");
course.dropStudent(studentsInSchool[4]);
System.out.println(course);
System.out.println("******Testing drop and toString methods: Chevy should be dropped");
course.dropStudent(studentsInSchool[2]);
System.out.println(course);
System.out.println("******Testing add, drop, and toString methods: Bob should now be added " +
" because he paid tuition and now there is room");
studentsInSchool[1].setTuitionPaid(true);
course.addStudent(studentsInSchool[1]);
System.out.println(course);
}
}
I really appreciate it!
Explanation / Answer
import java.io.*;
import java.util.*;
public class Student
{
private String fname;
private String lname;
private int SId;
private boolean tutionPaid;
public Student(String n1, String n2, int studentId, boolean paidOr)
{
fname=n1;
lname=n2;
SId=studentId;
tutionPaid=paidOr;
}
public String getFname()
{
return fname;
}
public String getLname
{
return lname;
}
public int getId()
{
return SId;
}
public boolean getPaidStatus()
{
return tutionPaid;
}
public void setFname(String newFname)
{
fname= newFname;
}
public void setLname(String newLname)
{
lname=newLname;
}
public void setId(String newSid)
{
SId =newSid;
}
public void setStatus(boolean newTutuionStatus)
{
tutionPaid= newTutionStatus;
}
public String toString()
{
String output=" ";
output=fname+" "+lname+" with id"+SId+"have status of tution fee paid or not as"+tutionPaid;
return output;
}
} //end of the student class
public class Course
{
private String cname;
private int capacity;
private Student[] students= new Student[50];
public Course(String n, int cap)
{
cname= n;
capacity=cap;
}
public String getCname()
{
return cname;
}
public int getCapacity()
{
return capacity;
}
public void setCname(String newCname)
{
cname=newCname;
}
public void setCapacity(int newCap)
{
capacity=newCap;
}
public String toString()
{
String output1=" ";
output1= cname+"("+capacity+"students) ";
for(int i=0;i<capacity;i++)
{
output1+= "("+(i+1)+")"+students[i]+" ";
}
return output1;
}
public boolean addStudent(Student student)
{
boolean flag;
if(getCapacity() != capacity && Student.getPaidStatus()== "yes")
{
student[capacity]= student;
flag=1;
}
else
flag =0; // don't add student as roaster is full or student has not paid the tution fee.
return flag;
} //end of add method
public boolean dropStudent(Student student)
{
boolean flag2;
int indexToDrop;
for(int i=0;i<capacity;i++)
{
if(student[i].equalsIgnoreCase(student) || student[i].getSid() ==student.SId)
{
indexTodrop=i;
if(indexTodrop != -1)
{
for(int i=indexTodrop; i<capacity;i++)
{
students[i]= students[i+1];
}
}
}
}
}
} // end of class Course
public class CourseAL
{
private String clname;
private int capacityL;
private ArrayList students;
public CourseAL(String n, int cap)
{
clname= n;
capacityL=cap;
}
public String getClname()
{
return clname;
}
public int getCapacityL()
{
return capacityL;
}
public void setClname(String newClname)
{
clname=newClname;
}
public void setCapacityL(int newCapL)
{
capacityL=newCapL;
}
public String toString()
{
String output2=" ";
output2= clname+"("+capacityL+"students) ";
for(int i=0;i<capacityL;i++)
{
output2+= "("+(i+1)+")"+students.get(i)+" ";
}
return output1;
}
public boolean addStudent(Student student)
{
boolean flag;
if(getCapacity() != capacity && Student.getPaidStatus()== "yes")
{
students.add(student);
flag=1;
}
else
flag =0; // don't add student as roaster is full or student has not paid the tution fee.
return flag;
} //end of add method
public ArrayList getStudents()
{
return students;
}
public boolean dropStudent(Student student)
{
boolean flag2;
int indexToDrop;
for(int i=0;i<capacity;i++)
{
if(student[i].equalsIgnoreCase(student) || student[i].getSid() ==student.SId)
{
indexTodrop=i;
if(indexTodrop != -1)
{
for(int i=indexTodrop; i<capacity;i++)
{
students.remove(student);
}
}
}
}
}
}
Class Test
{
public static void main(String [] args)
{
Student s=new Student();
Course c1=new Course();
CourseAL c2=new CourseAL();
//take input from users and call the methods of the class using the objects created above
}
}