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:
- Include a String instance variable that represents the name of the course’s instructor.
- Provide a set method to change the instructor’s name and a get method to retrieve it.
- Modify the constructor to specify two parameters — one for the course name and one for the instructor’s name.
- 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
1 2 |
/**<br /> *<br /> * @Author: Aghatise Osazuwa<br /> * Website: www.cscprogrammingtutorials.com<br /> *<br /> * Exercise 3.11 - Modified GradeBook Class<br /> * This Program Modifies Class GradeBook (Fig. 3.10) In The Book To Display<br /> * Course Name And Instructor's Name<br /> *<br /> */ <br /><br /> public class Ex03_11 {<br /> <br /> private String courseName; <br /> private String instructorName; <br /> <br /> public Ex03_11 (String name, String insName) {<br /> courseName = name;<br /> instructorName = insName;<br /> }<br /> <br /> public void setCourseName (String name) {<br /> courseName = name;<br /> }<br /> <br /> public String getCourseName () {<br /> return courseName;<br /> }<br /> <br /> public void setInstructorsName (String insName) {<br /> instructorName = insName;<br /> }<br /> <br /> public String getInstructorName () {<br /> return instructorName;<br /> }<br /> <br /> public void displayMessage () {<br /> System.out.printf ("Welcome to the gradebook for: %s!nThis course is "<br /> + "presented by: %sn", getCourseName (), getInstructorName ());<br /> }<br /><br /> }<br /> |
Below is Class Ex03_11_Test.java to test class Ex03_11
1 2 |
//Exercise 3.11 - Modified GradeBook Class Test<br />//This Program Tests Ex03_11 To Demonstrate The Class’s Capabilities<br /><br />import java.util.Scanner;<br /><br />public class Ex03_11_Test {<br /> public static void main (String [] args) {<br /> <br /> Ex03_11 mygradebook = new Ex03_11 ("Course Name", "Instructor's Name");<br /><br /> Scanner input = new Scanner (System.in);<br /> <br /> System.out.print ("Enter Your Course Name: ");<br /> String name = input.nextLine();<br /> mygradebook.setCourseName(name);<br /> System.out.print ("Enter Your Course Instructor's Name: ");<br /> String insName = input.nextLine();<br /> mygradebook.setInstructorsName(insName);<br /> <br /> System.out.println ();<br /> <br /> mygradebook.displayMessage();<br /><br /> }<br />}<br /> |
1 Comment
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.