Creating a Inheritance and Polymorphism Application in Java

Problem:

Vehicle
- manufacturer : String
- name : String
- engine : Engine
 - body : Body
- profit : double
+ Vehicle(manufacturer : String, name : String, engine : Engine, body : Body, profit : double) 
+ setManufacturer(manufacturer : String) : void
+ getManufacturer() : String
 + setName(name : String) : void
 + getName() : String
 + setEngine(engine : Engine) : void
+ getEngine() : Engine
+ setBody(body : Body) : void
+ getBody() : body
+ setProfit(profit : double) : void
 + getProfit() : double
 # abstract calculateTatalPrice() : double
+ toString() : String


Car
- doors : int
- gear : String
 - gearPrice : double
+ Car(manufacturer : String, name : String, engine : Engine, body : Body, profit : double, doors : int, gear : String, gearPrice : double)
+ setDoors(doors : int) : void
+ getDoors() : int
 + setGear(gear : String) : void
 + getGear() : String
+ setGearPrice(gearPrice : double) : void
 + getGearPrice() : double
+ calculateTotalPrice() : double
+ toString() : String     


Train
- compartments : int
- fuel : String
- cornpartmentPrice : double
+ Train(manufacturer : Stricw, name : String, engine : Engine, body : Body, profit : double, compartments : int, fuel : String, compartmentPrice : double)
+ setCompartments (compartments : int) : void
+ getCompartments () : int
+ setFuel(fuel : String) : void
 + getFuel() : String
 + setCompartmentPrice(compartmentPrice : double) : void
+ getCompartmentPrice () : double + calculateTotalPrice() : double
+ toString() : String


Engine
- id : String
- cost : double 
+ Engine(id : String, cost : double)
+ setld(id : String) : void
+ getld() : String
+ setCost(cost : double) : void
 + getCost() : double
 + toString() : String


Body
- width :
- height : double
- seats : int
 - cost : double
+ Body(width : double, height : double, seats: int, cost : double)
+ setWidth(width : double) : void
+ getWidth() : double
 + setHeight(height : double) : void
+ getHeight() : double
 + setSeats(seats : int) : void
+ getSeats() : int
+ setCost(cost : double) : void
+ getCost() : double
+ toString() : String

In this lab, you will be using inheritance to create a car and a train.
The Vehicle class is made up of an Engine and a Body and some other variables. It has setter and getter methods and a method called calculateTotalPrice which should be implemented by subclasses of Vehicle.
The Car class and the Train class are both subclasses of Vehicle. This means that you have to use inheritance to create both classes. Each of these classes' constructors must call the superclass constructor. Moreover, both of these classes have to implement the calculateTotalPrice method which has to sum up the total price of the vehicle plus the compartments' price if it is a train or the gear price if it is a car.
Write a tester class which has an array of Vehicles.
The array should contain two cars and two trains.
 Printing format is as follows:
(manufacturer) (name)
Engine:
(id) $(cost)

Body:
 (width)x(height)M
 (seats) seats
 (compartments) compartments (if train)
(doors) doors (if car)

Price:
 $(calculateTotalPrice())

Solution:
/**-----------------------Vehicle.java-------------------*/
public abstract class Vehicle {
 
 private String manufacturer;
 private String name;
 private Engine engine;
 private Body   body;
 private double profit;
 
 public Vehicle(String manufacturer, String name, 
                  Engine engine, Body body, double profit) {
  this.manufacturer = manufacturer;
  this.name         = name;
  this.engine       = engine;
  this.body         = body;
  this.profit       = profit;
 }

 public String getManufacturer() {
  return manufacturer;
 }

 public void setManufacturer(String manufacturer) {
  this.manufacturer = manufacturer;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Engine getEngine() {
  return engine;
 }

 public void setEngine(Engine engine) {
  this.engine = engine;
 }

 public Body getBody() {
  return body;
 }

 public void setBody(Body body) {
  this.body = body;
 }

 public double getProfit() {
  return profit;
 }

 public void setProfit(double profit) {
  this.profit = profit;
 }
 
 protected abstract double calculateTotalPrice();
 
 public String toString() {
  return manufacturer + " " + name + 
                     "\n\nEngine:\n" + engine.getId() + 
                   " $" + engine.getCost() + "\n\nBody:\n" + 
                    body.getWidth() + "x" + body.getHeight() +
                        "M\n" + body.getSeats() + " seats\n";
 }
}

/**-----------------------Car.java-------------------*/
public class Car extends Vehicle {

 private int    doors;
 private String gear;
 private double gearPrice;
 
 public Car(String manufacturer, String name, Engine engine,
                  Body body, double profit, int doors, String gear, 
                                                 double gearPrice) {
  super(manufacturer, name, engine, body, profit);
  this.doors     = doors;
  this.gear      = gear;
  this.gearPrice = gearPrice;
 }

