3.5 |
What is the purpose of keyword new? Explain what happens when this keyword is used in an application.
|
3.6 |
What is a default constructor? How are an object's instance variables initialized if a class has only a default constructor?
|
3.7 |
Explain the purpose of an instance variable.
|
3.8 |
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?
|
3.9 |
Explain how a program could use class Scanner without importing the class from package java.util.
|
3.10 |
Explain why a class might provide a set method and a get method for an instance variable.
|
3.11 |
Modify class GradeBook (Fig. 3.10) as follows:
- Include a second 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 parametersone for the course name and one for the instructor's name.
- Modify method displayMessage such that it first outputs the welcome message and course name, then outputs "This course is presented by: " followed by the instructor's name.
Use your modified class in a test application that demonstrates the class's new capabilities.
|
3.12 |
Modify class Account (Fig. 3.13) 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. 3.14) to test method debit.
|
3.13 |
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 variablesa 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.
|
3.14 |
Create a class called Employee that includes three pieces of information as instance variablesa first name (type String), a last name (type String) and a monthly salary (double). Your class should have 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, set it to 0.0. 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.
|
3.15 |
Create a class called Date that includes three pieces of information as instance variablesa month (type int), a day (type int) and a year (type int). Your class should have 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.
|