O'Reilly Forums: Chapter 8 Example Code - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 8 Example Code Is the code on page 207 complete?

#1 User is offline   wfc7 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 22-August 14

Posted 22 August 2014 - 02:25 PM

Hello,

The topic of polymorphism has been a bit challenging, and although I just finished the chapter, exercises and all, I'm still a bit confused about the "AnimalTestDrive" example shown on page 207. Is that supposed to be a complete code? I tried to compile it unsuccessfully. Thanks in advance!



public class MyAnimalList {

private Animal[] animals = new Animal[5];
private int nextIndex = 0;

public void add ( Animal d) {

animals[nextIndex] = d;
if (nextIndex < animals.length) {
System.out.println("Animal added at " + nextIndex);
nextIndex++;
}
}
}

-----------------------------------------------------------------------------------------

public class AnimalTestDrive implements Animal {
public static void main(String[] args) {

MyAnimalList list = new MyAnimalList();
Dog a = new dog();
Cat b = new cat();
list.add(a);
list.add(B);
}
}
0

#2 User is offline   always_java 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 02-November 14

Posted 02 November 2014 - 07:49 AM

The program given is perfect logically but however incomplete.
Inorder to have the full program follow the directions mentioned below:

1. make a dog class first. (dog.java) (you can choose to name each of them as dog.java,cat.java,(animal.java which is super class).
2. make a cat class .(cat.java)
3. inherit these above mentioned classes to an animal class.(animal.java)
4.Now create one more class as myanimallist and write the first block of code of page 207 on it save as seperate file.(myainimalist.java)
5. finally, make a class called animal test drive which has the main function mentioned at the second block on page 207.(animaltestdrive.java)
Run the project.
Be careful with the references and classes you use in all these files . This will definitely solve your problem...GOOD LUCK


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users