GradeBook Class with an Instance Variable, a set Method and a get Method

In our next application (Figs. B.5B.6), class GradeBook (Fig. B.5) maintains the course name as an instance variable so that it can be used or modified at any time during an application’s execution. The class contains three methods—setCourseName, getCourseName and displayMessage. Method setCourseName stores a course name in a GradeBook. Method getCourseName obtains a GradeBook’s course name. Method displayMessage, which now specifies no parameters, still displays a welcome message that includes the course name; as you’ll see, the method now obtains the course name by calling a method in the same class—getCourseName.


 1   // Fig. B.5: GradeBook.java
 2   // GradeBook class that contains a courseName instance variable
 3   // and methods to set and get its value.
 4
 5   public class GradeBook
 6   {
 7      private String courseName; // course name for this GradeBook
 8
 9      // method to set the course name              
10      public void setCourseName( String name )      
11      {                                             
12         courseName = name; // store the course name
13      } // end method setCourseName                 
14
15      // method to retrieve the course name
16      public String getCourseName()        
17      {                                    
18         return courseName;                
19      } // end method getCourseName        
20
21      // display a welcome message to the GradeBook user
22      public void displayMessage()
23      {
24         // calls getCourseName to get the name of
25         // the course this GradeBook represents
26         System.out.printf( "Welcome to the grade book for %s! ",
27            getCourseName() );
28      } // end method displayMessage
29   } // end class GradeBook


Fig. B.5 | GradeBook class that contains a courseName instance variable and methods to set and get its value.


 1   // Fig. B.6: GradeBookTest.java
 2   // Creating and manipulating a GradeBook object.
 3   import java.util.Scanner; // program uses Scanner
 4
 5   public class GradeBookTest
 6   {
 7      // main method begins program execution
 8      public static void main( String[] args )
 9      {
10         // create Scanner to obtain input from command window
11         Scanner input = new Scanner( System.in );
12
13         // create a GradeBook object and assign it to myGradeBook
14         GradeBook myGradeBook = new GradeBook();
15
16         // display initial value of courseName
17         System.out.printf( "Initial course name is: %s ",
18            myGradeBook.getCourseName() );
19
20         // prompt for and read course name
21         System.out.println( "Please enter the course name:" );
22         String theName = input.nextLine(); // read a line of text
23         myGradeBook.setCourseName( theName ); // set the course name
24         System.out.println(); // outputs a blank line
25
26         // display welcome message after specifying course name
27         myGradeBook.displayMessage();
28      } // end main
29   } // end class GradeBookTest

Initial course name is: null

Please enter the course name:
CS101 Introduction to Java Programming

Welcome to the grade book for
CS101 Introduction to Java Programming!


Fig. B.6 | Creating and manipulating a GradeBook object.

A typical instructor teaches more than one course, each with its own course name. Line 7 declares courseName as a variable of type String. Because the variable is declared in the body of the class but outside the bodies of the class’s methods (lines 10–13, 16–19 and 22–28), line 7 is a declaration for an instance variable. Every instance (i.e., object) of class GradeBook contains one copy of each instance variable. For example, if there are two GradeBook objects, each object has its own copy of courseName. A benefit of making courseName an instance variable is that all the methods of the class (in this case, GradeBook) can manipulate any instance variables that appear in the class (in this case, courseName).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset