This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Hey Mike, I need an idea for this one Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hey Mike, I need an idea for this one" Watch "Hey Mike, I need an idea for this one" New topic
Author

Hey Mike, I need an idea for this one

Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I am supposed to write statements to declare an array of 10 Animal named myOtherPets and randomly add 10 Pig�s or Duck�s into myOtherPets
I approached it like the arraylist problem but this is just a strait up array.
This is what I have so far, but I have no idea how to fill that array.
ideas?
Thanks bro

[ April 18, 2003: Message edited by: Steve Wysocki ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Do you need a random aggregation of Pigs and Ducks or an equal number of each in no particular order? If the former then you would need to do something like this:


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I need 1 or the other, I am choosing pigs on this one.
but, I need them to say Hello, just like the last one.
All of them need to use speak("Hello")
output example
Pig0
Oink Oink: Hello
The Pig class is the same exact class as the duck class in the other example but with pigs instead of ducks
Thanks Mike
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Well, try what I posted and follow that with something like:

Of course testing with instanceof may be defeating the ultimate purpose of this assignment which is to show the effects of polymorphism.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Well this is how I put it together, Im sure Im missing something somewhere cause it bombs out with:java.lang.NullPointerException
at project9_1.Test2.main(Test2.java:20)
Pig4
Exception in thread "main"
The Pig number will every time I run it.
Thanks Mike


[ April 18, 2003: Message edited by: Steve Wysocki ]
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
You know what Mike, I am going to make this alot more simple but it doesn't work.
it will count the pigs but it wont do the speak
it gives a null pointer exception error:
java.lang.NullPointerException
at project9_1.Test2.main(Test2.java:20)
Exception in thread "main" Pig6
This code SHOULD work. It follows num1 (the random number, but the error persists.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
GOT IT!!
I got yours to sort of work, but I looked at your instantiated pig object and realized thats what was causing my NPE so this worked:

Thanks for the awesome assistance on this one Mike.
Go Spurs Go
[ April 18, 2003: Message edited by: Steve Wysocki ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Once again Steve, you really did this one on your own. I wimped out and went to bed and left you hangin'. I've always believed the best way to learn a programming language is to screw up and figure out why you screwed up. That is the most valuable kind of experience.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Well, at least Im getting better at it. Your a better teacher than mine.
Anyway, you still helped a bunch, and I really appreciate it.
I'll talk to ya soon.
Cya
SW
GO SPURS GO
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Keep up the hard work. I enjoy helping you out, but as Mike says, you'll learn a lot more from your own mistakes. Keep coming back with more questions. We are glad to help point you in the right direction.
Cheers!
Layne


Java API Documentation
The Java Tutorial
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
my 2 cents.
also, once u know the 'wrong/bad' code, u learn to see the significance of 'better' code u know...i mean more on the code design tho..
regards
maulin
 
 
subject: Hey Mike, I need an idea for this one
 
Similar Threads
Methods
I know its goofy but Im having problems with it
The Ducks are back
Question Randomization in Access DB
Help with random class