user12761284
user12761284

Reputation: 65

Method for passing class objects to the list in another class

what will be the correct method to pass [from the main class Runner] the created Student objects to the List in the [public class Group] and to pass the Group objects to the List in the [public class Faculty]? I tried to do it using the constructer, but it does not seem to be a good idea.

public class Runner {
public static void main(String[] args) {

 Student student1 = new Student ("Steve", "Faculty of Mathematics", "Group 21");
 Student student2 = new Student ("Oliver", "Faculty of Chemistry", "Group 22");

 Group group1 = new Group(21, (student1));
 Group group2 = new Group(22, (student2));

 Faculty faculty1 = new Faculty("Faculty of Mathematics", (group));

    }
}

public class Group { 
    private int groupNumber;
    private ArrayList<Student> studentList = new ArrayList<Student>();

    public Group(int groupNumber, Student student) {
        this.groupNumber = groupNumber;
        this.student = student;
    }

    //adds student objects to the list
    List<Student> getStudents() {
        studentList.add(student);
        return studentList;
    }
}

public class Faculty {
   private ArrayList<Group> groupList = new ArrayList<Group>();
//some code
}

Upvotes: 0

Views: 641

Answers (2)

Neminda Prabhashwara
Neminda Prabhashwara

Reputation: 434

A good oop technique would be to add an append() method in both Group class and Faculty class.

Since Group and Faculty both have lists that contain multiple objects, it would be a good idea to generate your students through a loop either getting inputs from the user or other way that you prefer to generate the students, and append them to the list in the Group's Arraylist.

For the Faculty class this idea would work fine.

The heart of oop is not repeat the same code anywhere in your program. So my recommendation for your Student class is just to keep their private information in it. And when they are in a Group object it has their group information, same like that when the Group object is in a Faculty object it has the student's faculty information.

The basic structure for your Group, Faculty may be like below.

class Group {

  private int groupNumber;
  private ArrayList<Student> studentList = new ArrayList<Student>();

  public Group(int groupNumber) {
    this.groupNumber = groupNumber;

  }

  public void append(Student student){

    this.studentList.add(student);

  }

  public ArrayList getlist(){

    return this.studentList;

  }

}

class Faculty {
  private ArrayList<Group> groupList = new ArrayList<Group>();

  public Faculty(String faculty) {
  }

  public void append(Group group){

    this.groupList.add(group);

  }

  public ArrayList getlist(){

    return this.groupList;

  }

}

Then you can customise your Runner class class as you wish. Generating the students as you wish, through looping or as your wish.

class Runner {
  public static void main(String[] args) {

    Student student1 = new Student ("Steve");
    Student student2 = new Student ("Oliver");

    Group group1 = new Group(21);
    group1.append(student1);
    Group group2 = new Group(22);
    group2.append(student2);

    Faculty faculty1 = new Faculty("Faculty of Mathematics");
    faculty1.append(group1);
    faculty1.append(group2);

  }
}

A more efficient program would be like below. It generates the faculty according to your input and appends each group to it.

class Runner {
  public static void main(String[] args) {

    Scanner scanner=new Scanner(System.in);

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

      Faculty faculty1 = new Faculty(scanner.nextLine());
      for(int j=0;j<20;j++) {

        Group group = new Group(j);
        for (int i = 0; i < 10; i++) {
          group.append(new Student(scanner.nextLine()));
        }
        faculty1.append(group);

      }

    }

  }
}

Upvotes: 0

Michael Kreutz
Michael Kreutz

Reputation: 1256

One syntactically correct way of achieving this is to add student to studentList in the constructor, e.g.

public class Group {
  private int groupNumber;
  private List<Student> studentList = new ArrayList<>();

  public Group(int groupNumber, Student student) {
    this.groupNumber = groupNumber;
    this.studentList.add(student);
  }

  // Getter should only return the list
  public List<Student> getStudents() {
    return studentList;
  }
}

However I would recommend you to change the constructor to accept a list of students and provide an addStudent method, e.g.

public class Group {
  private int groupNumber;
  private List<Student> studentList;

  public Group(int groupNumber, List<Student> students) {
    this.groupNumber = groupNumber;
    this.studentList =students;
  }

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

  //adds student objects to the list
  public List<Student> getStudents() {
    return studentList;
  }
}

and change the caller to execute Group group1 = new Group(21, new ArrayList<>(student1)); Remarks

  • You do not need to wrap arguments in (), e.g. Group group1 = new Group(21, (student1)); can be written as Group group1 = new Group(21, student1);
  • Getters should not have logic but only return the object

Upvotes: 2

Related Questions