我目前正在尝试设计并实现一个弦乐器类。
此作业的说明如下:
那是主文件,我必须:
创建一个模拟使用仪器类的Java测试类。在您的测试课程中,您至少应该:a)构建您的乐器的10个实例, b)调整你的乐器, c)开始演奏乐器, d)打电话给你独特的方法,和 e)停止演奏乐器 (提示:数组和循环将使您的工作更轻松,并产生更高效的代码!)
我创建了这两个文件,但是我的主项目文件有一些问题阻止我在测试时看到输出。 (如果有帮助的话,我正在使用netbeans来测试程序。)
我当前的测试文件没有错误,但我知道输出要在主java文件上打印是必要的,所以我也附上了它。
这是我的主项目java文件:
import java.io.*;
/* File: KenMasonp3.java
* Author: Kenneth Mason
* Date: 19-04-2014
* Purpose: Design and implement a stringed musical instrument class
* Code edited/modified by myself with sources from LEO classroom modules, Liang
* book, javaprogrammingforums.com, dreamincode.net, and the instructor
*/
public class KenMasonp3 { // start main class
// start main method
public static void main(String[] args) throws IOException {
//creates a file named from the command line argument
File outputFile = new File(args[0]);
PrintWriter output = new PrintWriter(outputFile);
//creates an array of 10 objects
instrument[] guitarArray = new instrument[10];
/*calls methods to construct, tune, play,
*get the string names and stop the instrument array
*/
instrument.constructGuitarArray(guitarArray, output);
instrument.tuneGuitar(guitarArray, output);
instrument.playGuitar(guitarArray, output);
instrument.getStrings(guitarArray, output);
instrument.stopGuitar(guitarArray, output);
//close the file
output.close();
} // main method end
} // end main class
// Guitar class
class instrument {
//method to construct the instrument array
public static instrument[] constructGuitarArray(instrument[] array,
PrintWriter file) {
//creates a random instrument
for (int i = 0; i < array.length; i++) {
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
error is:
* " constructor instrument in class instrument cannot be applied to given types;
required: no arguments
found: int
reason: actual and formal argument lists differ in length "
*/
array[i] = new instrument((int) (1 + Math.random() * 12));
//prints the creation message to file
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol variable createdMessage
* location: class instrument
*/
file.println(array[i].createdMessage);
}
//returns array to main method
return array;
}
//method that calls the tune method for the instrument array
public static void tuneGuitar(instrument[] array,
PrintWriter file) {
//tunes all objects in the array
for (int i = 0; i < array.length; i++) {
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol method
* location: class instrument
*/
array[i].setTune(true);
//prints the tuned message to file
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
*error is: cannot find symbol variable tunedMessage
* location: class instrument
*/
file.println(array[i].tunedMessage);
}
}
//method that calls the play method for the instrument array
public static void playGuitar(instrument[] array, PrintWriter file) {
//plays all objects in the array
for (int i = 0; i < array.length; i++) {
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol method setPlay(boolean)
* location: class instrument
*/
array[i].setPlay(true);
//prints the tuned message to file
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol variable playMessage
* location: class instrument
*/
file.println(array[i].playMessage);
}
}
//method that calls the getStrings method for the instrument array
public static void getStrings(instrument[] array, PrintWriter file) {
for (int i = 0; i < array.length; i++) {
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol method setString(boolean)
* location: class instrument
*/
array[i].setString(true);
//prints the tuned message to file
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol variable stringMessage
* location: class instrument
*/
file.println(array[i].stringMessage);
}
}
//method that calls the stopGuitar method for the instrument array
public static void stopGuitar(instrument[] array, PrintWriter file) {
for (int i = 0; i < array.length; i++) {
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol method setStop(boolean)
* location: class instrument
*/
array[i].setStop(true);
//prints the tuned message to file
/******** CODE BELOW ALWAYS GIVES ME THIS ERROR EVEN WHEN I COPY PASTE ******
* error is: cannot find symbol variable stopMessage
* location: class instrument
*/
file.println(array[i].stopMessage);
}
}
注意:文件outputFile = new File(args [0]);
因为我需要将Instrument类方法的输出写入用户从命令行参数输入的文本文件
如果你能看到,我的主要问题似乎就是这行代码:
array[i] = new instrument((int) (1 + Math.random() * 12));
netbeans中的错误说明: “班级仪器中的构造函数不能应用于给定类型; 必需:没有参数 发现:int 原因:实际和正式的参数列表长度不同“
我认为是因为这个错误我无法在我的许多其他代码部分找到符号。
如果有人需要我的文本文件,它就在这里(但没有明显的错误):
/* File: KenMasonp3.java
* Author: Kenneth Mason
* Date: 19-04-2014
* Purpose: Design and implement a stringed musical instrument class
* Code edited/modified by myself with sources from LEO classroom modules, Liang
* book, javaprogrammingforums.com, dreamincode.net
*/
public class KenMasonp3test { // start main class
public static void main(String[] args) {
}
//instrument class
class guitarInstrument {
//private variable declarations
private boolean tuned, playing;
private String guitarType;
private String[] stringNames;
//public variable declarations
public String playMessage, tunedMessage, createdMessage;
public StringBuilder guitarStringsNames;
//default constructor that generates a random 6 string instrument
public guitarInstrument() {
int strings = (int) (1 + Math.random() * 12);
//generates array based on number of strings
String[] stringNames = new String[strings];
//fills string array with random string names
for (int i = 0; i < stringNames.length; i++) {
stringNames[i] = String.valueOf((char)('A' + Math.random() *
('G' - 'A' + 1)));
}
this.stringNames = stringNames;
//sets instrument name
guitarType = "random " + strings + "-string instrument";
//sets tuned and playing to false
tuned = false;
playing = false;
//sets string for construction of the instrument
createdMessage = "You created a " + guitarType;
} // end of guitar() method
//constructor allowing specific amount of strings
public guitarInstrument(int strings) {
//creates specific instrument based on number of strings input
if (strings == 6) {
//creates and fills string array
String[] stringNames = {"E", "A", "D", "G", "B", "E"};
this.stringNames = stringNames;
//names instrument
guitarType = "guitar";
} // end if
else {
//creates and fills string array
String[] stringNames = new String[strings];
//fills array with random string names
for (int i = 0; i < stringNames.length; i++) {
stringNames[i] = String.valueOf((char)('A' + Math.random()
* ('G' - 'A' + 1)));
}
this.stringNames = stringNames;
//names instrument
guitarType = strings + "-string instrument";
} // end else
//sets tuned and playing to false
tuned = false;
playing = false;
//sets string for construction of the instrument
createdMessage = "You created a " + guitarType;
} // end guitar (in strings) method
//method to tune or untune the instrument
public void setTune(boolean tune) {
this.tuned = tune;
//sets string for tuned instrument
if (tuned == true) {
tunedMessage = "The " + guitarType + " is now in tune";
} // end tuned if
//sets string for untuned instrument
else {
tunedMessage = "The " + guitarType + " is out of tune";
} // end of tuned else
} // end of setTune method
//method to play or stop the instrument
public void playGuitarInstrument(boolean play) {
this.playing = true;
// sets string for playing instrument
if (playing == true) {
playMessage = "The" + guitarType + "is now playing";
} // end playing if
// sets string for unplayed instrument
else {
playMessage = "The" + guitarType + "has stopped";
} // end play else
} // end of playGuitarInstrument method
//method to display the string names of the instrument
public void getStrings() {
//sets stringbuilder with default intro statement
StringBuilder guitarStringNames = new StringBuilder();
System.out.println("The has the following strings:"
+ guitarStringNames);
} // end of getString method
} // end of guitarInstrument method
} // end of main class
非常感谢任何帮助。
答案 0 :(得分:0)
new instrument((int) (1 + Math.random() * 12));
这一行有你的问题。 instrument
没有将整数作为参数的构造函数。