mysdyva wrote:
Thank you. That was helpful.

Can you tell me if this code is correct?


Code:
 
ArrayList<ArrayList<Student>> students = new ArrayList<ArrayList<Student>>();

Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);
That would be an ArrayList of objects of type ArrayList<Student>, and that inner ArrayList would have objects of type Student. There's no reason for you to nest objects like that; what was wrong with the code that I wrote a few posts up? Smile
I am receiving this error:

Quote:
cannot find symbol
symbol: variable students
location: class Testing01


with a little red squiggly line underneath the "students" variable.
That means you didn't declare students. Did you declare students as an ArrayList<Student> and then do a few students.add() commands as I recommended?
It worked! Thank you so much!!!
Just like you said, I created the new Array with this:


Code:
       ArrayList<Student> students = new ArrayList<Student>();


Then I added some objects to it:


Code:
            students.add(new Student("Donna Richards"), addGrade(s, 98));
            students.add(new Student("Cindy Maxwell"), addGrade(s, 98));
            students.add(new Student("Eddie Grant"), addGrade(s, 98));


Then I put in the constructor:



Code:
        Section sec = new Section(new Course("Java Programming"), new Instructor("George Jones"), students);


That solved the error and took away all the squiggly red lines.

My mistake was assuming that the constructor would instantiate the ArrayList.

Again, thank you very much.
My second problem: The assignment is to display grades next to each of the student names. In the Student class, there is an attribute for a grade.
Originally, I made a second ArrayList and ran them through the same for loop, but the grades would be related to the corresponding student that way. Technically, there is no such thing as a two-dimensional ArrayLists, but I know that there are some work-arounds for it, one of them being a nested ArrayList or even a Hashtable.

Will I have to alter the ArrayList or change it to a Hashtable? I could use the PrintWriter and FileWriter to store the values, but I suspect that is not what is intended for this assignment.

My Section class has two methods, one called addGrades and one called getGrades, but I am not certain how to use these methods with the ArrayList I have. I created the ArrayList before the Section object, meaning the Section object has not been created yet. I can't call the method from the Section class like this:


Code:
students.add(new Student("Donna Richards"), sec.getGrades("A")));


I know that I can add an item, remove an item, and replace an item, but is there an ArrayList method that will allow me to append items?

I would appreciate suggestions or even just being pointed in the right direction.
You would want to set the grade within the Student class, not as a separate element in the ArrayList. Then, you could simply add only the student to the ArrayList, keeping the grades contained within the Student objects.
As Souvik says, remember, a Student object is a bucket that you can put anything in, just as a Section object is a bucket that you put a Course, an Instructor, and a whole yarn full of Student objects in. Thus far, you've only been dropping a string containing a name into each Student object, but now you also want to put a grade string, so two strings will be sitting inside each Student object. I suspect you have a fundamental misunderstanding about what an Object is, exactly, and once you get cleared up that an Object is an abstract container that you can pack all kinds of other stuff into, this will start getting much easier for you.

By the way, a point of forum etiquette that I have been lenient on you for thus far: multi-posting (double-posting, triple-posting, etc) is highly frowned-upon. If you were the last person to post in a topic, and it has been less than a day since your post, you must edit your post to append new things you want to add rather than posting another post. You have both a double-post and a triple-post in this topic; please try to avoid that in the future.
An array list set up like that provides you with a 2D map of students, which is not what you want. Look at Section(...); It is defined as taking an ArrayList<Student>. That should tell you how to construct the students object. You already should have made it according to your post.

<edit>

Holy hell, I got ninja'd by 8 posts. Sad
KermMartian wrote:
By the way, a point of forum etiquette that I have been lenient on you for thus far: multi-posting (double-posting, triple-posting, etc) is highly frowned-upon. If you were the last person to post in a topic, and it has been less than a day since your post, you must edit your post to append new things you want to add rather than posting another post. You have both a double-post and a triple-post in this topic; please try to avoid that in the future.


I do apologize. It won't happen again.

Quote:
You would want to set the grade within the Student class, not as a separate element in the ArrayList. Then, you could simply add only the student to the ArrayList, keeping the grades contained within the Student objects.


KermMartian wrote:
As Souvik says, remember, a Student object is a bucket that you can put anything in, just as a Section object is a bucket that you put a Course, an Instructor, and a whole yarn full of Student objects in. Thus far, you've only been dropping a string containing a name into each Student object, but now you also want to put a grade string, so two strings will be sitting inside each Student object.


***OK, I understand that. The problem is that I created 15 student objects in the ArrayList. How do I access the objects in order to add the "grades" attributes? Or should I enter the attributes as I am adding to the ArrayList?*** Disregard this question; I figured it out.

KermMartian wrote:
I suspect you have a fundamental misunderstanding about what an Object is, exactly, and once you get cleared up that an Object is an abstract container that you can pack all kinds of other stuff into, this will start getting much easier for you.


Could you elaborate on this further? If I am misunderstanding the concept of an Object, I need to correct it. What do you believe my understanding of an Object? That might help me to figure it out.

Again, thanks for all of your help!

Edit: I learned from you that I need to instantiate an ArrayList before using it in a constructor. Is it the same for the two single objects that are also in this constructor?
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 2 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement