Prev
5. Inheritance and Encapsulation
Next
7. UML

6. exercise: Abstract classes and Interfaces

Tasks

General info

  1. Investigate jtm.activity06 package

Create Human class

Create Human class which implements Humanoid interface:

  1. Right-click on package main.java.jtm.activity06 and select New — Class
  2. Enter class name: Human
  3. Press Add.. in interfaces section and select Humanoid in main.java.jtm.activity06 package
  4. For section Which method stubs would you like to create? select Inherited abstract methods
  5. Press Finish
  6. Create constructors for Human class:
    1. Constructor without parameters, which sets weight of human to 42
    2. Constructor with one int parameter to set initial weight for Human

Create Martian class

Create Martian class, which implements Humanoid, Alien and Cloneable interfaces:

  1. Right-click on package main.java.jtm.activity06 and select New — Class
  2. Enter class name: Martian
  3. Press Add.. in interfaces section and select:
    1. Humanoid from main.java.jtm.activity06 package,
    2. Alien from main.java.jtm.activity06 package,
    3. Cloneable from java.lang package.
  4. For section Which method stubs would you like to create? select Inherited abstract methods
  5. Press Finish
  6. Create constructor for Martian class with one int parameter to set initial weight for Martian

Implement methods of Martian and Human classes

Implement methods according to the required logic, which is described in interface definitions and expected results from unit tests.

error Notes:

  1. Alien eats Humanoid only if it is alive and gains its weight.
  2. For Humanoid interface getBackpack() should return Object, but for Human class getBackpack() should return just String.
  3. For Humanoid interface setBackpack() should accept String, but for Martian class setBackpack() should accept Object.
  4. When implementing setBackpack() for Martian, check that Martian cannot put in backpack himself.
  5. When getBackpack() is called for Martian it should return new object with the same values.
  6. When clone() is called for Martian, its structure should be cloned recursively for content of all backpacks. You can do it by implementing internal method clone(Object) like this:

    /*
     * Implementation of Object clone() method for Cloneable interface
     * @see https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html
     */
    @Override
        public Object clone() throws CloneNotSupportedException {
        return clone(this);
    }

    private Object clone(Object current) {
        // TODO implement cloning of current object
        // and its backpack
    }
  7. Implementation of toString() method for Human and Martian classes should be:

    @Override
    public String toString() {
        return "ClassName: " + weight + " [" + backpack + "]";
    }

    where ClassName is Human for human and Martian for martian.
    For example, if current object is martian and its backpack contains another martian and then human with "Some string" in its backpack, it should show:

    [Martian: 1 [Martian: 2 [Human: 3 [Some string]]]

Prev
5. Inheritance and Encapsulation
Next
7. UML
Tags:
Created by Valdis Vītoliņš on 2017-01-03 10:07
Last modified by Valdis Vītoliņš on 2018-07-04 16:14
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License