4.5 (Keyword new
) What is the purpose of operator new
? Explain what happens when this keyword is used in an app.
4.6 (Default Constructor) What is a default constructor? How are an object’s instance variables initialized if a class has only a default constructor?
4.7 (Instance Variable) Explain the purpose of an instance variable.
4.8 (Properties) Explain why a class might provide a property for an instance variable.
4.9 (Account
Modification) Modify class Account
(Fig. 4.11) to provide a Withdraw
method that withdraws money from an Account
. Ensure that the withdrawal amount doesn’t exceed the balance. If it does, the balance should not be changed and the method should display a message indicating "Withdrawal amount exceeded account balance."
Modify class AccountTest
(Fig. 4.12) to test method Withdraw
.
4.10 (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 either instance variables or auto-implemented properties—a part number (type string
), a part description (type string
), a quantity of the item being purchased (type int
) and a price per item (decimal
). Your class should have a constructor that initializes the four values. Provide a property with a get
and set
accessor for any instance variables. For the Quantity
and PricePerItem
properties, if the value passed to the set
accessor is negative, the value of the instance variable should be left unchanged. Also, 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 decimal
value. Write a test app named InvoiceTest
that demonstrates class Invoice
’s capabilities.
4.11 (Employee
Class) Create a class called Employee
that includes three pieces of information as either instance variables or auto-implemented properties—a first name (type string
), a last name (type string
) and a monthly salary (decimal
). Your class should have a constructor that initializes the three values. Provide a property with a get
and set
accessor for any instance variables. If the monthly salary is negative, the set
accessor should leave the instance variable unchanged. Write a test app 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.
4.12 (Date
Class) Create a class called Date
that includes three pieces of information as auto-implemented properties—a month (type int
), a day (type int
) and a year (type int
). Your class should have a constructor that initializes the three automatic properties and assumes that the values provided are correct. Provide a method DisplayDate
that displays the month, day and year separated by forward slashes (/
). Write a test app named DateTest
that demonstrates class Date
’s capabilities.
4.13 (Removing Duplicated Code in Method Main
) In the AccountTest
class of Fig. 4.12, method Main
contains six statements (lines 13–14, 15–16, 26–27, 29–29, 39–40 and 41–42) that each display an Account
object’s Name
and Balance
. Study these statements and you’ll notice that they differ only in the Account
object being manipulated—account1
or account2
. In this exercise, you’ll define a new DisplayAccount
method that contains one copy of that output statement. The method’s parameter will be an Account
object and the method will output the object’s Name
and Balance
. You’ll then replace the six duplicated statements in Main
with calls to DisplayAccount
, passing as an argument the specific Account
object to output.
Modify class AccountTest
class of Fig. 4.12 to declare the following DisplayAccount
method after the closing right brace of Main
and before the closing right brace of class AccountTest
:
static void DisplayAccount(Account accountToDisplay)
{
// place the statement that displays
// accountToDisplay's Name and Balance here
}
Replace the comment in the member function’s body with a statement that displays accountToDisplay
’s Name
and Balance
.
Note that Main
is a static
method. We also declared method DisplayAccount
as a static
method. When Main
needs to call another method in the same class without first creating an object of that class, the other method also must be declared static
.
Once you’ve completed DisplayAccount
’s declaration, modify Main
to replace the statements that display each Account
’s Name
and Balance
with calls to DisplayAccount
—each receiving as its argument the account1
or account2
object, as appropriate. Then, test the updated AccountTest
class to ensure that it produces the same output as shown in Fig. 4.12.