• Post Reply Bookmark Topic Watch Topic
  • New Topic

So confused on creating an array  RSS feed

 
Valerie Martin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, I am VERY new to Java, and I have an assignment I would love some help with. I have to take this program and rewrite it as a LoopTest. Create an array of Animal and then loop through the array using an enhanced for () , calling the describe(), sound(), sleep(), and move(), methods successively. And then change the LoopTest to use an ArrayList instead of an array. I am so confused..can someone help me please??


 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for starters, take a look at the code you need to modify. There is basically three blocks; one to create your objects, one to describe the cat, and the other for the bird. What commands in those blocks are repetative? This is the code you'll want to run in your loop. Your book should have syntax for an enhanced for loop.

As for the array and arraylist; it looks like you have to write your program once using an array, and once using an arraylist. Beings (I'm assuming) cat and robin inherit from animal, you should create an array and arraylist of animal objects
 
Valerie Martin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replying, but I am VERY new to Java, and really have no idea what it is talking about. I have read it 4 times now...thanks anyway!
 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with what's happening in the original code?
 
Valerie Martin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To a point. I know that they have the methods in common.
 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has your class covered loops and arrays?
 
Valerie Martin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not really "in" a class, its all online (not my idea btw) and our teacher only covers what he has worked on in the past. He won't even answer emails from students...I am so frustrated!!
 
kunagu varun
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to learn using arrays and arraylists, your problem is similar to the one which is used to describe various concepts like polymorphism and arraylists in the book Head First Java... I think if you go through those chapters and understand those concepts, this problem would be a piece of cake
 
Vince Stout
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's unfortunate. If possible, I would suggest changing classes, but I know that's not always an option. Here are two links that will hopefully help you

This, is a link to the new bostons java tutorials. They are video tutorials and will walk you through from the ground up.

And here is a link to oracles java tutorial trail.

Between those two sites you should be able to get a good grasp on the basics.

 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Are you paying for this online course?
 
D. Wang
Greenhorn
Posts: 3
Eclipse IDE Java Windows Vista
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, you will need to know how to initilize an array:

You have the type (in this case, Animal), followed by [] (the array symbol). Then you name the array. I named it animals in this example, but you can name it whatever you want. The new Animal[] actually creates the array.
You can also set a specific size to the array:

This sets the size of the array to 6. Keep in mind, the array goes from animals[0] to animals[5].
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Wang wrote:First off, you will need to know how to initilize an array:


That's not legal syntax. You have to specify the array's size when you create it.

You have the type (in this case, Animal), followed by [] (the array symbol). Then you name the array.


No, you name the reference variable, not the array object it points to. Objects (including arrays) don't have names.

You can also set a specific size to the array:


"Must", not "can also".

And, to complete the nickel tour, one common gotcha is that an array of reference type (as opposed to an array of primitives, such as int[]) initializes all its elements to null. So new Animal[6] does not create 6 Animal objects, just an array with 6 null Animal references.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Wang wrote: . . . You have the type (in this case, Animal), followed by [] (the array symbol . . .
Not at all. In that case you have the type, Animal[], ie array of Animals. The [] operator is part of the type.

I am sure it was a mistake to allow Animal zoo[]; or similar, but they seem to have kept it as correct C++ syntax and we can’t now get rid of it.

And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!