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.

5. Inheritance and encapsulation

  1. Review the presentation Inheritance and Encapsulation
  2. Explore the jtm.activity05 package
  3. Create the class WaterRoad as a subclass of Road.
    1. Choose File — New — Class and enter:
    2. Name: WaterRoad
    3. Superclass: jtm.activity04.Road
    4. Select Constructors from superclass, Inherited abstract methods and press Finish
      Or, if you haven't done so in the class creation wizard: select Source — Generate Constructors from Superclass..., select both constructors Road() and Road(String,String,int) and OK.

    5. Choose Source — Override/Implement methods... and select the method toString().
    6. Replace the .toString() method, which returns a string in the following format: WaterRoad From — To, 00km
  4. Create the class Ship as a subclass of Transport.
    1. Create a protected byte field number of sails for the ship:
    2. Create a Ship(String id, byte sails) constructor,
    3. Replace move(Road) so that it returns a string of the form:
      ID Ship is sailing on (Road as String) with x sails

      where:

      • (Road as String) is the string representation of the road (without the parentheses),
      • x is the actual number of sails.

      Note that fuel is not used when the ship moves, as the wind is used for navigation instead.

      return Cannot sail on (Road as String) if it is not WaterRoad.

  5. Create a class Vehicle as a subclass of Transport.
    1. Create a protected int number of wheels field.
    2. Implement the Vehicle(String id, float consumption, int tankSize, int wheels) constructor,
    3. Replace the move(Road)) method for the vehicle, which returns a string of the form:
      ID Vehicle is driving on (Road as String) with x wheels

      where:

      • (Road as String) is the string representation of the road,
      • x is the actual number of wheels.

      return Cannot drive on (Road as String) if it is not a Road.

  6. Implement the Amphibia class so that it is a subclass of Transport:
    1. Make all internal fields of Amphibia private.
    2. Create a constructor Amphibia(String id, float consumption, int tankSize, byte sails, int wheels)
    3. Replace the move(Road road) method so that the Amphibia behaves like a Vehicle on a land road and a Ship on a water road.

lightbulb Tips

  1. Use .getClass().getSimpleName() to get the object type and reuse the Road.toString() method
  2. Use super(); to reference the parent class implementation of the method or constructor.
  3. Use if(road instanceof WaterRoad) to check if the object is a (sub)type of WaterRoad
  4. Use if(road.getClass() == Road.class) to check if the object is exactly Road type.

  

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