【问题标题】:constructor instrument in class instrument cannot be applied to given types类仪器中的构造函数仪器不能应用于给定类型
【发布时间】:2014-04-18 19:20:57
【问题描述】:

我目前正在尝试设计和实现一个弦乐器类。

此作业的说明是:

  1. 仪器的数据字段应包括字符串数量、表示字符串名称的字符串名称数组(例如 E、A、D、G),
  2. 布尔字段,用于确定乐器是否已调音,以及乐器当前是否正在演奏。如果您愿意,欢迎您添加其他数据字段。
  3. 将已调整和当前正在播放的字段设置为 false 的构造方法。
  4. 其他方法1)调乐器,
  5. 开始演奏乐器,以及 3) 停止演奏乐器。 您认为合适的其他方法(添加至少一种独特的方法)。

这是主文件,然后我必须:

创建一个模拟使用您的仪器类的 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

任何帮助将不胜感激。

【问题讨论】:

    标签: java arrays string constructor stringbuilder


    【解决方案1】:
     new instrument((int) (1 + Math.random() * 12)); 
    

    这条线有你的问题。 instrument 没有将整数作为参数的构造函数。

    【讨论】:

    • 好的,我尝试使用:Instrument instrument = new instrument(); 在主类中添加仪器的构造函数,但它给出错误提示:“仪器” - 找不到符号“我将构造函数放在主类中在仪器类中,但我得到了同样的错误。我可能写错了构造函数,我会继续查找。
    • 这个类被命名为instrument,而不是Instrument。你应该把它命名为Instrument
    • 嗯,好的。编辑:即使将仪器更改为仪器,我仍然有同样的错误。我可能错误地创建了构造函数。
    • 你需要将你的班级命名为Instrument
    • 即使将我的类命名为Instrument 并在Instrument 类中输入构造函数Instrument instrument = new Instrument();,我在array[i] = new instrument((int) (1 + Math.random() * 12)); 中也会遇到同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-18
    • 2020-06-21
    相关资源
    最近更新 更多