In: Computer Science
CS
Using the following UML outline, create the following abstract
parent class along with the
4 subclasses and Driver class. Implement the Classes, listed
attributes and methods along with any additional things that you
may need.
Add a driver class that utilizes the methods/attributes in the classes to output the following based on the vehicle type (therefore, you must create an instance for all 4 subclasses in your driver):
1. Vehicle Type (i.e., Car, Airplane, etc.)
2. Transportation method (wheels, wings, etc.)
3. Whether your vehicle transports passengers or not.
4. Whether your vehicle is a passenger vehicle or Cargo
Vehicle
5. Whether your vehicle travels on land, water or air.
Implement the following UML:
UML
Vehicle Class (Abstract)
Attributes:
-vehicleType:String
-passengers:boolean
Behaviors:
+Vehicle(vehicleType:String, passengers:boolean):void
+Abstract travelMethod(vehicleType:String):String
+getPassengers():boolean //returns passengers value
+getVehicleType():String
Child Class1:
Car
Attributes:
-travelMethod:String
Behaviors:
+travelMethod(vehicleType:String):String //Returns travelMethod
based on vehicleType
for instance if helicopter, the travelMethod = Blades/Propellers,
if car then
travelMethod = wheels, if airplane then wings, etc.
+isVehicle():void. //Outputs vehicle type, transportation method, whether it is a passenger vehicle or cargo vehicle and whether it rolls with wheels, flies with wings or flies with blades, or travels on water.
Child Class2:
Airplane
Attributes:
-travelMethod:String
Behaviors:
+travelMethod(vehicleType:String):String //Returns travelMethod
based on vehicleType
for instance if helicopter, the travelMethod = Blades/Propellers,
if car then
travelMethod = wheels, if airplane then wings, etc.
+isVehicle():void. //Outputs vehicle type, transportation method, whether it is a passenger vehicle or cargo vehicle and whether it rolls with wheels, flies with wings or flies with blades, or travels on water.
Child Class3:
Helicoptor
Attributes:
-travelMethod:String
Behaviors:
+travelMethod(vehicleType:String):String //Returns travelMethod
based on vehicleType
for instance if helicopter, the travelMethod = Blades/Propellers,
if car then
travelMethod = wheels, if airplane then wings, etc.
+isVehicle():void. //Outputs vehicle type, transportation method, whether it is a passenger vehicle or cargo vehicle and whether it rolls with wheels, flies with wings or flies with blades, or travels on water.
Child Class4:
Boat
Attributes:
-travelMethod:String
Behaviors:
+travelMethod(vehicleType:String):String //Returns travelMethod
based on vehicleType
for instance if helicopter, the travelMethod = Blades/Propellers,
if car then
travelMethod = wheels, if airplane then wings, etc.
+isVehicle():void. //Outputs vehicle type, transportation method, whether it is a passenger vehicle or cargo vehicle and whether it rolls with wheels, flies with wings or flies with blades, or travels on water.
Deliverables:
1. Parent Class for Vehicle, 4 child classes and 1 Driver class.
2. Screenshots of output
3. Place your name, class, date and assignment in comments at top of each .java file. (Points off for files with no name)
Code for all 5 classes is given below. Code is explained thoroughly in the code comments.5 Classes are : Vehicle , Car, Helicoptor , Airplane , Boat , Driver . The code runs smoothly. As you have not given the Driver class, i have made it on my own to test the program You can run the program according to your liking by giving different values to vehicles in Driver class objects. I have attached the output screenshot in the last.
If need any further clarification please ask in comments.
###########################################################################
CODE-->>>
Vehicle Class
//Abstract super class Vehicle
abstract class Vehicle
{
//private data members
private String vehicleType;
private boolean passengers;
//public constructor
public Vehicle(String vehicleType, boolean passengers) {
this.vehicleType = vehicleType;
this.passengers = passengers;
}
//abstract method
abstract String travelMethod(String vehicleType);
//Accessor functions
public String getVehicleType() {
return vehicleType;
}
public boolean isPassengers() {
return passengers;
}
}
Car class
//child class Car
class Car extends Vehicle
{
//private data member
private String travelMethod;
//constructor
public Car(String vehicleType, boolean passengers)
{
//Calling super class Constructor
super(vehicleType,passengers);
this.travelMethod="Wheels"; //setting value of travelmethod
}
//defining abstract method
public String travelMethod(String vehicleType)
{
return travelMethod;
}
//function to display info
public void isVehicle()
{
System.out.println("\nVehicle Type: "+super.getVehicleType());
if(super.isPassengers()) //if it is passenger vehicle
System.out.println("It is a Passenger Vehicle");
else //if it is cargo
System.out.println("It is a Cargo Vehicle");
System.out.println("Transportation Method: "+travelMethod);
System.out.println("It rolls with Wheels");
}
}
Airplane Class
//child class Airplane
class Airplane extends Vehicle
{
private String travelMethod;
//constructor
public Airplane(String vehicleType, boolean passengers)
{
super(vehicleType,passengers); //calling super class constructor
this.travelMethod="Wings";
}
//defining abstract method
public String travelMethod(String vehicleType)
{
return travelMethod;
}
//function to display info
public void isVehicle()
{
System.out.println("\nVehicle Type: "+super.getVehicleType());
if(super.isPassengers()) //if passenger vehicle
System.out.println("It is a Passenger Vehicle");
else //if cargo
System.out.println("It is a Cargo Vehicle");
System.out.println("Transportation Method: "+travelMethod);
System.out.println("It Flies with Wings");
}
}
Helicoptor Class
//child class Helicoptor
class Helicoptor extends Vehicle
{
private String travelMethod;
//constructor
public Helicoptor(String vehicleType, boolean passengers)
{
super(vehicleType,passengers); //super class constructor
this.travelMethod="Blades";
}
public String travelMethod(String vehicleType)
{
return travelMethod;
}
//function to display info
public void isVehicle()
{
System.out.println("\nVehicle Type: "+super.getVehicleType());
if(super.isPassengers())
System.out.println("It is a Passenger Vehicle");
else //if cargo
System.out.println("It is a Cargo Vehicle");
System.out.println("Transportation Method: "+travelMethod);
System.out.println("It flies with Blades");
}
}
Boat class
//child class boat
class Boat extends Vehicle
{
private String travelMethod;
//constructor
public Boat(String vehicleType, boolean passengers)
{
super(vehicleType,passengers); //calling super class constructor
this.travelMethod="Propeller"; //setting value of travelMethod
}
//defining abstract method
public String travelMethod(String vehicleType)
{
return travelMethod;
}
//function to display info
public void isVehicle()
{
System.out.println("\nVehicle Type: "+super.getVehicleType());
if(super.isPassengers()) //if passenger vehicle
System.out.println("It is a Passenger Vehicle");
else //if cargo vehicle
System.out.println("It is a Cargo Vehicle");
System.out.println("Transportation Method: "+travelMethod);
System.out.println("It travels on Water");
}
}
Driver Class
*Name:
* Class:
* Date:
*/
//driver class
public class Driver {
public static void main(String[] args)
{
//creating object of each class
Car car=new Car("Car", true);
Helicoptor helicoptor=new Helicoptor("Helicoptor", true);
Airplane airplane=new Airplane("Airplane", false);
Boat boat=new Boat("Boat", false);
//calling isVehicle() method to display info
car.isVehicle();
helicoptor.isVehicle();
airplane.isVehicle();
boat.isVehicle();
}
}
####################################################################
OUTPUT