具有儀器字符串名稱的數組是儀器類中的必需字段,但爲了簡化程序,我將接受解決方案的樂器不是單獨在每個琴絃上完成的。但是,如果您想要處理每個字符串,則可以在第8周附加註釋中找到一個很好的示例,其中考慮了以Tuba爲代表的另一類工具。創建儀器類(處理數組和方法)
請使用清單8.3和8.4作爲模型,在兩個不同的類中組織您的Project 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);
*我的問題每當我嘗試在for循環中使用數組instrumentContent創建儀器類的新對象時,都會導致錯誤。我無法理解我是否被允許以這種方式創建新對象。如果我不能這樣做,那麼這樣做的正確方法是什麼,以便我的每個陣列都可以使用? *
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
我們沒有任何地方需要接近這麼多的信息。我們所需要的僅僅是你沒有提供的唯一的東西,你得到的確切的錯誤以及它發生的確切線條。 – nhgrif