little help for java newbie please!!
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:
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
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)
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 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
public class Course {
private int currentEnrollment;
private int maxNumOfStudents;
private String courseName;
private Student[] Roster;
public Course(String courseName, int maxNumOfStudents)
{
this.courseName = courseName;
this.maxNumOfStudents =maxNumOfStudents;
Roster = new Student[maxNumOfStudents];
currentEnrollment =0;
}
public int getCurrentEnrollment()
{
return currentEnrollment;
}
public String getCourseName()
{
return courseName;
}
public void setCourseName(String courseName)
{
this.courseName = courseName;
}
public int getNumOfStudents()
{
return maxNumOfStudents;
}
public void setNumOfStudents(int maxNumOfStudents)
{
this.maxNumOfStudents = maxNumOfStudents;
}
public String toString(){
String s = courseName+" ,"+maxNumOfStudents+" Student capacity";
return s;
}
public void printRoster(){
for(int student =0; student< currentEnrollment; student++){
System.out.println(Roster[student]);
}
}
public boolean addStudent(Student s){
if(currentEnrollment < maxNumOfStudents){
for( int i=0; i< maxNumOfStudents; i++){
Roster[i]= s;
System.out.println(Roster[i]);
return true;
}
}else
System.out.println(s+ "Can not be added.");
printRoster();
return false;
}
public boolean dropStudent(Student s){
Roster[2]=s;
printRoster();
return false;
}
return true;
}
return false;
}
}
import java.util.Scanner;
public class StudentTester {
public static void main(String [] args){
Course subject =new Course("Media Studies",5);
Student[]inputArray = new Student[5];
inputArray[0]= new Student("Adam Ant","(S925)");
inputArray[1]= new Student("Bob Barker","(S713)");
inputArray[2]= new Student("Chevy Chase","(S512)");
inputArray[3]= new Student("Doris Day","(S513)");
inputArray[4]= new Student("Emilio Estevez","(S516)");
inputArray[5]= new Student("Farrah Fawcet","(S956)");
subject.printRoster();
for(int i=0; i
while(inputArray[2].equals("Chevy Chase"))
{
subject.dropStudent(inputArray[i]);
subject.printRoster();
}
}
}
}
I am sort of self learner, but I can't find any examples for this kind of problem in book. Any help will be highly appreciated. Thank you !!
where do you need help? if you can tell us where you are having problem, you will definitely get some hints on how to tackle it.
thank you!! I am trying to get help in methods.Mainly in the dropStudent method. I need to drop the third student and add the sixth student. I am glad that at least you replied :)
You can not just remove any element from array in java . The closest thing you can do is put some recognizable value there in that index like like 999 etc . Or you can use list if that is permissible. if you just want to drop third student and add sixth student, you can just add/put that sixth student in the index of third student, which will replace the third student.
Your dropStudent does not look good. You are passing student object, so you will need to loop through the array, find the index of that student in that array and then act on that index, probably set null so that you can identify it later.
Please log in to reply to this post
You can also log in using your Facebook