Joe Razon
Joe Razon

Reputation: 154

Java: how to return an Array?

this is the code, from what I understand, it is suppose to work. Also when I debug my program, it seems that the array is being filled with the objects that the same method is creating. But still, when I try to print it, it shows me "null" back agian, like the "return" does not work. Why is it happening?

public class Course {

String courseName;
String teacherName;
int gradeAv;
static int counter;
static int maxNumOfStudents;
Student studentsArray[] = new Student[5];
int numOfStudents = studentsArray.length;

public Course() {

}

public Course(String courseName) {

    this();
    this.courseName = courseName;
}

public Course(String courseName, String teacherName) {

    this(courseName);
    this.courseName = courseName;
    this.teacherName = teacherName;
}

public Student[] addStudent(String name, int age, int grade) {

    for (int i = 0; i < 5; i++) {

        studentsArray[i] = new Student(name, age, grade);

        age += 10;
        grade += 10;
    }
    return studentsArray;
}

public void printStudentArray(Student studentArray[]) {

    studentArray = this.studentsArray;

    for (int i = 0; i < studentsArray.length; i++) {

        System.out.println(studentsArray[i]);
    }
}

public int gradeAv() {

    for (int i = 0; i < studentsArray.length; i++) {

        int temp = 0;

        if (studentsArray[i].grade > temp) {
            gradeAv = temp;
            System.out.println(gradeAv);
        }
    }
    return gradeAv;
}

public void printCourse() {

    System.out.println("Course: ");
    System.out.println("Course Name: " + courseName + ". "
            + "Teacher's Name: " + teacherName + ". "
            + "Number Of Students: " + numOfStudents + ". ");

}
   }

This is my main class:

public class MainClass {

public static void main(String[] args) {

    Student stud = new Student();
    Course cour = new Course("Java", "Ronni");

    stud.addStudent("Joe", 23, 100);

    stud.printStudent();
    System.out.println();
    stud.printCourse();

    System.out.println();

    cour.printStudentArray(cour.studentsArray);
    System.out.println();

// cour.gradeAv();

}

}

Upvotes: 0

Views: 13369

Answers (2)

NickJ
NickJ

Reputation: 9559

Try this for example:

Course class:

import java.util.ArrayList;
import java.util.List;

public class Course {

private List<Student> students = new ArrayList<Student>();
private String teacherName;
private String subjectName;

public Course(String subjectName, String teacherName) {
    this.subjectName = subjectName;
    this.teacherName = teacherName;
}

public void addStudent(Student student) {
    students.add(student);
}

public float getAverageGrade() {
    float grade = 0;
    for (Student student : students) {
        grade += student.getGrade();
    }
    return grade / students.size();
}

public void printCourse() {
    System.out.println("Course "+subjectName+" taught by "+teacherName);
    System.out.println("Students:");
    printStudents();
    System.out.println("Aberage grade: "+getAverageGrade());
}

public void printStudents() {
    for (Student student : students) {
        System.out.println(student.getName()+"\t age "+student.getAge()+" \t grade "+student.getGrade());
    }
}
}

Student class:

public class Student {

private String name;
private int age;
private int grade;

public Student(String name, int age, int grade) {
    this.grade = grade;
    this.age = age;
    this.grade = grade;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int getGrade() {
    return grade;
}

public void setGrade(int grade) {
    this.grade = grade;
}


}

Upvotes: 1

in

Student stud = new Student();
Course cour = new Course("Java", "Ronni");

stud.addStudent("Joe", 23, 100);

stud.addStudent("Joe", 23, 100); will not add any students to your course. stud is an instance of a completely different class, Student, and the implementation of Student is not in the code you've posted.

call the addStudent method of cour instead of the one for stud

cour.addStudent("Joe", 23, 100);

instead


I would also like to add that there are a lot of curious elements to your code, some of which people have brought up in the comments. It would be tedious to list them all. I'd take a look-through, just to make sure you're not doing redundant things.

Upvotes: 3

Related Questions