Atpakaļ
5. Inheritance and Encapsulation
Tālāk
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
    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() returns Object, but for Human class getBackpack() should return String.
  3. For Humanoid interface setBackpack() accepts String, but for Martian class setBackpack() should accept Object.
  4. When getBackpack() is called for Martian it should return new object with the same values.
  5. When clone() is called for Martian, its structure should be cloned recursively for all backpacks.
  6. Beware of any other undocumented requirements coming from unit tests.

Atpakaļ
5. Inheritance and Encapsulation
Tālāk
7. UML
Tagi:
Izveidojis Valdis Vītoliņš 2017-01-03 10:07
Pēdējais mainījis Valdis Vītoliņš 2017-09-13 17:19
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License