Results 1 to 2 of 2
  1. #1
    cart1443 is offline Member
    Join Date
    Feb 2008
    Posts
    5
    Rep Power
    0

    Default Why does it still have it as "Null"?

    So why are the different instructor names showing up as "null" when the course name doesn't? They are formatted the same. Am I missing something?

    PHP Code:
    // Lab 2: GradeBook.java
    // GradeBook class with a constructor to initialize the course name.
    
    public class GradeBook
    {
       private String courseName; // course name for this GradeBook
       /* write code to declare a second String instance variable */
       private String InstructorName;
       
       // constructor initializes courseName with String supplied as argument
       public GradeBook( String name ) 
       {  
          courseName = name; // initializes courseName 
       } // end constructor 
    
       // 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
    
       /* write code to declare a get and a set method for the instructor’s name */
       public void setInstructorName( String Iname )
       {
          InstructorName = Iname; // store the course name
       } // end method setCourseName
    
       // method to retrieve the course name
       public String getInstructorName()
       {
          return InstructorName;
        }
       
        // 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.printf( "Welcome to the grade book for\n%s!\n\n", 
             getCourseName() );
          System.out.printf( "This course is presented by: %s!\n", 
             getInstructorName() );
          /* write code to output the instructor’s name */
       } // end method displayMessage
    
    } // end class GradeBook
    PHP Code:
    // Lab 2: GradeBookTest.java
    // GradeBook constructor used to specify the course name at the 
    // time each GradeBook object is created.
    
    public class GradeBookTest
    {
       // main method begins program execution
       public static void main( String args[] )
       { 
          // create GradeBook object
          GradeBook gradeBook1 = new GradeBook("CS101 Introduction to Java Programming" );
          GradeBook Iname1 = new GradeBook("Sam Smith");
          GradeBook Iname2 = new GradeBook("Judy Jones");
    
          gradeBook1.displayMessage(); // display welcome message
          
          /* write code to change instructor’s name and output changes */
          
          Iname2.setInstructorName(Iname2.getInstructorName());
          System.out.printf("\nChanging instructor name to %s", 
                Iname2.getInstructorName() );
       } // end main
    
    } // end class GradeBookTest

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The only way to set the name for the instructor is with the setInstructorName method which you did not use in your main method. Your only GradeBook constructor requires a String argument whose value is assigned to the "courseName" member variable. The member variable "InstructorName remains null until you assign it a value via the setInstructorName method.
    Java Code:
       public static void main( String args[] ) 
       {  
          GradeBook Iname1 = new GradeBook("Sam Smith"); 
          Iname1.setInstructorName("John Stuart");
          // Now it won't be null for the "Iname1" instance.
          ...

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. Name Coming Up As "null"
    By sylo18 in forum New To Java
    Replies: 8
    Last Post: 10-30-2007, 11:31 PM
  3. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 11:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •