Exercises

B.5 (Keyword new) What’s the purpose of keyword new? Explain what happens when you use it.

B.6 (Default Constructors) What is a default constructor? How are an object’s instance variables initialized if a class has only a default constructor?

B.7 (Instance Variables) Explain the purpose of an instance variable.

B.8 (Using Classes Without Importing Them) Most classes need to be imported before they can be used in an application. Why is every application allowed to use classes System and String without first importing them?

B.9 (Using a Class Without Importing It) Explain how a program could use class Scanner without importing it.

B.10 (set and get Methods) Explain why a class might provide a set method and a get method for an instance variable.

B.11 (Modified GradeBook Class) Modify class GradeBook (Fig. B.7) as follows:

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 a get 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.

B.12 (Modified Account Class) Modify class Account (Fig. B.9) to provide a method called debit that withdraws money from an Account. Ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the method should print a message indicating "Debit amount exceeded account balance." Modify class AccountTest (Fig. B.10) to test method debit.

B.13 (Invoice Class) Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.

B.14 (Employee Class) Create a class called Employee that includes three instance variables—a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.

B.15 (Date Class) Create a class called Date that includes three instance variables—a month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates class Date’s capabilities.

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

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