0

Ok, so this is a part of my main method:

    public class Driver {
public static void main(String[] args) {
    Scanner scan=new Scanner(System.in);
   //Variables to place data attributes of object in
    String teacherName;
    int teacherId;
    double basicSalary;
    double extraPaymentRate;
    int numOfCourses=100;
    Course []coursesTaught=new Course[numOfCourses];
    String courseName;
    int courseId;
    int y;
    System.out.println("Please enter number of teachers:");
    y=scan.nextInt();
    Teacher []teacherArray=new Teacher[y];
    for(int i=0;i<y;i++) {
        System.out.println("Enter teacher’s name, id, basic salary, and extra payment rate: ");
        teacherName=scan.next();
        teacherId=scan.nextInt();
        basicSalary=scan.nextDouble();
        extraPaymentRate=scan.nextDouble();
        System.out.println("Enter number of courses taught: ");
        numOfCourses=scan.nextInt();
        for(int j=0;j<numOfCourses;j++) {
            System.out.println("Enter course names and IDs:");
            courseName=scan.next();
            courseId=scan.nextInt();
            coursesTaught[j]=new Course(courseName,courseId);
        }
        teacherArray[i]=new Teacher(teacherName,teacherId,basicSalary,extraPaymentRate,numOfCourses,coursesTaught);
    }

now my problem is that every time i add a new teacher object, the new courses i add to that teacher object override the last one. so all the teacher have the same courses

here's the teacher class

public class Teacher {
       private String name;
       private int id;
       private double basicSalary;
       private double extraPaymentRate;
       public static int numOfCourses;
       public Course []coursesTaught= new Course[numOfCourses];
public Teacher() {

}
public Teacher(String name,int id,double basicSalary,double 
    extraPaymentRate,int numOfCourses,Course []coursesTaught) {
    this.name=name;
    this.id=id;
    this.basicSalary=basicSalary;
    this.extraPaymentRate=extraPaymentRate;
    Teacher.numOfCourses=numOfCourses;
    this.coursesTaught=coursesTaught;
}
//setter and getter methods here

and here's the course class

public class Course {
public String name;
public int id;
public Course() {

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

I'm still grasping the objects concept. Any help?

1 Answers1

4
Course []coursesTaught=new Course[numOfCourses];

is declared before the loop.
So you instantiate this once and all created Teacher instances use the same in the loop :

Course []coursesTaught = new Course[numOfCourses];
...
Teacher []teacherArray = new Teacher[y];
for(int i=0;i<y;i++) {
   teacherArray[i] = new Teacher(teacherName,teacherId,basicSalary,extraPaymentRate,numOfCourses,coursesTaught);
   ...
}

Declare coursesTaught in the loop that creates and populates each Teacher instance in order to instantiate coursesTaught at each iteration and consequently to attach it to the current created Teacher instance :

...
Teacher []teacherArray=new Teacher[y];
for(int i=0;i<y;i++) {
    Course []coursesTaught = new Course[numOfCourses];
    teacherArray[i] = new Teacher(teacherName,teacherId,basicSalary,extraPaymentRate,numOfCourses,coursesTaught);
    ...
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215