This discussion is archived
1 Reply Latest reply: Dec 3, 2012 3:05 AM by TPD-Opitz-Consulting-com RSS

clear() and dropStudent()

974869 Newbie
Currently Being Moderated
The problem I am faced with is to create a clear() method that will will remove all students from the course.
And to create a dropStudent() method. (Self explanatory)

Below I have implemented a clear() method but I've split up the syntax in the test class file by putting a while loop there. I technically want to do all of my calculations inside the method but I can't really figure out how I would do that. This displays there being 0 students which works but I just wanted some advice on putting this just in the clear() method.

Also I have NOT implemented the dropStudent() method & that's frankly because I'm not sure how I would go about this. So I'm really looking for suggestion on the logic here. So far as my logic goes with the dropStudent() method I've thought that I need to make some sort of comparison using .equalsIgnoreCase(student) so something like this:

if(stuidents.equalsIgnoreCase(student))
               {
                    indexOfStudentToDrop = i;




Here is my test class file
public class Program7Client {
    public static void main(String[] args){
        Course course1 = new Course("CS201");
        Course course2 = new Course("CS202");
        Course course3 = new Course("CS276");
        //hard coded students into course 1 & 2.
        course1.addStudent("Batman");
        course1.addStudent("Steve Jobs");
        course1.addStudent("Spider Man");
        course1.addStudent("Rick Astley");
        course1.addStudent("Rebecca Black");
        course2.addStudent("Melissa Rycroft");
        course2.addStudent("Tony Dovolani");
        course2.addStudent("Josh Martin");
        course2.addStudent("Walt Disney");
        course2.addStudent("Bill Gates");
        //for loop to fill up course 3 by calling addStudent which will take user input. 
        System.out.println("Please enter the name of the students you wish to add: ");
        for(int i = 0; i < 7; i++){
            course3.addStudent();          
        }
        //changes the name of one of the courses.
        course2.setCourseName("CS9000");
        
        //displays the number of courses.
        System.out.println();
        System.out.println("The number of courses is: " + Course.getNumberOfCourses());
        System.out.println();
        
        //display the roster.
        System.out.println(course1.getCourseName());
        course1.printRoster();
        System.out.println();
        System.out.println(course2.getCourseName());
        course2.printRoster();
        System.out.println();
        System.out.println(course3.getCourseName());
        course3.printRoster(); 
        System.out.println();
        
        *while(!course1.empty()){*
            *course1.clear();*
        *}*   
        *System.out.println("The number of students in course1 is: " + course1.getNumberOfStudents());*
    }
}
Here is my class file
public class Course {
    private String courseName;
    //changed array size to test so it will adjust as needed.
    private String[] students = new String[5];
    private int numberOfStudents = 0;
    private static int numberOfCourses = 0;
    
    public Course(String courseName){       
        this.courseName = courseName;
        numberOfCourses++;
    }
    public String[] addStudent(String student){
        String[] temp = new String[students.length * 2];
        if(numberOfStudents == students.length){           
            System.arraycopy(students, 0, temp, 0, students.length);
            students = temp;
        }
        students[numberOfStudents] = student;
        numberOfStudents++;
        return temp;
    }
    public String[] getStudents(){
        return students;
    }
    public int getNumberOfStudents(){
        return numberOfStudents;
    }
    public String getCourseName(){
        return courseName;
    }
    //public void dropStudent(String student){
       // int indexOfStudentToDrop = -1;
          //for(int i = 0; i < numberOfStudents; i++){
          //     if(students.equalsIgnoreCase(student)){
          //          indexOfStudentToDrop = i;
          //          if (indexOfStudentToDrop != -1){
          //               for (i = indexOfStudentToDrop; i < numberOfStudents; i ++){
          //                    
          //                    students[i]= students[i+1];
// }
          //          }// End if found.
          //          numberOfStudents --;
          //     }// End if equal.     
          //}// End outer for. */
          //students.remove(student);
//}
public static int getNumberOfCourses(){
return numberOfCourses;
}
public void displayNumberOfCourses(){
System.out.println("The number of courses is:" + numberOfCourses);
}
public void displayNumberOfStudents(){
System.out.println("Total students enrolled: " + numberOfStudents);
}
public void printRoster(){
//String[] student = new String[5];
for(int i = 0; i < getNumberOfStudents(); i++){
System.out.println(students[i]);
}
displayNumberOfStudents();
}
public void setCourseName(String newCourseName){
courseName = newCourseName;
numberOfCourses++;
}
Scanner input = new Scanner(System.in);
public String[] addStudent(){
String[] temp = new String[students.length * 2];
if(numberOfStudents == students.length){          
System.arraycopy(students, 0, temp, 0, students.length);
students = temp;
}
String studentName;
studentName = input.next();
students[numberOfStudents] = studentName;
numberOfStudents++;
return temp;
}
*public String clear(){*
*return students[--numberOfStudents];*
*}*
*public boolean empty(){*
return numberOfStudents == 0;
*}*



Thanks for all help/suggestions! Btw hope whoever is reading this is having a good weekend :)

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points