Java Programming - Arrays question

I have a class which stores a list of Students and their ages. I need to create a method that returns the oldest student. How would I do this? Do I need to implement this to an Array or?
[193 byte] By [sunlordclavea] at [2007-11-26 23:17:03]
# 1

before posting such question see the java tutorial or the jdk API

http://java.sun.com/docs/books/tutorial/collections/interfaces/list.html

http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html

Once you create YOUR program, you can post a question if you need help.

HTH

java_2006a at 2007-7-10 14:18:07 > top of Java-index,Java Essentials,Java Programming...
# 2

I have a class which stores a list of Students and their agesI have a class which stores a list of Students and their ages.

So, are you saying you already have the storage for these students? Are they in a list or an array or a fixed set of independent variables?

When you say "students and their ages," do you mean that the age is separate from the rest of the student data? Or do you have a Student class with an age or birthdate member?

It's hard to give specific advice with so little information, but in general, you would do it just like you'd do it manually: Walk through the list of students, checking each one's age against the max so far. If it's greater, then that student becomes the new oldest and his age becomes the new max age.

jverda at 2007-7-10 14:18:07 > top of Java-index,Java Essentials,Java Programming...
# 3

In the method that returns the oldest student, just iterate through the list, calling the method in Student that returns the age.

After that is it just simple comparisons. If you aren't using it for something else, you can implement Comparable, or if you have, Comparator to do the comparisons.

Keep track of the oldest age and a reference to that Student, then return that student

Rusty_Shackleforda at 2007-7-10 14:18:07 > top of Java-index,Java Essentials,Java Programming...
# 4
if you have a java.util.List of objects, then you must be aware of the collectionsframework, und so the utility class java.util.Collections. I wonder if it has anyhandy methods to help you out, in this case...;-))
DrLaszloJamfa at 2007-7-10 14:18:07 > top of Java-index,Java Essentials,Java Programming...
# 5

I have a class which stores a list of Students and

their agesI have a class which stores a list of

Students and their ages.

So, are you saying you already have the storage for

these students? Are they in a list or an array or a

fixed set of independent variables?

When you say "students and their ages," do you mean

that the age is separate from the rest of the student

data? Or do you have a Student class with an age or

birthdate member?

It's hard to give specific advice with so little

information, but in general, you would do it just

like you'd do it manually: Walk through the list of

students, checking each one's age against the max so

far. If it's greater, then that student becomes the

new oldest and his age becomes the new max age.

I have a class which stores a Student name, his age and a few other things. It doesn't have an arraylist or an array. And how would the method decide wat the max so far is without me entering the max as a parameter? The way this program works is the ages keep changing a lot so there's no way to have to define a max age manually.

sunlordclavea at 2007-7-10 14:18:07 > top of Java-index,Java Essentials,Java Programming...
# 6

I have a class which stores a Student name, his age

and a few other things.

Okay. So then if you're going to get the oldest, you must have more thane one of these, presumably in a collection or array.

It doesn't have an arraylist

or an array.

Of course not. Why would a Student object contain a list of students? But somewhere there must be something to hold a bunch of these things if you're going to get the max. I mean, maybe you just have several different, independent student variables, but that would be kind of silly.

And how would the method decide wat the

max so far is without me entering the max as a

parameter?

So, the only variables that can exist in a method are parameters?

The way this program works is the ages

keep changing a lot so there's no way to have a

definite max age.

This makes no sense.

You want to see the max age of a group of students at some point in time. So you look at each of their ages in turn. For each age, you see if it's greater than the greatest you've found so far:

Billy's 12, he's the oldest

Mary's 9, which is less than the oldest so far (Billy/12), so move on

Joe's 53, which is older than the oldest so far (Billy/12) so now Joe's the oldest.

Ludwig is 17, which is less than the oldest so far (Joe/53) so move on.

Exactly like you'd do it manually, as I stated before.

jverda at 2007-7-10 14:18:07 > top of Java-index,Java Essentials,Java Programming...