File tree 1 file changed +55
-0
lines changed
1_What_are_Classes_Objects_and_Methods/1.4_Build_a_Droid_Project
1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 1.
2
+ // The Droid.java file is empty.
3
+
4
+ // Start by defining the class Droid.
5
+
6
+ // Don’t forget to include a main() method! You can leave it empty for now.
7
+
8
+ // We want a Droid object that has the following state:
9
+
10
+ // name
11
+ // battery level
12
+ // and the following behavior:
13
+
14
+ // performing a task
15
+ // stating its battery level
16
+
17
+ // Stuck? Get a hint
18
+
19
+ public class Droid {
20
+ int batteryLevel ;
21
+ String name ;
22
+
23
+ public Droid (String droidName ) {
24
+ name = droidName ;
25
+ batteryLevel = 100 ;
26
+ }
27
+
28
+ public String toString () {
29
+ return "Hello, I'm the droid: " + name ;
30
+ }
31
+
32
+ public void performTask (String task ) {
33
+ System .out .println (name + " is performing task: " + task );
34
+ batteryLevel -= 10 ;
35
+ }
36
+
37
+ public void energyReport () {
38
+ System .out .println ("Battery level is: " + batteryLevel );
39
+ }
40
+
41
+ public static void main (String [] args ) {
42
+ Droid codey = new Droid ("Codey" );
43
+ System .out .println (codey );
44
+ codey .performTask ("dancing" );
45
+ codey .energyReport ();
46
+ codey .performTask ("playing" );
47
+ codey .energyReport ();
48
+ codey .performTask ("joking" );
49
+ codey .energyReport ();
50
+ Droid codey2 = new Droid ("Codey2" );
51
+ codey2 .performTask ("dancing" );
52
+ codey2 .energyReport ();
53
+ codey .energyReport ();
54
+ }
55
+ }
You can’t perform that action at this time.
0 commit comments