If it is not explicitly told in following recipe, setting up services are described for Ubuntu 20.04 server, but applications are described for Xubuntu 20.04 workstation. If you use different Ubuntu version or Linux distribution, settings as well as content, names and places of configuration files may be different!
Got it.

6. Abstract classes and interfaces

Review the presentation Abstract Classes and Interfaces

Tasks

Create a Human class

Explore the package jtm.activity06 and create a Human class that implements the Humanoid interface:

  1. Right-click the package main.java.jtm.activity06 and select New — Class
  2. Type class name: Human
  3. Click Add.. and select Humanoid in the main.java.jtm.activity06 package.
  4. In the Which method stubs would you like to create? section, select Inherited abstract methods
  5. Click Finish

Create the Martian class

Create the Martian class that implements the Humanoid, Alien, and Cloneable interfaces:

  1. Right-click the main.java.jtm.activity06 package and select New — Class.
  2. Enter class name: Martian
  3. In the interfaces section, click Add.. and select:
    1. Humanoid from the main.java.jtm.activity06 package,
    2. Alien from the main.java.jtm.activity06 package,
    3. Cloneable from the java.lang package.
  4. In the Which method stubs would you like to create? section, select Inherited abstract methods
  5. Click Finish

Implement the methods of the Martian and Human classes

Implement the methods as required, as described in the interface definitions and unit test checks.

error Notes:

  1. When Humanoid is hungry, it eats and gains weight. If Alien eats Humanoid, it kills it.
    1. When implementing Humanoid eat(), it must take an Integer, but Alien eat() must take an Object.
  2. When Humanoid vomits, it loses the weight of what it ate.
    1. Human vomit() only takes an Integer, but Alien can vomit any Object.
  3. If clone() is called on Martian, its structure must be cloned recursively for the contents of all "stomachs". This can be done by implementing the internal method clone(Object), for example:

    /*
    * 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 the toString() method for the Human and Martian classes in the following format:

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

    where ClassName is Human for a human, and Martian for a Martian.
    For example, if the current object is a Martian and it has another Martian in its "belly" and then a human with 3 in its belly, it should display:

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

More information


  

Created by Valdis Vītoliņš on 2025-01-28 17:34
Last modified by Valdis Vītoliņš on 2025-01-28 17:34
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License