Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How can I associate certain things to an array?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Reality Check




PostPosted: Wed May 23, 2007 10:04 am   Post subject: How can I associate certain things to an array?

I have an array of 9 balls and I want each ball to have certain attributes such as velocity, radius and whatnot. I could just make an array for each attribute but that's too much. Something like record in turing I guess. This is for a pool game by the way.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Wed May 23, 2007 10:23 am   Post subject: Re: How can I associate certain things to an array?

I think you should make an array of Ball objects, and of course you create the Ball class. That way can not only have data but you can have methods that handle all of the actions of each ball (instead of static functions in the main class that act on an entire array).

Java:
class Ball {
        public Ball (double x_, double y_, String clr_) {
                double x = x_;
                double y = y_;
                String clr = clr_;
                double vx, vy = 0;
        }
}
PaulButler




PostPosted: Wed May 23, 2007 10:26 am   Post subject: RE:How can I associate certain things to an array?

This is what classes are for. Classes are important in Java so you should really look them up to get a better idea of how they work, but here is how you would do what you want to do with classes:

Java:

// put this in ball.java
class Ball {
// note that we are assigning default values, which is not necessary
    public double radius = 10;
    public double velocity = 5;
    public double direction = 210;
}

// and then to use the class
Ball b = new Ball();
b.radius = 23;
b.velocity = 14;
// or with an array
Ball[] c = new Ball[9];
c[2] = new Ball();
c.velocity = 3;


To really make good use of classes you need to use a constructor, if you search around these forums I'm sure there is a java classes tutorial... If not, google "java classes" or something along those lines.
wtd




PostPosted: Wed May 23, 2007 12:12 pm   Post subject: RE:How can I associate certain things to an array?

And you use accessors, rather than public instance variables.
wtd




PostPosted: Wed May 23, 2007 2:06 pm   Post subject: Re: How can I associate certain things to an array?

Another suggestion: you may wish to use a Map.
Reality Check




PostPosted: Wed May 23, 2007 2:11 pm   Post subject: Re: How can I associate certain things to an array?

Wow this is exactly what I'm looking for! Thanks a lot guys.
PaulButler




PostPosted: Wed May 23, 2007 6:13 pm   Post subject: Re: RE:How can I associate certain things to an array?

wtd @ Wed May 23, 2007 12:12 pm wrote:
And you use accessors, rather than public instance variables.


I know this is generally good practice, but why is this? I guess if you were going to change how the data was handled it would be nice to be able to keep the same interface, but for a small project like this is there any benefit to doing this?
richcash




PostPosted: Wed May 23, 2007 7:05 pm   Post subject: Re: How can I associate certain things to an array?

Encapsulation. You shouldn't be able to directly manipulate data (you have to look at the class). Everything is handled through methods acting on data in good oop code. Classes can be reused, which is why it's good to write them well.

Maybe someone else can list other advantages.
Sponsor
Sponsor
Sponsor
sponsor
Reality Check




PostPosted: Wed May 23, 2007 7:19 pm   Post subject: Re: How can I associate certain things to an array?

Do I have to make a separate class for each ball or can I simply put:
code:

Ball a = new Ball ();
Ball b = new Ball ();
Ball c = new Ball ();

//and then just do:

c.radius = 10;
b.velocity += 1;
a.radius = 9;


If I can do that then this is awesome. I've been researching classes in the last hour or so and they seem very very useful and pretty easy to use once you get the hang of it.
richcash




PostPosted: Wed May 23, 2007 8:27 pm   Post subject: Re: How can I associate certain things to an array?

code:
Ball a = new Ball ();
Ball b = new Ball ();
Ball c = new Ball ();

Yes, you can. Smile But even better, you can have an array of objects.
Java:
Ball[] ball = new Ball[5];//an array of 5


code:
c.radius = 10;
b.velocity += 1;
a.radius = 9;

Technically, you can do this. But it's a better habit to use accessors (and constructors). Have a look at wtd's java tutorial. It shows you the right way to program in java!
Reality Check




PostPosted: Wed May 23, 2007 10:44 pm   Post subject: Re: How can I associate certain things to an array?

I read the part on Accessors and the stuff before it and I want to clarify my understanding. We're making methods within the ball class that simply return the value of said variable (lets say Velocity). To then change my Velocity I must call the method and return the changed value. I'm not sure what the advantage is to that but I'll do it I guess. Not too sure how the coding would work though. Would it be like:
code:

v = c.getVelocity ();
radius = a.getRadius ();


This is my last question I promise Very Happy
richcash




PostPosted: Wed May 23, 2007 11:47 pm   Post subject: Re: How can I associate certain things to an array?

Hmm, let me show you an example class :
Java:
class Ball {
        private String colour;
        public void setColour(String clr_) {
                colour = clr_;
        }
        public String getColour() {
                return colour;
        }
}

And then in your main program :
Java:
Ball cue = new Ball();
cue.setColour("White");
System.out.println(cue.getColour()); //outputs white


But, wouldn't it be nice if we could give our object all its starting data at one time? This is where constructors become useful :
Java:
class Ball {
        private double x, y, vx, vy, radius;
        private String colour;
        public Ball(double init_x, double init_y,
                        double init_vx, double init_vy, double init_radius,
                        String init_colour) {
                x = init_x;
                y = init_y;
                vx = init_vx;
                vy = init_vy;
                radius = init_radius;
                colour = init_colour;
        }
}


And to create a cueball we just have to :
Java:
Ball cue = new Ball(100, 100, 0, 0, 15, "white");


But constructors are just for assigning an initial value conveniently (they are called when you create a new object). You still have to use accessors along with it because you'll want to change and read the data of your objects later on in the program.


Reality Check wrote:
This is my last question I promise

Asking questions is good.
Reality Check




PostPosted: Thu May 24, 2007 7:11 am   Post subject: Re: How can I associate certain things to an array?

Thanks a lot. I actually understand the importance of this. I learned a lot these last 2 days!!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: