/** There seems to be a problem with the intended method call from the performance class. The object is recognized as band but for some reason when i call band.begin() (band is the new performance object and begin its method) band is not recognized and it suggests it be initialized as a variable? */
import java.util.Scanner;
public class Music {
public static Instrument assignInstrument () {
String instrumentChoice;
String name;
Scanner input = new Scanner (System.in);
System.out.println("Select an instrument for the band member. ");
System.out.print("Vocals, piccolo, Clarinet, Cymbal, or Drum: ");
instrumentChoice = input.nextLine();
System.out.print("Enter the band member's name: ");
name = input.nextLine();
if (instrumentChoice.equalsIgnoreCase("v")) {
return (new Vocal(name));
} else if (instrumentChoice.equalsIgnoreCase("p")) {
return (new Piccolo(name));
} else if (instrumentChoice.equalsIgnoreCase("cl")) {
return (new Clarinet(name));
} else if (instrumentChoice.equalsIgnoreCase("cy")) {
return (new Cymbal(name));
} else {
return (new Drum(name));
}
}
/** I instantiated a "band" performance object but it is not recognizing it when i call its method... */
public static void main (String [] args){
Performance band; // band object instantiated
Instrument bandmember1, bandmember2, bandmember3, bandmember4;
Scanner input = new Scanner (System.in);
String performanceChoice;
bandmember1 = assignInstrument();
bandmember2 = assignInstrument();
bandmember3 = assignInstrument();
bandmember4 = assignInstrument();
System.out.println(bandmember1 + " " + bandmember2 + " " + bandmember3 + " " + bandmember4 + "
");
System.out.print("Would you like to hear a solo, duet, trio, quartet, or leave?: ");
performanceChoice = input.nextLine();
while (!performanceChoice.equalsIgnoreCase("l")) { // choosing the correct form of object
if (performanceChoice.equalsIgnoreCase("s")) {
band = new Performance (bandmember1);
} else if ( performanceChoice.equalsIgnoreCase("d")) {
band = new Performance (bandmember1, bandmember2);
} else if ( performanceChoice.equalsIgnoreCase("q")) {
band = new Performance (bandmember1, bandmember2, bandmember3);
} else {
band = new Performance (bandmember1, bandmember2, bandmember3, bandmember4);
}
}
band.Begin(); // error message: "variable band might not have been initialized".?
}
}
public class Performance {
private String arrangement;
private Instrument solo;
private Instrument duet_1, duet_2;
private Instrument trio_1, trio_2, trio_3;
private Instrument quart_1, quart_2, quart_3, quart_4;
public Performance (Instrument s) {
solo = s;
arrangement = solo.makeSound();
}
public Performance (Instrument d1, Instrument d2){
duet_1 = d1;
duet_2 = d2;
arrangement = duet_1.makeSound() + duet_2.makeSound();
}
public Performance (Instrument t1, Instrument t2, Instrument t3){
trio_1 = t1;
trio_2 = t2;
trio_3 = t3;
arrangement = trio_1.makeSound() + trio_2.makeSound() + trio_3.makeSound();
}
public Performance (Instrument q1, Instrument q2, Instrument q3, Instrument q4){
quart_1 = q1;
quart_2 = q2;
quart_3 = q3;
quart_4 = q4;
arrangement = quart_1.makeSound() + quart_2.makeSound() + quart_3.makeSound() + quart_4.makeSound();
}
public void Begin() {
System.out.print(arrangement);
}
public String toString(){
String program = "The performance includes ";
program += arrangement;
return program;
}
}
/** Any suggestions? Thanks in advance*/
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…