Results 1 to 9 of 9
Thread: ArrayList as a parameter
- 10-04-2012, 04:32 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
ArrayList as a parameter
I have five classes all together: Instructor, Student, Course, Section, and Testing.
In the section class, I have this:
In the main method, I want to create a Section object by accessing this constructor: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 } }
So I started with:Java Code:public Section(Course course, Instructor instructor, ArrayList<Student> students) { }
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?Java Code:public class Testing01 { public static void main (String [] args) throws IOException { Section sec = new Section("Java Programming", "George Jones", /* ArrayList variable here*/); { } } }
- 10-05-2012, 05:27 PM #2
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
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?
- 10-05-2012, 07:50 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:// in the body of the ctor: students= new ArrayList<String>();
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-05-2012, 07:54 PM #4
Re: ArrayList as a parameter
Crossposted: 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!
- 10-05-2012, 10:46 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Re: ArrayList as a parameter
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.
- 10-05-2012, 10:46 PM #6
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Re: ArrayList as a parameter
Sorry about that.
- 10-06-2012, 09:10 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Re: ArrayList as a parameter
Show us how you did it before.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-06-2012, 05:15 PM #8
Member
- Join Date
- Oct 2010
- Posts
- 68
- Rep Power
- 0
Re: ArrayList as a parameter
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:
and then the ArrayList like this:Java Code:Instructor inst = new Instructor ("George Jones"); Course crse = new Course ("Java Programming");
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:ArrayList<String> students = new ArrayList<String>();
That was what I handed in as my assignment. I received this as feedback: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());
His Section class looks like this: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.
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 } }
- 10-06-2012, 06:28 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,603
- Blog Entries
- 7
- Rep Power
- 17
Re: ArrayList as a parameter
Concentrate on those three constructors and make them do something;
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 ...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; }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Help with formal parameter, ArrayList
By ezkatz19 in forum New To JavaReplies: 2Last Post: 01-27-2012, 06:05 AM -
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Using ArrayList as a parameter
By Crypts in forum New To JavaReplies: 6Last Post: 05-13-2010, 06:56 AM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks