具有仪器字符串名称的数组是Instrument类中的必填字段,但为了简化程序,我将接受在每个字符串上不单独调整仪器的解决方案。但是,如果你想使用每个字符串,可以在第8周附加说明中找到一个很好的例子,其中考虑了由Tuba代表的另一类工具。
请使用清单8.3和8.4作为模型,在两个单独的类中组织项目3的代码,一个用于定义仪器,另一个用于测试仪器。
使Instrument类中的方法返回String,与要求中的示例不同,此类方法直接写入标准输出。这是必要的,因为需要将测试类的输出写入用户在命令行上指定的文件。
在测试类中,您必须拥有一个包含10个Instrument类型元素的数组,使用Instrument类的实例填充数组(通过在类构造函数上使用new运算符),并使用while或for循环以在每个数组元素上执行测试(即调用Intrument类方法)。
根据要求中的规定,必须在命令行中使用参数启动测试类:
java Mynamep3tst myfilename.txt
其中myfilename.txt是必须输出所有输出的文件。该文件名应该在程序中使用如下(参见清单14.13):
java.io.File file = new java.io.File(args[0]);
java.io.PrintWriter output = new java.io.PrintWriter(file);
当您有要发送到文件的消息时,
output.println(message);
*我的问题是,每当我尝试使用数组instrumentContent在我的for循环中创建一个新的仪器类对象时,它会导致一个错误。我无法理解我是否不允许以这种方式创建新对象。如果我不允许这样做,那么正确的方法是什么才能使用我的每个数组? *
class StringInstrument {//begin class
//declare variables
boolean isTuned;
boolean isPlaying;
boolean band;
public String nameOfInstrument;
int numberOfStrings;
String nameofStringsInInstrument[] = {"E", "A", "D", "G", "B"}; //an array of string names
public StringInstrument() {//begin contructor
numberOfStrings = 5;
isTuned = false;
isPlaying = false;
band = false;
}//end constructor
public int NumberOfStrings(int stringNumber){//begin method
System.out.println("The number of strings for the " + nameOfInstrument + " is " + stringNumber );
return this.numberOfStrings = stringNumber;
}//end method
public String InstrumentNameGet() {//begin method
return nameOfInstrument;
}//end method
public void SetInstrumentName (String instrumentName) {//begin getter method
nameOfInstrument = instrumentName;
}//end method
public String InstrumentNameDisplay() {//begin method
System.out.println("Your instrument is the " + nameOfInstrument);
return nameOfInstrument;
}//end method
public boolean PlayInstrument(){//begin method
System.out.println("You are playing your " + nameOfInstrument);
return isPlaying = true;
}//end method
public boolean TuneInstrument(){//begin method
System.out.println("Tune " + nameOfInstrument);
return isTuned = true;
}//end method
public boolean stopTuneInstrument() {//begin method
System.out.println("The" + nameOfInstrument + " is out of tune.");
return isTuned = false;
}//end method
public boolean StopPlayInstrument() {//begin method
System.out.println("The " + nameOfInstrument + " has stopped playing");
return isTuned = false;
}//end method
public boolean PlayInstrumentBand() {//begin method
System.out.println("The " + nameOfInstrument + " is playing in a band");
return band = true;
}//end method
public boolean StopPlayInstrumentBand() {//begin method
System.out.println("The " + nameOfInstrument + " has stoped playing with the band");
System.out.println("\n");
return band = false;
}//end method
} //结束课
公共课RandyGilmanP3 {//开始上课
public static void main(String[] args) throws Exception{//begin main
java.io.File file = new java.io.File("RandyGilmanP3.txt");
//create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
//Declaring, creating, and intialize arrays
String[] instrumentList = new String [10];
String[] instrumentContent = new String [10];
int[] stringNumber = new int [10];
//input string names into array
instrumentList[0] = "Guitar";
instrumentList[1] = "Violin";
instrumentList[2] = "Bass Guitar";
instrumentList[3] = "Cello";
instrumentList[4] = "Banjo";
instrumentList[5] = "Sitar";
instrumentList[6] = "Rabab";
instrumentList[7] = "Viola";
instrumentList[8] = "Harp";
instrumentList[9] = "Ukulele";
//input string amounts into array
stringNumber[0] = 5;
stringNumber[1] = 4;
stringNumber[2] = 5;
stringNumber[3] = 4;
stringNumber[4] = 5;
stringNumber[5] = 18;
stringNumber[6] = 3;
stringNumber[7] = 4;
stringNumber[8] = 47;
stringNumber[9] = 4;
for (int i = 0; i < instrumentContent.length; i++){//begin for loop
StringInstrument instrumentList[i] = new StringInstrument();
output.println(instrumentList[i].InstrumentNameDisplay());
output.println(instrumentList[i].NumberOfStrings(stringNumber[i]));
output.println(instrumentList[i].TuneInstrument());
output.println(instrumentList[i].PlayInstrument());
output.println(instrumentList[i].PlayInstrumentBand());
output.println(instrumentList[i].StopPlayInstrument());
}//end for loop
}//end main
}//end class
答案 0 :(得分:0)
您已将instrumentList[]
声明为Strings
的数组。您的for
循环尝试在这些Strings
上调用以下方法:
InstrumentNameDisplay()
NumberOfStrings(String)
TuneInstrument()
PlayInstrumentBand()
StopPlayInstrument()
这些都不是String
类的方法。
看起来您可能要做的是构建一个Instruments
...