I am using netbeans and my code is as follows
public class GradeBook
{
private String courseName; // course name for this GradeBook
private String courseInstructor; // instructor name for this GradeBook
// constructor initializes courseName and courseInstructor with String Argument
public GradeBook( String name, String insname ) // constructor name is class name
{
courseName = name; // initializes courseName
courseInstructor = insname; // initializes courseInstructor
} // end constructor
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
} // end method setCourse
// method to retrieve the course name
public String getCourseName()
{
return courseName;
} // end method getCourseName
// method to set the Instructor name
public void setInstructorName( String insname)
{
courseInstructor = insname; // store the Instructor name
} // end method setInstructorName
// method to retrieve the Instructor name
public String getInstructorName()
{
return courseInstructor;
} // end method getInstructorName
// display a welcome message to the GradeBook user
public void displayMessage()
{
// this statement calls getCourseName to get the
// name of the course this GradeBook represents
System.out.println( "\nWelcome to the grade book for: \n"+
getCourseName()+"\nThis course is presented by: "+getInstructorName());
System.out.println( "\nProgrammed by Jack Friedman");
} // end method displayMessage
} // end
You should call the constructor of this class in your main method.
Make a new class GradeBookTest as follows:
public class GradeBookTest {
public static void main (String args[]) {
GradeBook book = new GradeBook("Math", "T.I.");
book.displayMessage(); //To see your results
}
}
Now you can launch this class to view your results.
请向您的应用程序添加静态main方法。
Where is the main method? Include below code to run your program-
public static void main(String[] args) {
GradeBook book = new GradeBook("subj1", "instruc1");
book.displayMessage();
}
In the Java programming language, every application must contain a main method whose signature is:
public static void main(String[] args)
main
method is the entry point for a java program, it has to be declared as public
so that it can be accessed from outside the class and static
so that it can be accessed even without creating an instance or object of the class.
For better understanding please read this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.