• Post Reply Bookmark Topic Watch Topic
  • New Topic

storing and accessing ArrayList elements  RSS feed

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is about the most complex program I have written to date. There are three classes here. The first is a driver, the second is the "bulk" of the project, and the third is used to store data. What I need to do is add 10 grades to each name the user inputs in the addStudent method. When the method is called again, the user can add a new student with an additional set of grades(up to 10). The I want to print out the students name with his/her grades as such:

Name 1 2 3 4 5 6 7 8 9 10
Ben Ritter 2.0 8.0 6.0 5.0 7.0 4.0 7.0 3.0 2.0 6.0
Joshua Smith 9.0 8.0 5.0 7.0 4.0 6.0 3.0 7.0 8.0 5.0
etc.....

Here is the driver class:


[/code]

And here is the data class:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and...is there something specific you need help with, or have a question about?
 
Ashish R Garg
Greenhorn
Posts: 8
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some suggestions:

1. Instead of using if..else...if..., you can use Switch() statement, will improve readability and performance.
2. Instead of using:

Use something like:

3. In Student Class too, you can use an arraylist instead of so many assignment grade variables. In fact, since you have your number of assignments fixed to 10, you can use array of size 10.

Do you have any specific query?
 
sai rama krishna
Ranch Hand
Posts: 530
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> When the method is called again, the user can add a new student with an additional set of grades(up to 10).

can you please elaborate on this more
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, what does ArrayList<Student> do? Is that accessing the constructor Student class? If so, I am not sure how to implement it. Basically, as the program is now, when the user chooses option 7 ( to list all the names and grades stored in their respective arrays ) it prints out of the names next to each other. I want it to print out the names on a new line with the grades entered for each name next to it.

Joe 10.0 4.0 3.0
Bill 10.0 3.0 8.0

Not like this:

[Joe, Bill] [10.0. 4.0, 3.0, 10.0, 3.0, 8.0]

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:First, what does ArrayList<Student> do? Is that accessing the constructor Student class?


No. It just says that this ArrayList will contain references to Student objects.


If so, I am not sure how to implement it. Basically, as the program is now, when the user chooses option 7 ( to list all the names and grades stored in their respective arrays ) it prints out of the names next to each other. I want it to print out the names on a new line with the grades entered for each name next to it.

Joe 10.0 4.0 3.0
Bill 10.0 3.0 8.0


Sounds like you should define a Student class, and it should have a member variable for the student's name and one for hist list of grades.
 
Ashish R Garg
Greenhorn
Posts: 8
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:First, what does ArrayList<Student> do? Is that accessing the constructor Student class? If so, I am not sure how to implement it.


Its the same like ArrayList<String>, just that, it will now hold object of type your Student Class rather than String.
The reason why I recommended that, its good OOPS practice to encapsulate all the relevant information at one place and abstracting the unnecessary details from outside. It will make your Student Class pluggable as well.


Ian Burres wrote:Basically, as the program is now, when the user chooses option 7 ( to list all the names and grades stored in their respective arrays ) it prints out of the names next to each other. I want it to print out the names on a new line with the grades entered for each name next to it.
Joe 10.0 4.0 3.0
Bill 10.0 3.0 8.0
Not like this:
[Joe, Bill] [10.0. 4.0, 3.0, 10.0, 3.0, 8.0]


You can use String.Format() available in java or can write custom on your own with the help of for or while loop. I think thats the easy part once you are done with Saving & Retrieving your student records efficiently.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:This is about the most complex program I have written to date.

Ian Burres wrote:First, what does ArrayList<Student> do?

you wrote this, but you don't know what it does?
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:>> When the method is called again, the user can add a new student with an additional set of grades(up to 10).

can you please elaborate on this more

When option 1 is chosen, you input a name. Then you input 10 values (I only have two right now for brevity, thought I will add 8 more later).
When the display prompt comes up again, the user can repeat theses steps indefinately. Assigning a name with 10 grades for each student.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Ian Burres wrote:This is about the most complex program I have written to date.

Ian Burres wrote:First, what does ArrayList<Student> do?

you wrote this, but you don't know what it does?


Yes, because I wrote ArrayList<String>. So I clearly did not realize that <Student> would access that class, as that is the bulk of my problem here.

 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:
sai rama krishna wrote:>> When the method is called again, the user can add a new student with an additional set of grades(up to 10).

can you please elaborate on this more

When option 1 is chose, you input a name. Then you input 10 values (I only have two right now for brevity, thought I will add 8 more later).
When the display prompt comes up again, the user can repeat theses steps indefinately. Assigning a name with 10 grades for each student.


disregard this last post.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish R Garg wrote:
Ian Burres wrote:First, what does ArrayList<Student> do? Is that accessing the constructor Student class? If so, I am not sure how to implement it.


Its the same like ArrayList<String>, just that, it will now hold object of type your Student Class rather than String.
The reason why I recommended that, its good OOPS practice to encapsulate all the relevant information at one place and abstracting the unnecessary details from outside. It will make your Student Class pluggable as well.


Ian Burres wrote:Basically, as the program is now, when the user chooses option 7 ( to list all the names and grades stored in their respective arrays ) it prints out of the names next to each other. I want it to print out the names on a new line with the grades entered for each name next to it.
Joe 10.0 4.0 3.0
Bill 10.0 3.0 8.0
Not like this:
[Joe, Bill] [10.0. 4.0, 3.0, 10.0, 3.0, 8.0]


You can use String.Format() available in java or can write custom on your own with the help of for or while loop. I think thats the easy part once you are done with Saving & Retrieving your student records efficiently.


Thank you. That makes sense. I will try that out.
 
Ashish R Garg
Greenhorn
Posts: 8
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No Problem. Happy Ranching!
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, when I chane ArrayLists<String> to <Student> I get compile-time run errors at the following location:



Why is this coming up? It's in the addStudent() method.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to put into that list? Students or Strings? If it's a list of Students, you can't add "Bob" to it because "Bob" is a String, not a Student. You'd have to create a Student object, set its name field to "Bob" and add that.

On the other hand, if you just want to store a list of the students' names, then you'd want a List<String>, not <Student>.

