Attributes and methods can be accessed within a package.
1. Write Java statements that compares Objects, O1 and O2, using the getClass() method.
2. What does the instanceof operator do?
3. What are the different ways in which you can check the class of an Object?
4. Create a class to represent a Rectangle. Your class should contain instance variables for length and width, as well as member method to calculate the area and perimeter.
5. Create a test driver to test the functionality of your Rectangle class created in the previous question.
Practical Exercises on Inheritance
(To Be Done In Practical With Partner)
Practical Exercise 1
(make sure you complete this one it is needed for later questions)
The following is some code for a video game. There is an Alien class to represent a monster and an AlienPack class that represents a band of aliens and how much damage they can inflict:
class Alien {
public static final int SNAKE_ALIEN = 0;
public static final int OGRE_ALIEN = 1;
public static final int MARSHMALLOW_MAN_ALIEN = 2;
publicint type; // stores one of three types above
publicint health; //0 = dead 100 = full strength
public String name;
public Alien (int type, int health, String name) {
this.type = type;
this.health = health;
this.name = name;
}
}
classAlienPack {
private Alien[] aliens;
publicAlienPack(intnumAliens) {
aliens = new Alien[numAliens];
}
public void addAlien(Alien newAlien, int index) {
aliens[index] = newAlien;
}
public Alien[] getAliens() {
return aliens;
}
}
publicintcalculateDamage() {
int damage = 0;
for (inti=0; i<aliens.length;i++) {
if (aliens[i].type == Alien.SNAKE_ALIEN) {
damage += 10;//ooh damage from snake is 10
} else if (aliens[i].type = Alien.OGRE_ALIEN) {
damage += 6;//Ogres do only 6 damage
} else if (aliens[i].type == Alien.MARSHMALLOW_MAN_ALIEN) {
damage += 1; //barely tickled
}
}
return damage;
}
}
The code is not very object oriented and does not support information hiding in the Alien class. Rewrite the code so that inheritance is used to represent the different types of aliens instead for the “type” parameter. This should result in deletion of “type” parameter. Also rewrite the Alien class to hide the instance variables and create a getDamage method for each derived class that returns the amount of damage the alien inflicts. Finally, rewrite the calculateDamage method to use getDamage and write a main method that tests the code.
Practical Exercise 2
Give the definition of a class named Doctor whose objects are records for a clinic’s doctors. This class will be a derived class of the class SalariedEmployee given in Display 7.5 from the textbook. A doctor record has the doctors speciality (eg GP, Surgeon etc.. as a String) and office visit fee (as a double). Be sure your class has a reasonable complement of constructors, accessor and mutator methods and suitably defined equals and toString methods. Write a program to test all your methods.
Short Questions on Polymorphism
· Explain the difference between early and late binding.
· What is polymorphism and how does it relate to late binding?
· What are the advantages of polymorphism?
· Write a decision statement to determine if an object should be downcast.
· Describe the limitations of the copy constructor.
· What is an abstract method?
· What is wrong with the following method definition? public abstract void doSomething(int count)
· Why should the instanceOf operator be used in conjunction with downcasting?
· Draw an inheritance hierarchy to represent a shoe object. The base class should have derived classes of Dress Shoes, Tennis Shoes and Boots.
· Implement the base class in the shoe hierarchy in number 8 above.
· Derive a class named Dress Shoes from the base class created in number 9 above.
· Derive a class named Tennis Shoes from the base class created in number 9 above.
· Derive a class named Boots from the base class created in number 9 above.
· Override the clone method inherited in the Dress Shoes class created in number 10 above.
· Override the clone method inherited in the Tennis Shoes class created in number 11 above.
· Override the clone method inherited in the Boots class created in number 12 above.
Practical Exercises on Polymorphism
Practical Exercise 1
The goal for this exercise is to create a simple 2D predator-prey simulation. In this simulation, the prey is ants, and the predators are doodlebugs. These critters live in a world composed of a 20×20 grid of cells. Only one critter may occupy a cell at a time. The grid is enclosed, so a critter is not allowed to move off the edges of the grid. Time is simulated in time steps. Each critter performs some action every time step.
The ants behave according to the following rules:
· Move. Every time step, randomly try to move in one of the directions. If the selected direction is blocked the ant remains where it is.
· Breed. If an ant survives for three time steps, Then at the end of the third time step (ie after moving) the ant will breed. This is simulated by creating a new ant in an adjacent cell that is empty. If there is no empty cell available, no breeding occurs. Once an offspring is produced no more breeding will occur for 3 time steps.
The doodlebugs behave according to the following model:
· Move. If any adjacent cell has an ant the doodlebug and stay where it is. Otherwise it will move like the ant.
· Breed. After 8 moves it will spawn a baby in the same way as an ant.
· Starve. If after three moves the doodlebug has not consumed an ant it will die and be removed from the board.
During one turn. All the doodlebugs should be moved before the ants.
Write a program to implement this simulation and draw the world using ASCII characters of “o” for an ant and “X” for a doodlebug. Create a class called Organism that encapsulates basic data common to both ants and doodlebugs.
This class should have an overridden method named move that is defined in the derived classes of Ant and Doodlebug. You may need additional data structures to keep track of which critters have moved.
Initialise the world with 5 doodlebugs and 100 ants. After each time step, prompt the user to press enter to move to the next time step. You should see a cyclical pattern between the population of predators and prey, although random perturbations may lead to the elimination of one or both species