2

Code;

public Course(Professor professor, String courseName) {
    this.professor = professor;
    this.courseName = courseName;
    this.students = new Student[100];
}

By my understanding since the array is initialized its contents are null. I'm trying to write a method called

public boolean enroll(Student s){...}

in order to replace the null values with the names of students that are successfully enrolled in the course. Is this possible without the use of lists?

3
  • 1
    It would be better to use an ArrayList which can grow as needed. To implement it with an array, though, you just need to keep track of numStudents so that you know what index to put a new student at. Commented Mar 11, 2017 at 0:17
  • 2
    The array is not null, but the objects in the array are null. The array is initialized with new Student[100]. Commented Mar 11, 2017 at 0:18
  • 2
    "since the array is only declared and not initialized" But it is initialized - the array is not null, the entries are. Yes you can do what you are asking students[i] = new Student(); Commented Mar 11, 2017 at 0:18

3 Answers 3

6

The array is initialized and is not null, however the entries of the array are null. It is possible without the use of a list (but it would be simpler). You need to store additonal information, how many students are enrolled.

int numberOfStudents = 0;

public boolean enroll(Student student) 
{
  if(numberOfStudents >= 100) return false;

  students[numberOfStudents] = student;
  numberOfStudents++;
  return true;
}

You also need to consider what should happen when there are more than 100 students?

Thats basically what an ArrayList does with the advantage that it will automatically grow the array, if there is too much data. If you know in advanced how many entries there will be, you can use ArrayList(int initialCapacity) to avoid unneccessary copying:

List<Student> students = new ArrayList<Student>(100);

public boolean enroll(Student student) 
{
  return students.add(student);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah this was my first method but since the homework is graded via machine, I have to use a standard array for it to count. Thank you for your solution!
5

The array is actually initialized, to 100 null Student objects, indexed 0 to 99.

So, assuming that the enroll() method is part of the Course class, returning true whenever it stores a non-null Student object, it can be implemented as follows:

public boolean enroll(Student s) {
  if ((s == null) || (this.counter >= 100)) {
    return false;
  }
  this.students[this.counter] = s;  
  ++this.counter;
  return true;
}

where this.counter is an integer initialized to 0 in the constructor, after the students array is initialized.

A better approach would have been to use an ArrayList instead of an array, to avoid having to check the size every time and have no maximum size limitation. In that case, this.students would be initialized as

List<Student> students;
...
this.students = new ArrayList<Student>();

and the implementation of enroll() would be simplified to

public boolean enroll(Student s) {
  if (s == null) {
    return false;
  }
  this.students.add(s);  
  return true;
}

without the need for a counter.

Comments

1

You can enroll a student in two ways:

  • Use ArrayList instead of array, by this way, you won't need to keep track of last entry or iterate through an array every time enroll is called, e.g.:

    public Course(Professor professor, String courseName) { this.professor = professor; this.courseName = courseName; this.students = new ArrayList<Student>(); }

    public void enroll(Student s){ this.students.add(s); }

  • Use array and iterate through it to find out the last null location, e.g.:

    public void enroll(Student s){ if(null == s) return; for(int i=0 ; i<this.students.length ; i++){ if(this.students[i] == null){ this.students[i] = s; return; } } }

In this method, you will also keep track of whether all 100 students have been enrolled already.

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.