Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on ArrayList

 
avelin chen
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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 ]
 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic