作业帮 > 综合 > 作业

Java的一个关于类的基础程序.

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/08/19 07:50:47
Java的一个关于类的基础程序.
Modify class GradeBook (Fig.3.10) as follows:
a) Include a String instance variable that represents the name of the course’s instructor.
b) Provide a set method to change the instructor’s name and a get method to retrieve it.
c) Modify the constructor to specify two parameters—one for the course name and one
for the instructor’s name.
d) Modify method displayMessage to output the welcome message and course name,fol-
lowed by "This course is presented by:" and the instructor’s name.
Use your modified class in a test application that demonstrates the class’s new capabilities.
Fig.3.10中的程序:
public class GradeBook
{
private String courseName;
}
public GradeBook( String name )
{
courseName = name;
}
public void setCourseName( String name )
{
courseName = name;
}
public String getCourseName()
{
return courseName;
}
public void displayMessage()
{ System.out.printf( "Welcome to the grade book for\n%s!\n",
getCourseName() );
}
}
.我一开始先把如上的程序打上去,但都运行不了.然后就无力了.
Java的一个关于类的基础程序.
第三行多了个花括号吧 而且没有main()方法
改成这样应该是你要的结果
public class Console{
private String courseName;
public Console( String name ){
courseName = name;
}
public void setCourseName( String name ){
courseName = name;
}
public String getCourseName(){
return courseName;
}
public void displayMessage(){
System.out.printf( "Welcome to the grade book for\n%s!\n",getCourseName() );
}
public static void main(String[] args){
new Console("网友").displayMessage();
}
}