Exercises

  1. 10.3 (Rectangle Class) Create class Rectangle. The class has attributes length and width, each of which defaults to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle. It has properties for both length and width. The set accessors should verify that length and width are each floating-point numbers greater than 0.0 and less than 20.0. Write an app to test class Rectangle.

  2. 10.4 (Modifying the Internal Data Representation of a Class) It would be perfectly reasonable for the Time2 class of Fig. 10.5 to represent the time internally as the number of seconds since midnight rather than the three integer values hour, minute and second. Clients could use the same public methods and properties to get the same results. Modify the Time2 class of Fig. 10.5 to implement the Time2 as the number of seconds since midnight and show that no change is visible to the clients of the class by using the same test app from Fig. 10.6.

  3. 10.5 (Savings-Account Class) Create the class SavingsAccount. Use the static read-write property AnnualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a property SavingsBalance, indicating the amount the saver currently has on deposit. Provide method CalculateMonthlyInterest to calculate the monthly interest by multiplying the SavingsBalance by AnnualInterestRate divided by 12—this interest should be added to savings-Balance. Write an app to test class SavingsAccount. Create two savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set AnnualInterestRate to 4%, then calculate the monthly interest and display the new balances for both savers. Then set the AnnualInterestRate to 5%, calculate the next month’s interest and display the new balances for both savers.

  4. 10.6 (Enhancing Class Date) Modify class Date of Fig. 10.7 to perform error checking on the initializer values for instance variables month, day and year (class Date currently validates only the month and day). You’ll need to convert the auto-implemented property Year into instance variable year with an associated Year property. Provide method NextDay to increment the day by 1. The Date object should always maintain valid data and throw exceptions when attempts are made to set invalid values. Write an app that tests the NextDay method in a loop that displays the date during each iteration of the loop to illustrate that the NextDay method works correctly. Test the following cases:

    1. incrementing to the next month and

    2. incrementing to the next year.

  5. 10.7 (Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0–100. The set is represented by an array of bools. Array element a[i] is true if integer i is in the set. Array element a[j] is false if integer j is not in the set. The parameterless constructor initializes the array to the “empty set” (i.e., a set whose array representation contains all false values). Provide the following methods:

    1. Method Union creates a third set that’s the set-theoretic union of two existing sets (i.e., an element of the third set’s array is set to true if that element is true in either or both of the existing sets—otherwise, the element of the third set is set to false).

    2. Method Intersection creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set’s array is set to false if that element is false in either or both of the existing sets—otherwise, the element of the third set is set to true).

    3. Method InsertElement inserts a new integer k into a set (by setting a[k] to true).

    4. Method DeleteElement deletes integer m (by setting a[m] to false).

    5. Method ToString returns a string containing a set as a list of numbers separated by spaces. Include only those elements that are present in the set. Use --- to represent an empty set.

    6. Method IsEqualTo determines whether two sets are equal.

    Write an app to test class IntegerSet. Instantiate several IntegerSet objects. Test that all your methods work properly.

  6. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form.

    The fraction

    2/4

    is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a parameterless constructor with default values in case no initializers are provided. Provide public methods that perform each of the following operations (all calculation results should be stored in a reduced form):

    1. Add two Rational numbers.

    2. Subtract two Rational numbers.

    3. Multiply two Rational numbers.

    4. Divide two Rational numbers.

    5. Display Rational numbers in the form a/b, where a is the numerator and b is the denominator.

    6. Display Rational numbers in floating-point format. (Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.)

  7. 10.9 (HugeIntegerClass) Create a class HugeInteger which uses a 40-element array of digits to store integers as large as 40 digits each. Provide methods Input, ToString, Add and Subtract. For comparing HugeInteger objects, provide the following methods: IsEqualTo, IsNotEqualTo, IsGreaterThan, IsLessThan, IsGreaterThanOrEqualTo and IsLessThanOrEqualTo. Each of these is a method that returns true if the relationship holds between the two HugeInteger objects and returns false if the relationship does not hold. Provide method IsZero. If you feel ambitious, also provide methods Multiply, Divide and Remainder. In the Input method, use the string method ToCharArray to convert the input string into an array of characters, then iterate through these characters to create your HugeInteger. If you feel ambitious, provide methods Multiply, Divide and Remainder. [Note: The .NET Framework Class Library includes type BigInteger for arbitrary sized integer values.]

  8. 10.10 (Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has been won and whether it’s a draw. If you feel ambitious, modify your app so that the computer makes the moves for one of the players. Also, allow the player to specify whether he or she wants to go first or second. If you feel exceptionally ambitious, develop an app that will play three-dimensional Tic-Tac-Toe on a 4-by-4-by-4 board.

  9. 10.11 (Extension Methods for Class Time2) Use an extension method to provide for class Time2 (Fig. 10.5) a Tick method that increments the time by one second. The Time2 object should always remain in a consistent state, so be sure to account for and test the following cases:

    1. incrementing into the next minute.

    2. incrementing into the next hour.

    3. incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).

  10. 10.12 (Enhanced ComplexNumberClass) Enhance class ComplexNumber (Fig. 10.14) with overloaded == and != operators that enable you to compare complex numbers for equality and inequality. Each overloaded-operator method should return a bool. Test your new overloaded operators.

  11. 10.13 (Project: Replacing Rational Methods with Overloaded Operators) Modify class Rational (Exercise 10.8) to declare overloaded operators +, -, * and / that replace the class’s methods for adding, subtracting, multiplying and dividing fractions. Test your new overloaded operators.

  12. 10.14 (Project: Replacing HugeInteger Methods with Overloaded Operators) Modify class HugeInteger (Exercise 10.9) to declare overloaded operators +, -, ==, !=, >, <, >= and <= that replace the class’s methods Add, Subtract, IsEqualTo, IsNotEqualTo, IsGreaterThan, IsLessThan, IsGreaterThanOrEqualTo and IsLessThanOrEqualTo. Test your new overloaded operators. If you feel ambitious, provide overloaded operators for multiplication (*), division (/) and remainder (%).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset