-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicle.java
More file actions
57 lines (49 loc) · 1.8 KB
/
Copy pathVehicle.java
File metadata and controls
57 lines (49 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Vehicle {
private int passengers, fuelcap, mpg;
Vehicle (int passengers, int fuelcap, int mpg) {
this.passengers = passengers;
this.fuelcap = fuelcap;
this.mpg = mpg;
}
int getRange () {
return (fuelcap * mpg);
}
double calculateFuelNeeded(int miles) {
return (double) miles/mpg;
}
}
// Extend Vehicle to create a Truck specialization.
class Truck extends Vehicle {
private int cargocap; // cargo capacity in pounds
// This is a constructor for Truck.
Truck(int p, int f, int m, int cargocap) {
// Using super keyword to refer to Vehicle constructor.
super(p, f, m);
this.cargocap = cargocap;
}
// Accessor methods for cargocap.
int getCargo() { return cargocap; }
void putCargo(int c) { cargocap = c; }
}
class TestVehicle {
public static void main(String args[]) {
Vehicle minivan = new Vehicle(7, 16, 21);
System.out.println("Minivan's range is: " + minivan.getRange());
System.out.println("Minivan needs " + minivan.calculateFuelNeeded(100) + " gallons.");
// Construct some trucks.
Truck semi = new Truck(2, 200, 7, 44000);
Truck pickup = new Truck(3, 28, 15, 2000);
double gallons;
int dist = 252;
gallons = semi.calculateFuelNeeded(dist);
System.out.println("Semi can carry " + semi.getCargo() +
" pounds.");
System.out.println("To go " + dist + " miles semi needs " +
gallons + " gallons of fuel.\n");
gallons = pickup.calculateFuelNeeded(dist);
System.out.println("Pickup can carry " + pickup.getCargo() +
" pounds.");
System.out.println("To go " + dist + " miles pickup needs " +
gallons + " gallons of fuel.");
}
}