Friday, July 3, 2015

// // 1 comment

Java GradeBook Program Using Instance Variables

The program below is the answer to Deitel's Java How to Program (9th Edition) Chapter 3 Exercise 3.11.

Question: Modify class GradeBook (Fig. 3.10) as follows:
  1. Include a String instance variable that represents the name of the course’s instructor.
  2. Provide a set method to change the instructor’s name and a get method to retrieve it.
  3. Modify the constructor to specify two parameters — one for the course name and one for the instructor’s name.
  4. 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.

To run the application save both files with the same name as the class (because it is a public class) and with the .java file extension (in this case Ex03_11.java and Ex03_11_Test.java). Then compile both classes  and run Ex03_11_Test. Ex03_11 will not run because it does not have a main method.

To compile both classes at the same time using the command prompt, use the command
javac Ex03_11.java Ex03_11_Test.java

To run Ex03_11_Test, use the command
java Ex03_11_Test

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 *
 * Exercise 3.11 - Modified GradeBook Class
 * This Program Modifies Class GradeBook (Fig. 3.10) In The Book To Display
 * Course Name And Instructor's Name
 *
 */ 

 public class Ex03_11 {
    
     private String courseName; 
     private String instructorName; 
    
     public Ex03_11 (String name, String insName) {
         courseName = name;
         instructorName = insName;
     }
    
     public void setCourseName (String name) {
         courseName = name;
     }
    
     public String getCourseName () {
         return courseName;
     }
    
     public void setInstructorsName (String insName) {
         instructorName = insName;
     }
    
     public String getInstructorName () {
         return instructorName;
     }
    
     public void displayMessage () {
         System.out.printf ("Welcome to the gradebook for: %s!\nThis course is "
                   + "presented by: %s\n", getCourseName (), getInstructorName ());
     }

 }

Below is Class Ex03_11_Test.java to test class Ex03_11

//Exercise 3.11 - Modified GradeBook Class Test
//This Program Tests Ex03_11 To Demonstrate The Class’s Capabilities

import java.util.Scanner;

public class Ex03_11_Test {
        public static void main (String [] args) {
           
            Ex03_11 mygradebook = new Ex03_11 ("Course Name", "Instructor's Name");

     Scanner input = new Scanner (System.in);
            
            System.out.print ("Enter Your Course Name: ");
            String name = input.nextLine();
            mygradebook.setCourseName(name);
            System.out.print ("Enter Your Course Instructor's Name: ");
            String insName = input.nextLine();
            mygradebook.setInstructorsName(insName);
            
            System.out.println ();
            
            mygradebook.displayMessage();

    }
}


Click here to see other answers to Java How to Program.

1 comment:

  1. I just couldn't leave your website without telling you that I truly enjoyed the top quality info you present to your visitors. I will be back again frequently to check up on new posts.

    ReplyDelete


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately.

Thanks.