-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuto1Q4.java
More file actions
78 lines (63 loc) · 1.61 KB
/
Copy pathTuto1Q4.java
File metadata and controls
78 lines (63 loc) · 1.61 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
72
73
74
75
76
77
78
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package Week1;
/**
*
* @author szeyu
*/
abstract class Vehicle{
double maxSpeed;
protected double currentSpeed;
public Vehicle(double maxSpeed) {
this.maxSpeed = maxSpeed;
}
public abstract void accelerate();
public double getCurrentSpeed(){
return currentSpeed;
}
public double getMaxSpeed(){
return maxSpeed;
}
public void pedalToTheMetal(){
while(currentSpeed < maxSpeed){
accelerate();
if(currentSpeed > maxSpeed){
currentSpeed = maxSpeed;
}
System.out.println(this);
}
}
@Override
public String toString(){
return "Current speed: " + currentSpeed;
}
}
class Perodua extends Vehicle{
public Perodua(double maxSpeed){
super(maxSpeed);
}
public void accelerate(){
currentSpeed += 2.7;
}
}
class Tesla extends Vehicle{
public Tesla(double maxSpeed){
super(maxSpeed);
}
public void accelerate(){
currentSpeed += 5.5;
}
}
public class Tuto1Q4 {
public static void main(String[] args) {
Vehicle vehicle1 = new Perodua(100);
Vehicle vehicle2 = new Tesla(200);
System.out.println("Perodua");
vehicle1.pedalToTheMetal();
System.out.println("");
System.out.println("Tesla");
vehicle2.pedalToTheMetal();
}
}