0

Good day. This is a homework question. I am required to create a course object using the constructor by using a course name. Students can be added by using addStudent (String student) method and return all the students for that course using getStudents() method. My test program requires me to display the number of student and students name for that courses as shown below: Number of students in Programming II course: 6

  1. Siti Aminah Muhammad
  2. Halim Zainal Abidin
  3. Jason Lim
  4. ...
  5. ... Number of Students in Database Systems course: 15
  6. Fatimah Ahmad
  7. Sarah Goh ...

However, when I run my test program, the names are output as null and I can't figure out why. Below is my code:

public class Course {
    private String name; 
    private int numberOfStudents; 
    private String[] students = new String [100];
    
    
    public Course() {
    }
    
    public Course(String name) {
        this.name = name;
    }
    
    public void addStudent(String student) {
        students [numberOfStudents] = student;
    }
    
    
    public String[] getStudents() {
        return students;
    }
    
    public int getNumberOfStudents() {
        return numberOfStudents;
    }
    
    public void setNumberOfStudents(int NumberOfStudents) {
        this.numberOfStudents = NumberOfStudents;
    }
    
    public String getName() {
        return name;
    }
}

public class TestCourse {

    public static void main(String[] args) {
    
        Course course1 = new Course("Programming II");
        course1.setNumberOfStudents(6);

        System.out.println("Number of students in Programming II Course: "  +course1.getNumberOfStudents());
        String[] students = course1.getStudents();
        course1.addStudent("Siti Aminah Muhammad");
        course1.addStudent("Halim Zainal Abidin");
        course1.addStudent("Jason Lim");
        for (int i= 1; i<course1.getNumberOfStudents(); i++)
            System.out.println( +i+ "." +students[i]);
        
        Course course2 = new Course("Database Systems");
        course2.addStudent("Fatimah Ahmad");
        course2.addStudent("Sarah Goh");
        course2.setNumberOfStudents(15);
        System.out.println("Number of students in Database Systems Course:" +course2.getNumberOfStudents());
        for (int i= 1; i<course2.getNumberOfStudents(); i++)
            System.out.println( +i+ "." +students[i]);
        
    }
  }

This is the output that I have.

3
  • 1
    You have issue in add function as well, you are adding to same index every time. Commented Apr 13, 2021 at 2:37
  • @code_mechanic Hi, can you elaborate more on this issue as I don't really understand what's the issue in my add function. Thank you in advance. Commented Apr 13, 2021 at 2:41
  • You are setting number of students before adding and then writing to same index in Student[] i.e 6 number of students in first case, then inside the class this is the line students [numberOfStudents] = student, this would write to only 6 index and while printing you are printing index less than 6, so before 6 every index have null value in array Commented Apr 13, 2021 at 2:44

1 Answer 1

1

In your method addStudent(), you are adding the student in the array at [numberOfStudents]. Doing so, your students are all placed at the same place in the array (6 for the first test and 15 for the second). This is why the only place you have a student is at 6. If you want them to be at 1,2,3,etc you need to set your new numberOfStudent each time you add one to make sure your next student will be after the last one.

If you don't want to do this each time, you can simply add this.numberOfStudent++; in your addStudent method.

Sign up to request clarification or add additional context in comments.

3 Comments

Program is setting numberOfStudents before adding, so it is not writing to 0 index.
Thank you. It actually solved my problem. But since the question requires me to have an output with ... and I only added the names as listed in the question, I was wondering if I can replace the null with "..." instead. Do you know how I can do it?
Either fill the array at the start like this for (int i = 0; i < students.length; i++) { students[i] = "..."; } or check for null values when you print the array if(students[i] == null) System.out.println( "..."); else System.out.println( +i+ "." +students[i]);

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.