Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Download: http://solutionzip.com/downloads/grade-book-java/
- Ex 7.10 (To modify Gradebook class in Fig 7.4)
- 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 get a 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, followed 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.
- This Java program uses existing class in example Fig 7.4. So you may first copy it to your Textpad and work on it by adding your new Java code and logic. You may copy an existing Java program example like Fig 7.4 below:
- Sample Java Code can be used for your program from Fig 7.4
- // Fig. 7.4: GradeBook.java
- // Class declaration with a method that has a parameter.
- public class GradeBook
- {
- // display a welcome message to the GradeBook user
- public void displayMessage( String courseName )
- {
- System.out.printf( “Welcome to the grade book for\n%s!\n”,
- courseName );
- } // end method displayMessage
- } // end class GradeBook
- This example is a foundation to your own solution of your individual Ex 7.10, but you need add new logic into it. In your new class you need to:
- 1. To add a new string instance variable for course instructor by changing
- public void displayMessage( String courseName )
- to
- public void displayMessage( String courseName, String courseInstructor )
- 2. To provide two new methods – get and set like set and get courseName in Fig 7.4 on page 309. An example is below
- 1) isPrime (to determine whether or not current number you are checking is a prime number or not.)
- // method to set the instructor name
- public void setInstructorName( String name )
- {
- instructorName = name; // store the instructor name
- } // end method setInstructorName
- 3. Modify constructor to specify two variables to include new variable courseInstructor.
- public GradeBook( String course, String instructor )
- {
- courseName = course; // initializes courseName
- instructorName = instructor; // initializes instructorName
- } // end constructor
- 4. Modify display message to include the new variable courseInstructor.
- public void displayMessage( String courseName )
- {
- System.out.printf( “Welcome to the grade book for\n%s!\n”,
- courseName );
- } // end method displayMessage
- you may create another method to display instructor name in the same way by modifying Java code above. Then you will be able to display instructor name in instructorName:
- public void displayInstructorMessage( String instructorName )
- {
- System.out.printf( “Welcome to the grade book for Instructor \n%s!\n”,
- instructorName );
- } // end method displayMessage
- 5. In main method certain changes or new additional logic are necessary to include new variable courseInstructor
- // Fig. 7.5: GradeBookTest.java
- // Create GradeBook object and pass a String to
- // its displayMessage method.
- 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();
- // prompt for and input course name
- System.out.println( “Please enter the course name:” );
- String courseName = input.nextLine(); // read a line of text
- System.out.println(); // outputs a blank line
- // call myGradeBook’s displayMessage method
- // and pass courseName as an argument
- myGradeBook.displayMessage( courseName );
- } // end main
- Download: http://solutionzip.com/downloads/grade-book-java/
Advertisement
Add Comment
Please, Sign In to add comment