Skip to content

Commit 5c80ab9

Browse files
committed
Did 1 What are Classes Objects and Methods / 1.4 Build a Droid Project
1 parent b04f7b7 commit 5c80ab9

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 numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
}

0 commit comments

Comments
 (0)