• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding Your Own Classes  RSS feed

 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1. In this particular instance, why is it not appropriate to make Monster[] and numberOfMonsters public?

2. In this particular instance, why is it not appropriate to make removeAMonster private?

3. What does the condition do?

4. What does the for loop do?

5. Assuming that the driver code compiles, explain what its use is?

Could anyone help me with these questions please! Thank you!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are your answers?
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not 100% sure with them, but my answer for:

3. if the monster exists in the array, then delete the monster and shuffle down, otherwise, do nothing?

4. the for loop just loops through the array and shuffles down all of the monsters by 1??

5. If the compiler code works, this just pretty much removes a monster in the array when called to do so (depending on which index is chosen)???
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3.if the monster exists in the array, then delete the monster and shuffle down, otherwise, do nothing?


Yes , better than that it just checks if you passed a valid index (assuming that numberOfMonsters has the right length of monsters)

4. the for loop just loops through the array and shuffles down all of the monsters by 1??


Well I think that the right direction of the shuffling it's left.

5. If the compiler code works, this just pretty much removes a monster in the array when called to do so (depending on which index is chosen)???


Too, but it does more. It reassigns the array object references (Monster) with the reference next to it (the ref. from the right). This goes from the given index till the penultimate one.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use <= number - 2 That is confusing to read. Use < and - 1.
You realise that what will happen is that the last monster will be duplicated and gradually move up to fill the array; you need to set the reference to the last monster to null when you remove a monster.
 
Mark Do
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome....so, just to tidy things up, would these answer be appropriate?

1. What does the if-condition do?
The if-condition checks whether or not the monster specified is in the zoo. The monster gets deleted if it is.
2. What does the for loop do?
It reassigns all of the monster’s value (by more than 1) that precedes the deleted monster.
3. Assuming that the driver code compiles, explain what it’s use is?
When a specific monster is identified, it gets deleted in the zoo and the array object references (Monsters) get reassigned with the reference next to it from the targeted monster to the penultimate one.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!