(Although I haven't looked at your problem in detail, I would think it would make more sense to have a list of Student objects, rather than just a list of their names.)
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the error in the Student class or the Midterm class?
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:What do you want to put into that list? Students or Strings? If it's a list of Students, you can't add "Bob" to it because "Bob" is a String, not a Student. You'd have to create a Student object, set its name field to "Bob" and add that.

On the other hand, if you just want to store a list of the students' names, then you'd want a List<String>, not <Student>.

(Although I haven't looked at your problem in detail, I would think it would make more sense to have a list of Student objects, rather than just a list of their names.)


Ok Jeff, I think you have an idea of where I want to go. I want to creat a Student object and set it's name to what the user enters. Is the Student class set up correctly? I have global variables, a default constructor, a constructor with parameters and the accessor (getter) and mutator (setter) methods.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the student name has been declared, along with the associated assignment grades, the addStudent method would be called. Then, if the user chooses to list all the students currently in the array (option 7), it will print them out as I mentioned earlier. Each one on a new line.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:I want to creat a Student object and set it's name to what the user enters.


Okay, so the Student class will have a name member variable at the very least. What else will a student have associated with him? A list of grades perhaps?

Is the Student class set up correctly?


I don't know. I can't see it.

I have global variables


No you don't. There's no such thing in Java.

, a default constructor, a constructor with parameters and the accessor (getter) and mutator (setter) methods.


There's no right or wrong answer as to which constructors you should have, and whether you should have getters and setters.

For instance, one reasonable approach would be that a student cannot change his name. So your only constructor would be the one that takes a name parameter, the name member variable would be declared final, and you'd probably have a getter but you wouldn't have a setter.

As for other member variables, just blindly providing get/set methods--especially for mutable objects like List or Date--is not a good idea. It's usually better to provide methods that tell the object what to do, and let it operate on itself, rather than exposing its state. For instance, if Student has a list of grades, it would be better to to student.addGrade(grade) rather than student.getGradeList().add(grade).

One exception to the above rule is JavaBeans, which usually have state but no behavior. That's not something you need to delve into now. Just be aware that these cases exist.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Jeff there are certain restrictions I must follow when constructing this program.

Here is the Student class:


And this is the method I want to use to add the student name entered by the user.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a mess, I know.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
’Fraid it is.
Why have you got so many fields? Why have you given them Hungarian names? You ought not to have so many fields. Can’t you replace them with a double[] array?
You should have as few constructors as possible. If there is a constructor which might not initialise all your fields the way you really want them, get rid of it. You have one which sets all the marks to 0 and the name to "". That doesn’t look right. Get rid of it. You have a constructor with ten parameters. That is too many. Can’t you replace some with an array (as I said earlier).
Then when you get into the constructor, it doesn’t do anything. You might call those get methods, but that doesn’t have any effect on anything. You should be able to find more about constructors in the Java Tutorials.

Forget about options like add a student at this stage. Get the student class working now, and you can add such things later.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment requirements:


Class Name: Student
Default Constructor – Should do nothing
Overridden Constructor – Should create a student with a name and all 10 grades

Global Variables:

strStudentName
dblAssignmentOne
dblAssignmentTwo
dblAssignmentThree
dblAssignmentFour
dblAssignmentFive
dblAssignmentSix
dblAssignmentSeven
dblAssignmentEight
dblAssignmentNine
dblAssignmentTen

Methods: Each global variable should have an accessor (getter) and a mutator (setter).
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have all of these requirements. Whether they are done correctly or not is another story.

<<<---- is Very frustrated right now.
 
Ian Mcloud
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:’Fraid it is.
Why have you got so many fields? Why have you given them Hungarian names? You ought not to have so many fields. Can’t you replace them with a double[] array?
You should have as few constructors as possible. If there is a constructor which might not initialise all your fields the way you really want them, get rid of it. You have one which sets all the marks to 0 and the name to "". That doesn’t look right. Get rid of it. You have a constructor with ten parameters. That is too many. Can’t you replace some with an array (as I said earlier).
Then when you get into the constructor, it doesn’t do anything. You might call those get methods, but that doesn’t have any effect on anything. You should be able to find more about constructors in the Java Tutorials.

Forget about options like add a student at this stage. Get the student class working now, and you can add such things later.


About spit my drink out when I saw this. I changed them by removing the dbl and str prefixes.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that your instructor uses the term "global variables" (which don't exist in Java), and suggests you have 10 doubles for the grades, makes me weep tears of blood. I guess the 10 variables is excusable if he's going to teach you arrays or lists next, and just wants to have you do it this way to provide some context and contrast. But otherwise, ick.
 
Ashish R Garg
Greenhorn
Posts: 8
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't get too worked up Ian.
Here at CodeRanch, the philosophy is not to give fish to hungry, but teach him how to fish.
Now you have to decide what your actual problem is, assignment or Java basics.
Don't hush-hush things at beginner level, because it will make a concrete base for your advanced topics.
Just go through the Classes, constructors, Collections, Arrays topics once more.
Java Learning
If you have any query or problem there, then we are here only, happy to help.
Hail Java
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Burres wrote:Assignment requirements:


Class Name: Student
Default Constructor – Should do nothing . . .
You should query that. You cannot have a default constructor alongside a written constructor. That is a contradiction in terms. A default constructor is written automatically by the javac tool when the programmer has not provided their own constructor. You might write a no‑arguments constructor, however.

You should query that. It is poor design to have a constructor which does nothing, because fields can retain their default values. In the case of those doubles, they will be 0 anyway, so no problem there. Only a style problem. In the case of the name, it will point to null, and that is dangerous because there is a risk of a NullPointerException if that name value is used elsewhere. Or even worse, of recording a student with no name.

Jeff V might be weeping blood. I simply thought, “find another course elsewhere.”
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!