0

Here's example:

 public class Course {
        private String name;
        private Student[] students;
        private int capacity=40;
        private int numberOfStudents;

        public Course(String name){
            this.name=name;
        }

        public Course(String name, int capacity){
            this.name= name;
            this.capacity= capacity;
        }

        public int getNumberOfStudents(){
            return numberOfStudents;
        }

        public String getCourseName(){
            return name;
        }

        public Student[] getStudents(){
            return students;
        }

        public boolean addStudents(Student newStudent){
            if(numberOfStudents < capacity){    
                students[numberOfStudents++] = newStudent;
                return true;
            }
            return false;
        }
    }

I'm trying to add a new student to the Student[] students array. I wrote the code above. In the Student class, every student has a unique id.

The problem is that while I am adding newStudent, I want check if newStudent already exists in the class. To do that I should use id property of students because every student has its own unique id. How can I add it to do if statement?

4
  • 4
    I really recommend using a HashMap instead of an array for this. Commented Dec 27, 2013 at 17:05
  • 1
    Didn't I see this exact post the other day? Why are you reposting it? Commented Dec 27, 2013 at 17:06
  • Is this a homework assignment? If it's not you should use David Wallace's advise. Commented Dec 27, 2013 at 17:11
  • If you want to do it efficently, you should use a HashSet to store the ids that are currently in the array. When you add a new student, just make sure his id is not in the set. Set look up O(1) so it is really faster than looping through the array especially if the array is large! Commented Dec 27, 2013 at 17:21

2 Answers 2

3

You need to use a loop to loop through the students. Something like this. The loop checks if the students exists. If it does, then the method will return false without adding a student.

 public boolean addStudents(Student newStudent){
     for (Student student : students){
         if (student.getID() == newStudent.getId()){
             return false;
         }
     }
     if(numberOfStudents < capacity){    
         students[numberOfStudents++] = newStudent;
         return true;
     }
     return false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

id is double so .equals gave an error. Instead of that i wrote "==" and it works. Thank you.
To accept an answer, you just click the check mark. Also I noticed you haven't accepted an past answers. You should do that is an answer helps you solve a problem.
1

If Student correctly overrides equals (and, as a good practice, hashCode) you can do the following:

public boolean addStudents(Student newStudent){
    if(numberOfStudents < capacity && isNew(newStudent)){    
        students[numberOfStudents++] = newStudent;
        return true;
    }
    return false;
}

public boolean isNew(Student student) {
   for (int i = 0; i < numberOfStudents; i++) {
       if (students[i].equals(student)
           return false;   
   }
   return true;
}

Overridden equals method on student:

public boolean equals(Object obj) {
    if (obj == null)
        return false;
    if (obj == this)
        return true;
    if (!(obj instanceof Student))
        return false;

    Student s = (Student) obj;
    return getID() == s.getID() 
        || (getID() != null && getID.equals(s.getID())) // skip if id is a primitive
}

You can also replace the use of acessors (getID() == s.getID()) calls with the use of properties since your Student class will have access to private properties (id == s.id).


If you have a lot of students per class I would follow @David advice and use a HashMap or similar data structure so that you don't need to loop through all of the class students (O(n)) to find out if you are adding a new student.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.