Mam do zrobienia poniższe zadanie, ale potrzebuje mądrzejszej głowy od mojej która podpowie mi co jest nie tak, dlaczego pętla for nie będzie iterowała po liście instruments???...
- Stwórz interfejs Instrument i zadeklaruj w nim metodę play().
- Stwórz klasy Guitar, Flute i Trombone, które realizują interfejs Instrument i definiują metodę play()
- Stwórz klasę Orchestra, która:
a. również realizuje interfejs Instrument
b. posiada atrybut instruments, który jest listą obiektów typu Instrument
c. posiada metodę addInstrument(Instrument), która dodaje do orkiestry nowy instrument
d. definiuje metodę play(), w której gra na wszystkich instrumentach - wołając w pętli na każdym z nich play(). - Dodaj metodę main, w której utworzysz instancje wszystkich rodzajów instrumentów oraz jeden obiekt orkiestry, do której dodasz utworzone instrumenty.
MÓJ KOD:
// 1
public interface Instrument {
void play();
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// 2
class Guitar implements Instrument {
public void play() {
System.out.println("Gram");
}
}
//2
class Flute implements Instrument {
public void play() {
}
}
//2
class Trombone implements Instrument {
public void play() {
}
}
// 3 a
public class Orkiestra implements Instrument {
private Scanner newInstrument;
// 3b
public void play() {
List<Instrument> instruments = new ArrayList();
Instrument guitar = new Guitar();
Instrument flute = new Flute();
Instrument trombone = new Trombone();
instruments.add(guitar);
instruments.add(flute);
instruments.add(trombone);
for(int i=0; i<instruments.length; i++) {
Instrument x = (Instrument) instruments;
((Instrument)x).play();
}
}
// 3c
String addInstrument(String Instrument) {
newInstrument = new Scanner(System.in);
Instrument = newInstrument.nextLine();
return Instrument;
}
// 4 jeszcze nie zaczete
public static void main(String[] args) {
}
}