creating array

J

justin

Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)

mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;

public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;

public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}

and i am making another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys
 
S

Stefan Waldmann

justin said:
Hie i am a newb and i was wondering if i created a class of the name
Swimmer
with the following codes (just abit not all..cos dun wanna flood the
place)

mport java.util.GregorianCalendar;
import javax.swing.*;
import java.util.Calendar;

public class Swimmer {
private String firstName,surname,gender,bestStroke,bestTime;
private double weight,height;
private GregorianCalendar dateOfBirth;
private int calcAge;

public Swimmer(String n,String s,String g,double w,double h,String
bs,String bt,int bYear,int bMonth,int bDay){
firstName=n;
surname=s;
gender=g;
weight=w;
height=h;
bestStroke=bs;
bestTime=bt;
dateOfBirth = new GregorianCalendar(bYear, bMonth, bDay);}

and i am making another class...which is the GUI interface...what do i
do to set up and array of the class Swimmer? and i must be able to
browse the array thru the GUI with some buttons..thanks guys

Hi

you can create an array of Swimmer (capable to hold 5 elements) as follows:

int size = 5;
Swimmer[] swimmers = new Swimmer[size];

// note that at this point all elements of the arary are null!

// fill the array elements
for(int i=0; i<swimmers.length; i++) {
// initialize swimmer element i
swimmers = new Swimmer(...);
}

But it's important to know that an array's size is static, that means
that you cannot add more Swimmer elements to the array during runtime.
If you want something dynamic to add and remove Swimmers at runtime, use
a List, for example java.util.ArrayList.

Or, as in your case, if you want to create a GUI, use a ListModel (f.i.
javax.swing.DefaultListModel), which you can use in combination with
JList f.i., that is updated as soon as you add or remove Elements from
the ListModel.

One other thing: I hope you are aware that Calendar counts the months
from 0 - 11 (instead of 1 - 12 as one should think!) This is what causes
much confusion among newbies...

Hope I could help

Greetings,
Stefan Waldmann


stefan DOT waldmann AT web DOT de
 
J

justin

Roedy Green said:
For some generic advice about arrays see
http://mindprod.com/jgloss/gotchas.html#ARRAY

Thanks guys will look up into this...thanks heaps appreciate it..but
just another question...when you can create an array of Swimmer
(capable to hold 5 elements) as follows:

int size = 5;
Swimmer[] swimmers = new Swimmer[size];

// note that at this point all elements of the arary are null!

// fill the array elements
for(int i=0; i<swimmers.length; i++) {
// initialize swimmer element i
swimmers = new Swimmer(...);
}

where will i be coding this code? in the GUI class? under the public
void main (String ..... etc?)

and when i am entering the elements for swimmer in // initialize
swimmer element i
swimmers = new Swimmer(...);
}
do i put new Swimmer(Justin, Teh,etc);? because like shown in the
previous code of my attributes for swimmer...there is name etc, weight
(which is double and not string) i m just double checking to make
sure. thanks heaps!!!
 
A

Andrew Thompson

(snip help)
......
Thanks guys will look up into this...thanks heaps appreciate it..but
just another question...

Or two?
where will i be coding this code? in the GUI class? ....
do i put new Swimmer(Justin, Teh,etc);?

...or three? ;-)

You might be best helped by a
different group for the moment,
<http://www.physci.org/codes/javafaq.jsp#cljh>

A lot of the answers to your questions
might be found by looking over this code
that uses an array of 5 'ball' objects
in a cute little animation..
<http://www.physci.org/launcher.jsp#JAnimateFrame>

The rendering part is unimportant (to
your question), but the code might show
you how the an array is created at line
42, and filled with ball objects (52-54).

The 'Ball' class itself starts at line 127.

But please, if you need to ask further
qns. about making arrays, send 'em over
to c.l.j.help.

HTH
 
J

justin

Hie andrew...just referring back to the code in the ball
rendering...does my swimmer class have to be declared/defined in the
same class as the gui? as with the case of the ball which was in on
whole list/class
 
A

Andrew Thompson

On 23 Apr 2004 05:22:11 -0700, justin wrote:

(A.T.)
Hie andrew...just referring back to the code in the ball
rendering...does my swimmer class have to be declared/defined in the
same class as the gui? as with the case of the ball which was in on
whole list/class

ahemm..
I will be _happy_ to answer further
qn.s over at c.l.j.help

Hint, Hint!
 
A

Andrew Thompson

(Please not, this is a response to a thread
on c.l.j.programmer. The poster seems to
have found their way here so I will respond here)

Hie andrew...just referring back to the code in the ball
rendering...does my swimmer class have to be declared/defined in the
same class as the gui? as with the case of the ball which was in on
whole list/class

We need to be clear on terms.
The Ball class at line 127 is not 'in' the
GUI class, it is outside the GUI class's last

}

...and simply follows it.

It _is_ in the same .java file though, and that
is what might create confusion.

Most programmers would put Ball class
in a file called Ball.java, whereas I
have employed a few little tricks to
ensure I can keep it in the JAnimateFrame.java
source file.

The thing is, "every public class needs
to be in a 'MyClassName.java' file", right?

Indeed. The trick is, ball class is _not_ public.
Look closely at it. (again, that's line 127)

class Ball

This is not public or private or protected,
it is 'default'.

[ I wish Sun had required the specific word,
as I feel the overhead would have been quite
minor for the benefit of clarity, but.. ]

A default access class can be compiled in
another .java file quite happily.

I do that for technical reasons _particular_
to launching/displaying off the net, you can
see some other things you should watch out for here..
<http://www.physci.org/launcher.jsp>
The big yellow/cream box goes into further detail.

Bottom word is, the important 'bit' of each example
is described the in JavaDoc comments and usually
marked by comments just before it all happens.

OTOH - Ignore some of the things
I do in my on-line codes.

_Finally_ This is what you _should_ do

a) Make each class/interface a different .java
b) Put your classes in packages as soon as you
begin to get the hang of it - you will have a handy
'toolbox' before you know it
c) Give each class an appropriate access
level, a bad habit of most programmers is
to make everything public, which is in
itself a bad practise.

Anyway (checks watch) I need to make
web pages of the 1.4 src.zip today,
so I better get started! ;-)
 
A

Andrew Thompson

(Please not, this is a response to a thread
on c.l.j.programmer. The poster seems to
have found their way here so I will respond here)

D'Oh! And I did everything I meant to do in
that _except_ change the group to c.l.j.h.

My apologies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top