Wrap-Up

Answers to Self Review Exercises

4.1

a) object. b) class. c) new. d) type, name. e) global namespace. f) instance variable. g) float, double, decimal. h) double-precision. i) ToDecimal. j) access modifier. k) void. l) ReadLine. m) using directive. n) single-precision. o) C. p) value, reference.

4.2

a) False. By convention, method names begin with an uppercase first letter and all subsequent words in the name begin with a capital first letter. b) False. A property's get accessor enables a client to retrieve the value of the instance variable associated with the property. A property's set accessor enables a client to modify the value of the instance variable associated with the property. c) True. d) True. e) False. After defining a property, you can use it the same way you use a variable. f) True. g) False. Such variables are called local variables and can be used only in the method in which they are declared. h) False. A property declaration can contain a get accessor, a set accessor or both. i) True. j) False. Instance variables are initialized by default. k) True. l) True. m) True. n) False. Such literals are of type double by default.

4.3

A local variable is declared in the body of a method and can be used only from the point at which it is declared through the end of the method declaration. An instance variable is declared in a class, but not in the body of any of the class's methods. Every object (instance) of a class has a separate copy of the class's instance variables. Also, instance variables are accessible to all methods of the class. (We will see an exception to this in Chapter 9, Classes and Objects: A Deeper Look.)

4.4

A parameter represents additional information that a method requires to perform its task. Each parameter required by a method is specified in the method's declaration. An argument is the actual value that is passed to a method parameter when a method is called.

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

Modify class GradeBook (Fig. 4.12) as follows:

  1. Include a second string instance variable that represents the name of the course's instructor.
  2. Provide a property with accessors to change the instructor's name and to retrieve it.
  3. Modify the constructor to specify two parametersone for the course name and one for the instructor's name.
  4. 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.

4.11

Modify class Account (Fig. 4.15) 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. 4.16) to test method Debit.

4.12

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 (decimal). Your class should have a constructor that initializes the four instance variables. Provide a property with a get and set accessor 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 decimal value. If the quantity is negative, it should be left unchanged. Similarly, if the price per item is negative, it should be left unchanged. Write a test application named InvoiceTest that demonstrates class Invoice's capabilities.

4.13

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 (decimal). Your class should have a constructor that initializes the three instance variables. Provide a property with a get and set accessor for each instance variable. 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

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 property with a get and set accessor 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.

Control Statements Part 1

Категории