Results 1 to 9 of 9
  1. #1
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default ArrayList as a parameter

    I have five classes all together: Instructor, Student, Course, Section, and Testing.

    In the section class, I have this:

    Java Code:
    public class Section {
        private Course course;
        private Instructor instructor;
        private ArrayList<Student> students;
    
        public Section() {
            super();
        }
        
        public Section(Course course) {
            
        }
        
        public Section(Course course, Instructor instructor) {
            
        }
        
        public Section(Course course, Instructor instructor, ArrayList<Student> students) {
    
        }
          // getters and setters
    
        public void setCourse(Course course)
          {
            this.course = course;
          }
    
        public Course getCourse()
          {
            return course;
          }
    
        public void setInstructor(Instructor instructor)
          {
            this.instructor = instructor;
          }
    
        public Instructor getInstructor()
          {
            return instructor;
          }
    
        public void setStudents(ArrayList<Student> students)
          {
            this.students = students;
          }
    
        public ArrayList<Student> getStudents()
          {
            return students;
          }
    
        public void addStudent(Student student)
          {
            students.add(student);  // add a student to the section roster
          }   
        }
    In the main method, I want to create a Section object by accessing this constructor:

    Java Code:
        public Section(Course course, Instructor instructor, ArrayList<Student> students) {
    
        }
    So I started with:

    Java Code:
    public class Testing01 {
        public static void main (String [] args) throws IOException {
            
            Section sec = new Section("Java Programming", "George Jones", /* ArrayList variable here*/); {
    
    }
    }
    }
    I would like the constructor to create the ArrayList, but I can not figure out how to code the parameter. It's not in my reading and I have scoured the Internet for the answer with no results. Can anyone tell me how to code it correctly?

  2. #2
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Re: ArrayList as a parameter

    Hey there,

    I am still banging my head against the keyboard trying to figure this out. Does anyone have any suggestions?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList as a parameter

    If you want the constructor to create the ArrayList, why pass one in as a parameter? Get rid of the parameter and make the constructor create the ArrayList:

    Java Code:
    // in the body of the ctor:
    students= new ArrayList<String>();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: ArrayList as a parameter

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Re: ArrayList as a parameter

    Quote Originally Posted by JosAH View Post
    If you want the constructor to create the ArrayList, why pass one in as a parameter? Get rid of the parameter and make the constructor create the ArrayList:

    Java Code:
    // in the body of the ctor:
    students= new ArrayList<String>();
    kind regards,

    Jos
    I thought so, too, but someone told me to create it this way.

    The really horrible fact about this entire thing is that I had finished this, and it was working. Apparently, though, I was told that I did it the hard way and given some advice to change it. Now I am really confused and frustrated.

  6. #6
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Re: ArrayList as a parameter

    Sorry about that.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList as a parameter

    Show us how you did it before.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    ladykrimson is offline Member
    Join Date
    Oct 2010
    Posts
    69
    Rep Power
    0

    Default Re: ArrayList as a parameter

    Quote Originally Posted by JosAH View Post
    Show us how you did it before.

    kind regards,

    Jos
    My assignment was to create 5 classes: Instructor, Student, Course, Section, and a class, which I named Testing01, to house the main method. I had no problem with this part.

    The second part was to create an Instructor object, a Course object, and an Student object with 15 student names. I also needed to have these objects displayed. I accomplished the student list with the PrintWriter() function (since we hadn't learned arrays, yet).

    The third part was to create the ArrayList() for the students and another List for the grades, having the program display the two ArrayLists (I do realize I could have done a two-dimensional ArrayList).

    At this point, I had the four classes with getters and setters, and the fifth class with the main method. I created the new objects like this:

    Java Code:
    Instructor inst = new Instructor ("George Jones");
    Course crse = new Course ("Java Programming");
    and then the ArrayList like this:

    Java Code:
    ArrayList<String> students = new ArrayList<String>();
    Next, I added the student names using the ArrayList.add method (I could make it two dimensional to add the grade list as well). I also used a for loop to cycle through the list of names:

    Java Code:
           for (int i = 0; i < students.size(); i++){
                    // Instantiation of Student class
                    Student stdt = new Student();                                      
                    stdt.setFullName(students.get(i));  
                    stdt.setLevel(grades[i]);
                    // Print the current value of the variable
                    System.out.printf("%s\t\t%s\n", stdt.getFullName(), stdt.getLevel());
    That was what I handed in as my assignment. I received this as feedback:

    You're pretty close on this one. You have the modeling down up to the section and then you made things a little too hard on yourself. Take a look at the Section class that I posted in the discussion this week. You can use this Section class to make an object that has a course, instructor and roster of students. That is a much more sensible place to bring the elements together and then you just have the main method be an exercise of instantiating the Section object and populating it and then printing its values. This is a good effort, though.
    His Section class looks like this:

    Java Code:
    import java.util.ArrayList;
    
    public class Section {
        private Course course;
        private Instructor instructor;
        private ArrayList<Student> students;
    
        public Section() {
            super();
        }
         
        public Section(Course course) {
             
        }
         
        public Section(Course course, Instructor instructor) {
             
        }
         
        public Section(Course course, Instructor instructor, ArrayList<Student> students) {
    
        }
          // getters and setters
    
        public void setCourse(Course course)
          {
            this.course = course;
          }
    
        public Course getCourse()
          {
            return course;
          }
    
        public void setInstructor(Instructor instructor)
          {
            this.instructor = instructor;
          }
    
        public Instructor getInstructor()
          {
            return instructor;
          }
    
        public void setStudents(ArrayList<Student> students)
          {
            this.students = students;
          }
    
        public ArrayList<Student> getStudents()
          {
            return students;
          }
    
        public void addStudent(Student student)
          {
            students.add(student);  // add a student to the section roster
          }    
        }

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList as a parameter

    Concentrate on those three constructors and make them do something;

    Java Code:
    public Section(Course course) { this(course, null); }            
    public Section(Course course, Instructor instructor) { this(course, instructor, new ArrayList<String>()); }
    public Section(Course course, Instructor instructor, ArrayList<Student> students) {       
       this.course= course;
       this.instructor= instructor;
       this.students= students;
    }
    The first ctor calls the second one and the second ctor calls the third one that does all of the initialization. The second ctor creates a empty new ArrayList<String> for the third one and the third one sets its students member with that new ArrayList<String>; if you called the first ctor, no instructor has been set and has to be done later with one of the setter methods. After that the object is all set up to be used by the rest of your program. Maybe this is what your instructor had in mind ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with formal parameter, ArrayList
    By ezkatz19 in forum New To Java
    Replies: 2
    Last Post: 01-27-2012, 06:05 AM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  4. Using ArrayList as a parameter
    By Crypts in forum New To Java
    Replies: 6
    Last Post: 05-13-2010, 06:56 AM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •