java:constructor不能应用于给定的类型

时间:2016-07-18 05:50:46

标签: java

请帮助一个菜鸟。 我正在尝试运行一个项目,其中我有两个类,一个测试类,另一个我有构造函数和方法。

我必须输入课程名称和教师姓名, - 为每个对象创建2个对象 - 从键盘上读取它们 - 打印结果

完整的错误消息:

错误:(14,29)java:构造函数GradeBook中的GradeBook不能应用于给定的类型;   required:java.lang.String,java.lang.String   发现:没有争论   原因:实际和正式的参数列表长度不同

我以前曾就此主题进行过研究,但我找不到能帮助我的问题。

这是带有构造函数的类:

public class GradeBook{

    private String courseName; //course name for this gradebook
    private String instrName; //instructor's name for this gradebook

    //constructor initializes coursename with String argument
    public GradeBook(String name, String nameinstr)
    {
        courseName = name; //initializes courseName with name
         instrName = nameinstr; //initializez instrName with nameinstr

    }

    //method to set the instructor name;
    public void setInstrName(String nameinstr)
    {
        instrName = nameinstr;
    }

    public String getInstrName() //method to get the instructor name
    {
        return instrName;
    }
    //method to set the coursename

    public void setCourseName(String name)
    {
        courseName = name; //store the course name
    }

    //method to return the coursename

    public String getCourseName()
    {
        return courseName;
    }//end method getCourseName

    public void displayMessage()
    {
        System.out.printf("The course name is %s\n\n", getCourseName());
        System.out.printf("The name of the instructor is %s\n", getInstrName());
                //this statement calls getCoursename to get the name of the course this GradeBook represents
    }//end method displayMessage
}//end class GradeBook

以下是测试它的课程:

public class GradeBookTest

{
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        GradeBook course1 = new GradeBook(); //object 1 for the course name
        GradeBook instructor1 = new GradeBook(); //object 2 for the instructor's name

        System.out.println("Enter the name of the course:\n");
        String theName = input.nextLine();
        course1.setCourseName( theName );
        System.out.println();


        System.out.println("Enter the name of the instructor: \n");
        String theName2 = input.nextLine();
        instructor1.setInstrName( theName2);
        System.out.println();




        course1.displayMessage();
        instructor1.displayMessage();
    }
}

提前感谢您,并为noob问题感到抱歉

0 个答案:

没有答案