Programme de liste pour java


Bonjour, j'ai une question sur la façon de résoudre ce programme. J'ai fait les grandes lignes de cela, mais j'ai besoin d'aide pour le compléter. Merci à vous tous pour les commentaires!

Problème.

Un objet de la classe Roster représente une section particulière d'un cours. Une liste a un tableau de students, un int numStudents, qui dit combien d'étudiants il y a dans la section, un {[4] } qui donne le nombre maximum d'étudiants qui peuvent être dans la section, et un {[5] } qui dit quel cours cette liste est un la section de.

Notez que les étudiants ne sont pas dans un ordre particulier dans le tableau, mais ils seront dans les premiers éléments numStudents du tableau.

Code:

public class Roster {
    Student [ ] students;
    int numStudents;
    int stopPoint;
    Course course;

    /**
     * The constructor for this class.
     * Initialize this roster so that it is empty, i.e., holds no students,
     *  but so that it can hold up to stopPoint students
     *   and so that it has the given stop point and course
     */
    public Roster(int stopPoint, Course course){
    this.stopPoint = stopPoint;
    this.course = course;
    this.students = new Student [stopPoint];
    }

    /**
     * toString is a method every class has.  It returns a string 
     * that represents the object for printing
     */
    public String toString( ){
    String res = "";
    for(int j = 0; j < numStudents; j++){
        res = res + "\n" + students[j].toString();
    }
    return course + " " + numStudents + "/" + stopPoint+res;
    }

    /**
     * isFull returns true if and only if the number of students in it is 
     *   at the stopPoint
     */
    public boolean isFull( ){
    return false;   // replace this line with your code
    }

    /**
     * add given student to this roster
     * if student already on roster or numStudents already == stopPoint, 
     *   do not change roster and return false
     * worst case O(1) - add the new Student at the end of the array
     * @return true if successful, else false
     */
    public boolean addStudent(Student student){

    return false; // replace this line with your code

    }


    /**
     * returns true if and only if the student is on this roster.
     */
    public boolean findStudent(Student student){

    return false; // replace this line with your code

    }

    /**
     * Remove given student from this roster. 
     * If student is not on this roster do not change roster and return           false
     * @return true if successful, else false
     */
    public boolean dropStudent(Student student){

    return false; // replace this line with your code

    }

}
Author: Vladimir Vagaytsev, 2016-07-24

1 answers

Je suggère que vous utilisiez une liste au lieu d'un tableau pour enregistrer les étudiants, puis le reste des méthodes que vous avez déléguerait simplement à cette liste.

public Roster(int stopPoint, Course course){
this.stopPoint = stopPoint;
this.course = course;
this.students = new ArrayList<Students>(stopPoint);
}


public String toString( ){
String res = "";
for(Student s: students){
    res = res + "\n" + s.toString();
}
return course + " " + numStudents + "/" + stopPoint+res;
}


public boolean isFull( ){
return students.size() == stopPoint;
}


public boolean addStudent(Student student){
if(!student.contains(student) && !isFull()){
    students.add(student);
    return true;    
}
    return false;

}

public boolean findStudent(Student student){
return students.contains(student);
}

public boolean dropStudent(Student student){
return students.remove(student); 
}
 0
Author: yamenk, 2016-07-24 18:44:35