OOP Lab

1. Indicate which variant of foo will be called or write "error."

class AAA {
   void foo(int x, boolean y) { ... } //v1
   void foo(double y) { ... } // v2
}

class BBB extends AAA {
   void foo(boolean x, int y) { ... } // v3
   void foo(double z) { ... } // v4
   public static void main(String[] args) {
      AAA x = new AAA();
      AAA y = new BBB();
      BBB z = new BBB();

      x.foo(3, false);
      x.foo(10);

      y.foo(3, false);
      y.foo(true, 2);
      y.foo(10);

      z.foo(3, false);
      z.foo(true, 2);
      z.foo(10);
   }
}

 

3. A software company requires each class to have a non-static, parameterless method called test. This method tests the class and returns true if the class passes the test and false otherwise.

Complete the implementation of the following test driver:

class TestDriver {
   public boolean test(Collection<String> classNames) { ??? }
}

TestDriver.test gets a collection of class names as its input. It loads each class and invokes its test method. If all of the tests pass, true is returned, otherwise false is returned. Note: All tests must be run, even if some fail.

 3. A group of monsters attack a hero. They take turns decrementing the hero's health until the hero dies (health == 0). The monsters must not attempt to decrement the hero's health at the exact same time, and the monsters must be cooperative. Here's the implementation of the Warrior class:

class Warrior {
   private int health = 100;
   public void addHealth(int amt) { health += amt; }
   public void subHealth(int amt) { health -= amt; }
   public int getHealth() { return health; }
}

Finish the implementation of Monster:

class Monster extends Thread {
   private static Warrior hero = new Warrior();
   // etc.
}

4.  A command is anything that implements the Command interface:

interface Command {
   Object execute(); // returns null or a result
}

There are four types of commands: Condition, Iteration, Expression, and Block. A condition has the form

if (EXPRESSION) COMMAND else COMMAND

An iteration has the form:

while (EXPRESSION) COMMAND

A block is a sequence of one or more commands of the form:

{COMMAND; ...; COMMAND;}

Executing an expression returns a result. For example, executing the expression "3 < 2" returns the result false; executing all other types of commands returns null.

A. Draw a UML class diagram showing how the concepts mentioned are related.

B. Implement the Block class.

7. Many creatures hide in a cave: snakes, spiders, scorpions, etc. When an explorer enters the cave, the creatures attack.

Draw a UML class diagram showing how you would model a cave and its occupants.

Implement your design. For now the attack methods can simply produce some console output.