-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDogAppDemo.java
More file actions
49 lines (33 loc) · 1.22 KB
/
Copy pathDogAppDemo.java
File metadata and controls
49 lines (33 loc) · 1.22 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
package Week2;
import java.util.Scanner;
public class DogAppDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Dog fido = new Dog(); //"Dog" is a class
writeLine("My dog is " + fido);
fido.name = "Fido";
writeLine("Her name is " + fido.name);
writeLine(fido.name + " has " + fido.numberOfLegs + " legs."); //haven't yet declared number of legs
fido.numberOfLegs = 3;
writeLine(fido.name + " has " + fido.numberOfLegs + " legs.");
//Fido runs away and ends up in a shelter for three-legged dogs
//When Fido gets adopted...
fido.rename("Hope");
writeLine("Her name is now " + fido.name);
fido.speak();
writeLine("Bark, Hope!");
fido.bark();
Dog myOtherDog = new Dog();
myOtherDog.name = "Krusty";
writeLine("My other dog's name is " + myOtherDog.name);
myOtherDog.speak();
writeLine("You come home. Your dogs act as if you've been gone for sooo long.");
Dog[] myDogs = {fido, myOtherDog};
for(Dog currentDog: myDogs) { //could have: for(int index = 0; index < myDogs.length; index++) {
currentDog.bark();
}
} //closing bracket for main
public static void writeLine(String message) {
System.out.println(message);
}
}