• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with creating an array of objects to be used to spawn in another class object  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, so this is a Greenfoot project, but a lot of it has to do with Java so I will keep it strictly with Java terms. I need to create an array of objects called "BallInfo" that will be used to create a "Ball" object. Now the parameter for a Ball object contains a reference to the BallInfo class, for example it looks like this in the Ball constructor:



I have read in values from a file to spawn in a BallInfo object that takes three integer values representing red, green, and blue values of a Ball. Here is the code for that:



I just need help making an array of "BallInfo" objects to be used in the creation of a "Ball" object. Any tips on how I can use the values I read in to make an array of BallInfo objects, and then use that array to spawn in "Balls?"
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you been told to use that technique? Why are you using arrays? We usually think that somebody asking how to use XYZ to do something shou‍ld think about different ways to do it. Look at this FAQ.

I can see all sorts of dubious things about that code.
  • Does that constructor simply initialise a Ball object? Or is it doing all sorts of things, which shou‍ld be done by other code? Is that image a field of Ball?
  • It isn't clear what the ball info object is doing. It appears to provide a colour and nothing else. What else is in BallInfo and where are you using it? If it only provides colours, why do you need the BallInfo class at all? Why not use Color? Why not use the three ints red green and blue?
  • You are simply copying the BallInfo object in line 3. If BallInfo is a mutable class, there is a risk of it changing its state elsewhere and breaking the Ball object.
  • Where do all those numbers come from? Why are balls at intervals of 12? Why do they always move at a speed of 0.05? Why are you using floating‑point numbers if pixels are counted in integers?
  • What does the addObject method do? That method's purpose isn't obvious to me from its name.
  • I think you shou‍ld go back and write down how you are getting the data from the file.
    Remember that arrays have predetermined sizes. How many lines are there? How do you know how many lines there are? Is there a way to get the information from the file to the Ball object without any intermediaries? What other data structures have you considered?
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!