 public int getDoors() {
  return doors;
 }

 public void setDoors(int doors) {
  this.doors = doors;
 }

 public String getGear() {
  return gear;
 }

 public void setGear(String gear) {
  this.gear = gear;
 }

 public double getGearPrice() {
  return gearPrice;
 }

 public void setGearPrice(double gearPrice) {
  this.gearPrice = gearPrice;
 }

 @Override
 protected double calculateTotalPrice() {
  return getEngine().getCost() + 
                   getBody().getCost() + getProfit() + gearPrice;
 }
 
 public String toString() {
  return super.toString() + doors 
         + " doors\n\nPrice:\n$" + calculateTotalPrice() + "\n\n";
 }
}
/**-----------------------Train.java-------------------*/
public class Train extends Vehicle {

 private int    compartments;
 private String fuel;
 private double compartmentPrice;
 
 public Train(String manufacturer, String name, Engine engine,
                 Body body, double profit, int compartments, 
                            String fuel, double compartmentPrice) {
  super(manufacturer, name, engine, body, profit);
  this.compartments = compartments;
  this.fuel = fuel;
  this.compartmentPrice = compartmentPrice;
 }

 public int getCompartments() {
  return compartments;
 }

 public void setCompartments(int compartments) {
  this.compartments = compartments;
 }

 public String getFuel() {
  return fuel;
 }

 public void setFuel(String fuel) {
  this.fuel = fuel;
 }

 public double getCompartmentPrice() {
  return compartmentPrice;
 }

 public void setCompartmentPrice(double compartmentPrice) {
  this.compartmentPrice = compartmentPrice;
 }

 @Override
 protected double calculateTotalPrice() {
  return getEngine().getCost() + getBody().getCost() 
                    + getProfit() + (compartments*compartmentPrice);
 }

 public String toString() {
  return super.toString() + compartments + 
        " compartments\n\nPrice:\n$" + calculateTotalPrice() + "\n\n";
 }
}

/**-----------------------Engine.java-------------------*/
public class Engine {
 
 private String id;
 private double cost;
 
 public Engine(String id, double cost) {
  this.id   = id;
  this.cost = cost;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public double getCost() {
  return cost;
 }

 public void setCost(double cost) {
  this.cost = cost;
 }
 
 public String toString() {
  return "";
 }
}
/**-----------------------Body.java-------------------*/
public class Body {
 
 private double width;
 private double height;
 private int    seats;
 private double cost;
 
 public Body(double width, double height,
                                   int seats, double cost) {
  this.width  = width;
  this.height = height;
  this.seats  = seats;
  this.cost   = cost;
 }

 public double getWidth() {
  return width;
 }

 public void setWidth(double width) {
  this.width = width;
 }

 public double getHeight() {
  return height;
 }

 public void setHeight(double height) {
  this.height = height;
 }

 public int getSeats() {
  return seats;
 }

 public void setSeats(int seats) {
  this.seats = seats;
 }

 public double getCost() {
  return cost;
 }

 public void setCost(double cost) {
  this.cost = cost;
 }
 
 public String toString() {
  return "Body:\n" + width + "x" + 
                             height + "M " + seats + " seats\n\n";
 }
}
/**-----------------------Tester.java-------------------*/
public class Tester {
 
 public static void main(String args[]) {
  
  Engine engine1 = new Engine("150511", 2000);
  Engine engine2 = new Engine("151211", 3000);
  Engine engine3 = new Engine("150911", 4000);
  Engine engine4 = new Engine("151515", 5000);
  
  Body body1 = new Body(2.3, 1.2, 2, 2000);
  Body body2 = new Body(2.6, 1.5, 4, 3000);
  Body body3 = new Body(20.3, 3.2, 50, 4000);
  Body body4 = new Body(30.3, 3.2, 70, 5000);
  
  Vehicle vehicles[] = new Vehicle[4];
  
  vehicles[0] = new Car("Company1", "Car1", 
                    engine1, body1, 10000, 2, "Manual", 500);
  vehicles[1] = new Car("Company2", "Car2", 
                    engine2, body2, 20000, 4, "Automatic", 400);
  vehicles[2] = new Train("Company1", "Train1", 
                    engine3, body3, 50000, 10, "Coal", 200);
  vehicles[3] = new Train("Company2", "Train2", 
                    engine4, body4, 100000, 20, "Electricity", 300);
  
  for (int i = 0; i < vehicles.length; i++) {
   if (vehicles[i] instanceof Train) {
    
    Train currentVehice = (Train) vehicles[i];
    System.out.println(currentVehice.toString());
    
   } else {
    
    Car currentVehice = (Car) vehicles[i];
    System.out.println(currentVehice.toString());
    
   }
  }
 }
}



No comments :

Post a Comment

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact