-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2.1.txt
57 lines (47 loc) · 1.11 KB
/
Q2.1.txt
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
----------------instrumenttest--------------
public class InstrumentTest {
public static void main(String[] args) {
Instrument[] Inst=new Instrument[10];
for(int i = 0; i<10; i++) {
if(i==1 || i==5 || i==9)
Inst[i]=new Piano();
else if(i==3 || i==4 || i==7)
Inst[i]=new Flute();
else
Inst[i]=new Guitar();
if(Inst[i] instanceof Piano)
System.out.println("Piano is playing tan tan tan tan");
else if(Inst[i] instanceof Flute)
System.out.println("Flute is playing toot toot toot toot");
else
System.out.println("Guitar is playing tin tin tin ");
System.out.println();
}
}
}
------------------------------------Instrument ---------------------------
abstract class Instrument
{
abstract void play();
}
class Piano extends Instrument
{
void play()
{
System.out.println("Piano is playing tan tan tan tan");
}
}
class Flute extends Instrument
{
void play()
{
System.out.println("Flute is playing toot toot toot toot");
}
}
class Guitar extends Instrument
{
void play()
{
System.out.println("Guitar is playing tin tin tin ");
}
}