-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaceDemo2.java
More file actions
48 lines (48 loc) · 1.06 KB
/
InterfaceDemo2.java
File metadata and controls
48 lines (48 loc) · 1.06 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
//Interface Constants
import java.util.Random;
interface SharedConstants{
int NO=0;
int YES=1;
int MAYBE=2;
int LATER=3;
int SOON=4;
int NEVER=5;
}
class Question implements SharedConstants{
Random rand = new Random();
int ask(){
Double a=rand.nextDouble();
System.out.print(a+"\t");
int prob = (int) (100* a);
if(prob<30)
return NO;
else if(prob<60)
return YES;
else if(prob<75)
return LATER;
else if(prob<98)
return SOON;
else
return NEVER;
}
}
public class InterfaceDemo2 implements SharedConstants{
static void answer(int result){
switch(result){
case NO: System.out.println("NO"); break;
case YES: System.out.println("YES"); break;
case MAYBE: System.out.println("MAYBE");break;
case LATER: System.out.println("LATER");break;
case SOON: System.out.println("SOON"); break;
case NEVER: System.out.println("NEVER");break;
default: System.out.println("Wrong Answer");
}
}
public static void main(String args[]){
Question q = new Question();
answer(q.ask());
answer(q.ask());
answer(q.ask());
answer(q.ask());
}
}