Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hey Mike, I need an idea for this one

 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic