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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
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