-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVehicle.java
More file actions
69 lines (59 loc) · 1.96 KB
/
Vehicle.java
File metadata and controls
69 lines (59 loc) · 1.96 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
58
59
60
61
62
63
64
65
66
67
68
69
public class Vehicle {
String type;
String powerType;
int numWheels;
int numDoors;
String color;
double fuelRemaining;
double fuelEconomy;
int numPassangers;
int passengerCapacity;
double milesTraveled = 0;
public Vehicle(String type, String powerType, int numWheels, int numDoors, String color, double fuelEconomy,
int passangerCapacity) {
this.type = type;
this.powerType = powerType;
this.numWheels = numWheels;
this.numDoors = numDoors;
this.color = color;
this.fuelRemaining = 100.0;
this.fuelEconomy = fuelEconomy;
this.numPassangers = 0;
this.passengerCapacity = passengerCapacity;
}
public int embark(int partySize) {
if (numPassangers + partySize > passangerCapacity) {
System.out.println("Party size is too big");
} else {
numPassangers += partySize;
}
return numPassangers;
}
public int dismbark(int partySize) {
if (numPassangers - partySize > 0) {
numPassangers -= partySize;
} else {
System.out.println("The party size is too big or there is no one on board.");
}
}
public double travel(double miles) {
double fuelUsed = miles / fuelEconomy;
if (fuelUsed > fuelRemaining) {
System.out.println("There is not enough fuel");
}
fuelRemaining -= fuelUsed;
// Update milage counter
milesTraveled += miles;
return fuelRemaining;
}
// Refuel
public static void main(String[] args) {
Vehicle prius = new Vehicle("Car", "gas", 4, 4, "red", 15, 5);
prius.embark(5);
Vehicle subaru = new Vehicle("Car", "gas", 4, 4, "red", 12, 10);
subaru.embark(3);
prius.travel(1000);
System.out.println("Prius has" + prius.fuelRemaining + "%");
System.out.println("Subaru has" + subaru.fuelRemaining + "%");
}
}