File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
lab9/lab9q1s6434480323/src/lab9q1s6434480323 Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * To change this license header, choose License Headers in Project Properties.
3+ * To change this template file, choose Tools | Templates
4+ * and open the template in the editor.
5+ */
6+ package lab9q1s6434480323 ;
7+
8+ /**
9+ *
10+ * @author ratchanon
11+ */
12+ public class Truck extends Car {
13+ // Additional Instance Variable
14+ private double M_weight ;
15+ private double weight ;
16+
17+
18+ public Truck (double gas , double efficiency , double M_weight ,double weight ) {
19+ super (gas , efficiency );
20+ this .M_weight = M_weight ;
21+
22+ if (weight > M_weight ){
23+ this .weight = M_weight ;
24+ } else {
25+ this .weight = weight ;
26+ }
27+ }
28+
29+ @ Override
30+ public void drive (double distance ){
31+ double additionalGas ;
32+ double useGas = distance / super .getEfficiency ();
33+
34+ if (this .weight < 1 ){
35+ additionalGas = 0 ;
36+ } else if (this .weight <= 10 ){
37+ additionalGas = useGas * 0.1 ;
38+ } else if (this .weight <= 20 ){
39+ additionalGas = useGas * 0.2 ;
40+ } else {
41+ additionalGas = useGas * 0.3 ;
42+ }
43+
44+ useGas += additionalGas ;
45+
46+ if (useGas > super .getGas ()) {
47+ System .out .println ("You cannot drive too far, please add gas" );
48+ } else {
49+ super .setGas (super .getGas () - useGas );
50+ }
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments