// GradeBook.java
// GradeBook class that contains a courseName instance variable
// and methods to set and get its value.
public class GradeBook
{
private String courseName; // course name for this GradeBook
// method to set the course name
public void setCourseName( String name )
{
courseName = name; // store the course name
} // end method setCourseName
// method to retrieve the course name
public String getCourseName()
{
return courseName;
} // end method getCourseName
// display a welcome message to the GradeBook user
public void displayMessage()
{
// calls getCourseName to get the name of
// the course this GradeBook represents
System.out.printf( "Welcome to the grade book for\n%s!\n",
getCourseName() );
} // end method displayMessage
} // end class GradeBook
// GradeBookTest.java
// Creating and manipulating a GradeBook object.
import java.util.Scanner; // program uses Scanner
public class GradeBookTest
{
// main method begins program execution
public static void main( String[] args )
{
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
// create a GradeBook object and assign it to myGradeBook
GradeBook myGradeBook = new GradeBook();
// display initial value of courseName
System.out.printf( "Initial course name is: %s\n\n",
myGradeBook.getCourseName() );
// prompt for and read course name
System.out.println( "Please enter the course name:" );
String theName = input.nextLine(); // read a line of text
myGradeBook.setCourseName( theName ); // set the course name
System.out.println(); // outputs a blank line
// display welcome message after specifying course name
myGradeBook.displayMessage();
} // end main
} // end class GradeBookTest
No comments:
Post a Comment