Note, that this recipe is not updated long time and could be outdated!
Got it.

7. Abstract classes and Interfaces

Tasks

General info

  1. Review presentation Abstract classes and Interfaces
  2. Review notes about practical exercise
  3. 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

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

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. When Humanoid is hungry, it eats and gains weight. If Alien eats Humanoid it kills it.
    1. Implementation of Humanoid eat() should accept Integer, but implementation of Alien eat() should accept Object.
  2. When Humanoid vomits, it loses weight of eaten item.
    1. Human vomit() only Integer, but Alien vomit any Object.
  3. When clone() is called for Martian, its structure should be cloned recursively for content of all stomaches. 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 stomach
    }
  4. Implement toString() method for Human and Martian classes in following format:

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

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

    [Martian: 3 [Martian: 4 [Human: 5 [3]]]

Optional information


  

Created by Valdis Vītoliņš on 2020-05-28 10:55
Last modified by Valdis Vītoliņš on 2021-04-13 14:31
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License