-
Notifications
You must be signed in to change notification settings - Fork 1
/
InterfaceZoo_gonz.java
167 lines (141 loc) · 4.25 KB
/
InterfaceZoo_gonz.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.util.ArrayList;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner; // for input
// New sound stuff
import javax.sound.sampled.*;
import java.io.File;
//TODO: Make the files local
/**
* @author Brandon Gonzalez <[email protected]>
*/
interface Speakable {
void speak();
}
class Animal implements Speakable {
String name;
public Animal(String name) {
this.name = name;
}
public void speak() {
System.out.println(toString());
}
public String toString() {
return "Name: " + this.name;
}
}
class Horse implements Speakable {
public void speak() {
System.out.println("Monkey sound goes here");
String fileName = "./sound/Horse.wav";
AudioClip clip = InterfaceZoo_gonz.getAudioClip(fileName);
clip.play();
InterfaceZoo_gonz.wait(1);
}
}
class Dog implements Speakable {
public void speak() {
System.out.println("woof");
// Setup file name for the sound for this animal. The file should be in same folder as
// the class file that contains main
String fileName = "./sound/dogbark4.wav";
AudioClip clip = InterfaceZoo_gonz.getAudioClip(fileName);
clip.play();
InterfaceZoo_gonz.wait(3);
}
public void sleep() {
System.out.println("zZz");
}
}
class Cat implements Speakable {
public void speak() {
System.out.println("meow");
// Setup file name for the sound for this animal. The file should be in same folder as
// the class file that contains main
String fileName = "./sound/catme.wav";
AudioClip clip = InterfaceZoo_gonz.getAudioClip(fileName);
clip.play();
InterfaceZoo_gonz.wait(3);
}
}
/**
* Tests out the Speakable hierarchy by having those creatures speak.
*/
public class InterfaceZoo_gonz {
/**
* Based on the file name provided, this will load the corresponding
* AudioClip (au wav aif mid formats) and return that AudioClip object
* @param relativeURL - this is the file name for the sound file, it
* is in the same folder that you are executing this application from
* @return an AudioClip that you can play
*/
public static AudioClip getAudioClip( String relativeURL ) {
URL completeURL = null;
try {
URL baseURL = new URL("file:" + System.getProperty("user.dir") + "/");
completeURL = new URL(baseURL, relativeURL);
System.out.println( "Accessing: " + completeURL.toString() );
} catch (MalformedURLException e){
System.err.println(e.getMessage());
} catch( Exception e) {
}
AudioClip audioClip = Applet.newAudioClip(completeURL);
return audioClip;
}
/**
* Convenience method to halt this thread of execution for specified number
* of seconds
* @param numSeconds
*/
public static void wait(int numSeconds) {
try {
Thread.sleep(numSeconds * 1000); // it wants milliSeconds
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Test driver. Creates animals, has them speak, puts them in ArrayList
* and has them speak. great fun.
*/
public static void main(String[] args) {
ArrayList<Speakable> zoo = new ArrayList<Speakable>();
Speakable horse = new Horse();
Speakable rover = new Dog();
zoo.add(horse);
// Rover speaks.
rover.speak();
Scanner scan = new Scanner(System.in);
System.out.println("Rover spoke, hit <Enter> to continue...");
scan.nextLine();
// Add first animal to zoo, a zoo of 1!
zoo.add(rover);
// casting required below (and must be parenthesized so that the cast takes place
// before the "." operator
((Dog)rover).sleep();
// Create 2nd animal.
Speakable suzie = new Cat();
// Add 2nd animal to zoo, a zoo of 2!
zoo.add(suzie);
// Suzie speaks:
suzie.speak();
System.out.println("Suzie spoke, hit <Enter> to continue...");
scan.nextLine();
suzie = rover; // no problems, but we lost our cat ....
rover = suzie; // no problems, 2 references to same object now
// The new suzie speaks
suzie.speak();
System.out.println("The new Suzie spoke, hit <Enter> to continue...");
scan.nextLine();
// iterate over each element in ArrayList
for(Speakable obj : zoo)
{
obj.speak(); // tell each Speakable to speak
}
System.out.println("All animals spoke, hit <Enter> to end program");
scan.nextLine();
System.exit(0);
} // end main
}// end class Interfacezoo_gonz