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-------------------*/ |
002 | public abstract class Vehicle { |
004 | private String manufacturer; |
006 | private Engine engine; |
008 | private double profit; |
010 | public Vehicle(String manufacturer, String name, |
011 | Engine engine, Body body, double profit) { |
012 | this .manufacturer = manufacturer; |
014 | this .engine = engine; |
016 | this .profit = profit; |
019 | public String getManufacturer() { |
023 | public void setManufacturer(String manufacturer) { |
024 | this .manufacturer = manufacturer; |
027 | public String getName() { |
031 | public void setName(String name) { |
035 | public Engine getEngine() { |
039 | public void setEngine(Engine engine) { |
040 | this .engine = engine; |
043 | public Body getBody() { |
047 | public void setBody(Body body) { |
051 | public double getProfit() { |
055 | public void setProfit( double profit) { |
056 | this .profit = profit; |
059 | protected abstract double calculateTotalPrice(); |
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" ; |
070 | /**-----------------------Car.java-------------------*/ |
071 | public class Car extends Vehicle { |
075 | private double gearPrice; |
077 | public Car(String manufacturer, String name, Engine engine, |
078 | Body body, double profit, int doors, String gear, |
080 | super (manufacturer, name, engine, body, profit); |
083 | this .gearPrice = gearPrice; |
086 | public int getDoors() { |
090 | public void setDoors( int doors) { |
094 | public String getGear() { |
098 | public void setGear(String gear) { |
102 | public double getGearPrice() { |
106 | public void setGearPrice( double gearPrice) { |
107 | this .gearPrice = gearPrice; |
111 | protected double calculateTotalPrice() { |
112 | return getEngine().getCost() + |
113 | getBody().getCost() + getProfit() + gearPrice; |
116 | public String toString() { |
117 | return super .toString() + doors |
118 | + " doors\n\nPrice:\n$" + calculateTotalPrice() + "\n\n" ; |
121 | /**-----------------------Train.java-------------------*/ |
122 | public class Train extends Vehicle { |
124 | private int compartments; |
126 | private double compartmentPrice; |
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; |
134 | this .compartmentPrice = compartmentPrice; |
137 | public int getCompartments() { |
141 | public void setCompartments( int compartments) { |
142 | this .compartments = compartments; |
145 | public String getFuel() { |
149 | public void setFuel(String fuel) { |
153 | public double getCompartmentPrice() { |
154 | return compartmentPrice; |
157 | public void setCompartmentPrice( double compartmentPrice) { |
158 | this .compartmentPrice = compartmentPrice; |
162 | protected double calculateTotalPrice() { |
163 | return getEngine().getCost() + getBody().getCost() |
164 | + getProfit() + (compartments*compartmentPrice); |
167 | public String toString() { |
168 | return super .toString() + compartments + |
169 | " compartments\n\nPrice:\n$" + calculateTotalPrice() + "\n\n" ; |
173 | /**-----------------------Engine.java-------------------*/ |
179 | public Engine(String id, double cost) { |
184 | public String getId() { |
188 | public void setId(String id) { |
192 | public double getCost() { |
196 | public void setCost( double cost) { |
200 | public String toString() { |
204 | /**-----------------------Body.java-------------------*/ |
207 | private double width; |
208 | private double height; |
212 | public Body( double width, double height, |
213 | int seats, double cost) { |
215 | this .height = height; |
220 | public double getWidth() { |
224 | public void setWidth( double width) { |
228 | public double getHeight() { |
232 | public void setHeight( double height) { |
233 | this .height = height; |
236 | public int getSeats() { |
240 | public void setSeats( int seats) { |
244 | public double getCost() { |
248 | public void setCost( double cost) { |
252 | public String toString() { |
253 | return "Body:\n" + width + "x" + |
254 | height + "M " + seats + " seats\n\n" ; |
257 | /**-----------------------Tester.java-------------------*/ |
260 | public static void main(String args[]) { |
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 ); |
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 ); |
272 | Vehicle vehicles[] = new Vehicle[ 4 ]; |
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 ); |
283 | for ( int i = 0 ; i < vehicles.length; i++) { |
284 | if (vehicles[i] instanceof Train) { |
286 | Train currentVehice = (Train) vehicles[i]; |
287 | System.out.println(currentVehice.toString()); |
291 | Car currentVehice = (Car) vehicles[i]; |
292 | System.out.println(currentVehice.toString()); |
No comments :
Post a Comment