jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Question on ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on ArrayList" Watch "Question on ArrayList" New topic
Author

Question on ArrayList

avelin chen
Greenhorn

Joined: Dec 11, 2005
Posts: 27
I'm supposed to complete computeStudentMean method and the displayMeans method. Method computeStudentMean accesses the four grades in each individual student record, computes the mean of the four grades and stores the result in mean. Method displayMeans list all the student name and their respective test averages.


For the computeStudentMean part, I know that I can convert an object into a primitive data type by doing somthing like this:


What I don't get is how to convert my Student object into like an integer, s I can add them up and find the mean. Also, for my displayStudentMean, I get the addresses instead of the actual numbers. Thanks in advance!
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
"What I don't get is how to convert my Student object into like an integer, s I can add them up and find the mean."

Write a public method in Student that returns the proper integer value. These are called "get" methods, or more properly accessors.


"Also, for my displayStudentMean, I get the addresses instead of the actual numbers. "

You need to override the toString method of Object

public String toString()
{
//return whatever Student values you want in String form

}
[ March 05, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Conrad McLaughlin
Greenhorn

Joined: Jan 31, 2006
Posts: 27
Are you asking how to convert an object to an integer? To do so you have to convert the object to string first. Look at my example where the object is called fn, string is fn2 and integer is fn3.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Conrad McLaughlin:
Are you asking how to convert an object to an integer? To do so you have to convert the object to string first. Look at my example where the object is called fn, string is fn2 and integer is fn3.



I'm positive that this code, if you are able to compile it, will throw a runtime Exception. Integer.parseInt(String) can only parse a String that contains a parsable integer. The String returned by calling toString() on an array instance is definitely not a parsable integer. Rusty's idea was on the right track.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
 
subject: Question on ArrayList
 
Similar Threads
subString
Averaging Grades Assignment
Doubt in Strings
String puzzle
String and String Object Problem