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:
001/**-----------------------Vehicle.java-------------------*/
002public abstract class Vehicle {
003  
004 private String manufacturer;
005 private String name;
006 private Engine engine;
007 private Body   body;
008 private double profit;
009  
010 public Vehicle(String manufacturer, String name,
011                  Engine engine, Body body, double profit) {
012  this.manufacturer = manufacturer;
013  this.name         = name;
014  this.engine       = engine;
015  this.body         = body;
016  this.profit       = profit;
017 }
018 
019 public String getManufacturer() {
020  return manufacturer;
021 }
022 
023 public void setManufacturer(String manufacturer) {
024  this.manufacturer = manufacturer;
025 }
026 
027 public String getName() {
028  return name;
029 }
030 
031 public void setName(String name) {
032  this.name = name;
033 }
034 
035 public Engine getEngine() {
036  return engine;
037 }
038 
039 public void setEngine(Engine engine) {
040  this.engine = engine;
041 }
042 
043 public Body getBody() {
044  return body;
045 }
046 
047 public void setBody(Body body) {
048  this.body = body;
049 }
050 
051 public double getProfit() {
052  return profit;
053 }
054 
055 public void setProfit(double profit) {
056  this.profit = profit;
057 }
058  
059 protected abstract double calculateTotalPrice();
060  
061 public String toString() {
062  return manufacturer + " " + name +
063                     "\n\nEngine:\n" + engine.getId() +
064                   " $" + engine.getCost() + "\n\nBody:\n" +
065                    body.getWidth() + "x" + body.getHeight() +
066                        "M\n" + body.getSeats() + " seats\n";
067 }
068}
069 
070/**-----------------------Car.java-------------------*/
071public class Car extends Vehicle {
072 
073 private int    doors;
074 private String gear;
075 private double gearPrice;
076  
077 public Car(String manufacturer, String name, Engine engine,
078                  Body body, double profit, int doors, String gear,
079                                                 double gearPrice) {
080  super(manufacturer, name, engine, body, profit);
081  this.doors     = doors;
082  this.gear      = gear;
083  this.gearPrice = gearPrice;
084 }
085 
086 public int getDoors() {
087  return doors;
088 }
089 
090 public void setDoors(int doors) {
091  this.doors = doors;
092 }
093 
094 public String getGear() {
095  return gear;
096 }
097 
098 public void setGear(String gear) {
099  this.gear = gear;
100 }
101 
102 public double getGearPrice() {
103  return gearPrice;
104 }
105 
106 public void setGearPrice(double gearPrice) {
107  this.gearPrice = gearPrice;
108 }
109 
110 @Override
111 protected double calculateTotalPrice() {
112  return getEngine().getCost() +
113                   getBody().getCost() + getProfit() + gearPrice;
114 }
115  
116 public String toString() {
117  return super.toString() + doors
118         + " doors\n\nPrice:\n$" + calculateTotalPrice() + "\n\n";
119 }
120}
121/**-----------------------Train.java-------------------*/
122public class Train extends Vehicle {
123 
124 private int    compartments;
125 private String fuel;
126 private double compartmentPrice;
127  
128 public Train(String manufacturer, String name, Engine engine,
129                 Body body, double profit, int compartments,
130                            String fuel, double compartmentPrice) {
131  super(manufacturer, name, engine, body, profit);
132  this.compartments = compartments;
133  this.fuel = fuel;
134  this.compartmentPrice = compartmentPrice;
135 }
136 
137 public int getCompartments() {
138  return compartments;
139 }
140 
141 public void setCompartments(int compartments) {
142  this.compartments = compartments;
143 }
144 
145 public String getFuel() {
146  return fuel;
147 }
148 
149 public void setFuel(String fuel) {
150  this.fuel = fuel;
151 }
152 
153 public double getCompartmentPrice() {
154  return compartmentPrice;
155 }
156 
157 public void setCompartmentPrice(double compartmentPrice) {
158  this.compartmentPrice = compartmentPrice;
159 }
160 
161 @Override
162 protected double calculateTotalPrice() {
163  return getEngine().getCost() + getBody().getCost()
164                    + getProfit() + (compartments*compartmentPrice);
165 }
166 
167 public String toString() {
168  return super.toString() + compartments +
169        " compartments\n\nPrice:\n$" + calculateTotalPrice() + "\n\n";
170 }
171}
172 
173/**-----------------------Engine.java-------------------*/
174public class Engine {
175  
176 private String id;
177 private double cost;
178  
179 public Engine(String id, double cost) {
180  this.id   = id;
181  this.cost = cost;
182 }
183 
184 public String getId() {
185  return id;
186 }
187 
188 public void setId(String id) {
189  this.id = id;
190 }
191 
192 public double getCost() {
193  return cost;
194 }
195 
196 public void setCost(double cost) {
197  this.cost = cost;
198 }
199  
200 public String toString() {
201  return "";
202 }
203}
204/**-----------------------Body.java-------------------*/
205public class Body {
206  
207 private double width;
208 private double height;
209 private int    seats;
210 private double cost;
211  
212 public Body(double width, double height,
213                                   int seats, double cost) {
214  this.width  = width;
215  this.height = height;
216  this.seats  = seats;
217  this.cost   = cost;
218 }
219 
220 public double getWidth() {
221  return width;
222 }
223 
224 public void setWidth(double width) {
225  this.width = width;
226 }
227 
228 public double getHeight() {
229  return height;
230 }
231 
232 public void setHeight(double height) {
233  this.height = height;
234 }
235 
236 public int getSeats() {
237  return seats;
238 }
239 
240 public void setSeats(int seats) {
241  this.seats = seats;
242 }
243 
244 public double getCost() {
245  return cost;
246 }
247 
248 public void setCost(double cost) {
249  this.cost = cost;
250 }
251  
252 public String toString() {
253  return "Body:\n" + width + "x" +
254                             height + "M " + seats + " seats\n\n";
255 }
256}
257/**-----------------------Tester.java-------------------*/
258public class Tester {
259  
260 public static void main(String args[]) {
261   
262  Engine engine1 = new Engine("150511", 2000);
263  Engine engine2 = new Engine("151211", 3000);
264  Engine engine3 = new Engine("150911", 4000);
265  Engine engine4 = new Engine("151515", 5000);
266   
267  Body body1 = new Body(2.3, 1.2, 2, 2000);
268  Body body2 = new Body(2.6, 1.5, 4, 3000);
269  Body body3 = new Body(20.3, 3.2, 50, 4000);
270  Body body4 = new Body(30.3, 3.2, 70, 5000);
271   
272  Vehicle vehicles[] = new Vehicle[4];
273   
274  vehicles[0] = new Car("Company1", "Car1",
275                    engine1, body1, 10000, 2, "Manual", 500);
276  vehicles[1] = new Car("Company2", "Car2",
277                    engine2, body2, 20000, 4, "Automatic", 400);
278  vehicles[2] = new Train("Company1", "Train1",
279                    engine3, body3, 50000, 10, "Coal", 200);
280  vehicles[3] = new Train("Company2", "Train2",
281                    engine4, body4, 100000, 20, "Electricity", 300);
282   
283  for (int i = 0; i < vehicles.length; i++) {
284   if (vehicles[i] instanceof Train) {
285     
286    Train currentVehice = (Train) vehicles[i];
287    System.out.println(currentVehice.toString());
288     
289   } else {
290     
291    Car currentVehice = (Car) vehicles[i];
292    System.out.println(currentVehice.toString());
293     
294   }
295  }
296 }
297}


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