Interface, dziedziczenie, polimorfizm

0

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???...

  1. Stwórz interfejs Instrument i zadeklaruj w nim metodę play().
  2. Stwórz klasy Guitar, Flute i Trombone, które realizują interfejs Instrument i definiują metodę play()
  3. 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().
  4. 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) {
	
}

}
0

Jest nie tak, bo punkty 3b), 3c) i częściowo 4) wrzuciłeś do 3d).

3b) To klasa posiada atrybut. Nie metoda.
3c) masz addInstrument(String), powinno być addInstrument(Instrument)
W treści zadania nie ma mowy o pobieraniu danych wejściowych.

0

Czy ten kod jest poprawny dla 3 punktu i jego podpunktów ?

//3a
public class Orkiestra implements Instrument {

	// 3b
	private List<Instrument> instruments;
	
	public Orkiestra(Collection<? extends Instrument> c) {
		instruments = new ArrayList<Instrument>(c);
		for (int i=0; i<((CharSequence) instruments).length(); i++) {
		}		
	}
	
	// 3c 
    public void addInstrument(Collection<? extends Instrument> Instrument) {
    	instruments.addAll(Instrument);
	}
    
    //3d
    public void play() {
    	
    }
0

Po co Collection<? extends Instrument>? Nie przekazujesz listy tylko pojedyncze obiekty.

Konstruktor Orkiestra może być bezargumentowy.
W addInstrument(Instrument) po prostu dodaj przekazany obiekt do listy.
W Orkiestra.play() zrób pętlę po obiektach listy i wywołaj w każdym metodę play().

Przypomnę: length jest w tablicach (instrumenty[]). Tu jest lista.

1

Sam sobie skomplikowałeś ;) Gdzie tu masz napisane, żeby użyć Scannera? Realizuj punkty po kolei, tak jak masz w treści zadania, nie wszystkie na raz.

class Orkiestra implements Instrument { // 3.a
    private Collection<Instrument> instruments; // 3.b

    public Okriestra() {
        instruments = new ArrayList<>();
    }

    public void play() { // 3.d
        for (Instrument instrument : instruments) {
            instrument.play();
        }
    }

    public void addInstrument(Instrument instrument) { // 3.c
        instruments.add(instrument);
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1