Introduction To Classes, Objects, Methods and Strings: (Modification) Modify Class
Introduction To Classes, Objects, Methods and Strings: (Modification) Modify Class
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 bal-
ance. If it does, the balance should not be changed and the method should display a message indi-
cating "Withdrawal amount exceeded account balance." Modify class AccountTest (Fig. 4.12) to
test method Withdraw.
4.10 (Student Record Class) Create a class called Student that an institute might use to represent
a record for students qualifying from the institute. A Student record should include five pieces of
information as either instance variables or auto-implemented properties: a student's id (type
string), a student's name (type string) and three separate variables for scores in three subjects (type
decimal). Your class should have a constructor that initializes the five values. Provide a property
with a get and set accessor for any instance variables. For the scores in subjects, if the value passed
to the set accessor is negative, the value of the instance variable should be left unchanged. Also, pro-
vide methods named GetAggregate and GetPercentage that calculate the aggregate marks in the
three subjects (sum of three subject marks) and the percentage (i.e., sum divided by the maximum
marks, 50, and then multiplied by 100), and then return the aggregate and percentage as decimal
value. Write a test app named StudentRecordTest that demonstrates class Student’s capabilities.
4.11 (Asset Class) Create a class called Asset that includes three pieces of information as either
instance variables or auto-implemented properties—asset name (type string), asset value (type
decimal) and depreciation rate (type 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 asset
value or depreciation rate is negative, the set accessor should leave the instance variable unchanged.
Write a test app named AssetTest that demonstrates class Asset’s capabilities. Create two Asset ob-
jects and display each object's amount of depreciation and their depreciated value. Then increase the
value of each asset by 5% and display each asset's amount of depreciation and their depreciated value.
4.12 (Coaching Class) Create a class called Coaching that includes four pieces of information as
auto implemented properties—the coaching type (type string), the number of players (type int),
class timings (type string) and charges (type decimal). Your class should have a constructor that
initializes the four automatic properties and assumes that the values provided are correct. Provide a
method DisplayDetails that displays the coaching type, number of players, class timing and
charges (in $) separated by tabs. Write a test app named CoachingTest that demonstrates class
Coaching'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 dis-
play 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 pa-
rameter 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 argu-
ment 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 accountToDis-
play’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.