- 4.1 Introduction
- 4.2 Classes, Objects, Methods, Properties and Instance Variables
- 4.3 Declaring a Class with a Method and Instantiating an Object of a Class
- 4.4 Declaring a Method with a Parameter
- 4.5 Instance Variables and Properties
- 4.6 UML Class Diagram with a Property
- 4.7 Software Engineering with Properties and set and get Accessors
- 4.8 Auto-Implemented Properties
- 4.9 Value Types vs. Reference Types
- 4.10 Initializing Objects with Constructors
- 4.11 Floating-Point Numbers and Type decimal
- 4.12 Wrap-Up
- Summary
- Terminology
- Self-Review Exercises
- Answers to Self-Review Exercises
- Exercises
- Making a Difference Exercises
Exercises
4.5 |
What is the purpose of operator new? Explain what happens when this keyword is used in an application. |
4.6 |
What is a default constructor? How are an object's instance variables initialized if a class has only a default constructor? |
4.7 |
Explain the purpose of an instance variable. |
4.8 |
Explain how an application could use class Console without using a using directive. |
4.9 |
Explain why a class might provide a property for an instance variable. |
4.10 |
(GradeBook Modification) Modify class GradeBook (Fig. 4.12) as follows:
Use your modified class in a test application that demonstrates the class's new capabilities. |
4.11 |
(Account Modification) Modify class Account (Fig. 4.15) to provide a method called Debit that withdraws money from an Account. Ensure that the debit amount doesn't exceed the balance. If it does, the balance should not be changed and the method should display a message indicating "Debit amount exceeded account balance." Modify class AccountTest (Fig. 4.16) to test method Debit. |
4.12 |
(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 automatic 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 application named InvoiceTest that demonstrates class Invoice's capabilities. |
4.13 |
(Employee Class) Create a class called Employee that includes three pieces of information as either instance variables or automatic 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 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. |
4.14 |
(Date Class) Create a class called Date that includes three pieces of information as automatic 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 application named DateTest that demonstrates class Date's capabilities. |