-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFuel.java
More file actions
71 lines (57 loc) · 1.67 KB
/
Fuel.java
File metadata and controls
71 lines (57 loc) · 1.67 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
70
71
/*
Implement following inheritance
Superclasee : Vehicle(vno, model)
SubClass 1 : Car(average)
SubClass 2: Bike(average)
Display how much fuel is require for car and bike for 400km travel
Fuel = Distance / Average
*/
class Vehicle {
int vno;
String model;
Vehicle(int vno, String model) {
this.vno = vno;
this.model = model;
}
void display() {
System.out.println("Vehicle No: " + vno);
System.out.println("Model: " + model);
}
}
// Subclass 1: Car
class Car extends Vehicle {
double average;
Car(int vno, String model, double average) {
super(vno, model);
this.average = average;
}
void calculateFuel(double distance) {
double fuel = distance / average;
display();
System.out.println("Car Average: " + average + " km/l");
System.out.println("Fuel required for " + distance + " km: " + fuel + " liters\n");
}
}
// Subclass 2: Bike
class Bike extends Vehicle {
double average;
Bike(int vno, String model, double average) {
super(vno, model);
this.average = average;
}
void calculateFuel(double distance) {
double fuel = distance / average;
display();
System.out.println("Bike Average: " + average + " km/l");
System.out.println("Fuel required for " + distance + " km: " + fuel + " liters\n");
}
}
public class Fuel {
public static void main(String[] args) {
double distance = 400; // km
Car car = new Car(101, "Honda City", 20);
Bike bike = new Bike(202, "Yamaha", 40);
car.calculateFuel(distance);
bike.calculateFuel(distance);
}
}