-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathVehicle.java
52 lines (42 loc) · 956 Bytes
/
Vehicle.java
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
package com.guide.c7;
// Try This 7-1
//
// Build a subclass of Vehicle for trucks.
public class Vehicle {
private int passengers; // number of passengers
private int fuelcap; // fuel capacity in gallons
private int mpg; // fuel consumption in miles per gallon
Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}
int range() {
return fuelcap * mpg;
}
/**
* Compute fuel needed for a given distance.
*/
double fuelneeded(int miles) {
return (double) miles / mpg;
}
// Accessor methods for instance variables.
int getPassengers() {
return passengers;
}
void setPassengers(int p) {
passengers = p;
}
int getFuelcap() {
return fuelcap;
}
void setFuelcap(int f) {
fuelcap = f;
}
int getMpg() {
return mpg;
}
void setMpg(int m) {
mpg = m;
}
}