• Tidak ada hasil yang ditemukan

Exercises

Dalam dokumen Chapter 14. Defining Classes (Halaman 112-115)

1. Define a class

Student

, which contains the following information about students: full name, course, subject, university, e-mail and phone number.

2. Declare several constructors for the class

Student

, which have different lists of parameters (for complete information about a student or part of it). Data, which has no initial value to be initialized with

null

. Use nullable types for all non-mandatory data.

3. Add a static field for the class

Student

, which holds the number of created objects of this class.

4. Add a method in the class

Student

, which displays complete information about the student.

5. Modify the current source code of

Student

class so as to encapsulate the data in the class using properties.

6. Write a class

StudentTest

, which has to test the functionality of the class

Student

.

7. Add a static method in class

StudentTest

, which creates several objects of type

Student

and store them in static fields. Create a static property of the class to access them. Write a test program, which displays the information about them in the console.

8. Define a class, which contains information about a mobile phone:

model, manufacturer, price, owner, features of the battery (model, idle time and hours talk) and features of the screen (size and colors).

9. Declare several constructors for each of the classes created by the previous task, which have different lists of parameters (for complete information about a student or part of it). Data fields that are unknown have to be initialized respectively with

null

or

0

.

10. To the class of mobile phone in the previous two tasks, add a static field

nokiaN95

, which stores information about mobile phone model Nokia N95. Add a method to the same class, which displays information about this static field.

11. Add an enumeration

BatteryType

, which contains the values for type of the battery (Li-Ion, NiMH, NiCd, …) and use it as a new field for the class

Battery

.

12. Add a method to the class

GSM

, which returns information about the object as a

string

.

13. Define properties to encapsulate the data in classes

GSM

,

Battery

and

Display

.

14. Write a class

GSMTest

, which has to test the functionality of class

GSM

. Create few objects of the class and store them into an array. Display information about the created objects. Display information about the static field

nokiaN95

.

15. Create a class

Call

, which contains information about a call made via mobile phone. It should contain information about date, time of start and duration of the call.

16. Add a property for keeping a call history

CallHistory

, which holds a list of call records.

17. In

GSM

class add methods for adding and deleting calls (

Call

) in the archive of mobile phone calls. Add method, which deletes all calls from the archive.

18. In

GSM

class, add a method that calculates the total amount of calls (

Call

) from the archive of phone calls (

CallHistory

), as the price of a phone call is passed as a parameter to the method.

19. Create a class

GSMCallHistoryTest

, with which to test the functionality of the class

GSM

, from task 12, as an object of type

GSM

. Then add to it a few phone calls (

Call

). Display information about each phone call.

Assuming that the price per minute is 0.37, calculate and display the total cost of all calls. Remove the longest conversation from archive with phone calls and calculate the total price for all calls again. Finally, clear the archive.

20. There is a book library. Define classes respectively for a book and a library. The library must contain a name and a list of books. The books must contain the title, author, publisher, release date and ISBN-number.

In the class, which describes the library, create methods to add a book to the library, to search for a book by a predefined author, to display information about a book and to delete a book from the library.

21. Write a test class, which creates an object of type library, adds several books to it and displays information about each of them. Implement a test functionality, which finds all books authored by Stephen King and deletes them. Finally, display information for each of the remaining books.

22. We have a school. In school we have classes and students. Each class has a number of teachers. Each teacher has a variety of disciplines taught. Students have a name and a unique number in the class. Classes have a unique text identifier. Disciplines have a name, number of lessons and number of exercises. The task is to shape a school with C# classes.

You have to define classes with their fields, properties, methods and constructors. Also define a test class, which demonstrates, that the other classes work correctly.

23. Write a generic class

GenericList<T>

, which holds a list of elements of type

T

. Store the list of elements into an array with a limited capacity that is passed as a parameter of the constructor of the class. Add methods to add an item, to access an item by index, to remove an item by index, to insert an item at given position, to clear the list, to search for an item by value and to override the method

ToString()

.

24. Implement auto-resizing functionality of the array from the previous task, when by adding an element, it reaches the capacity of the array.

25. Define a class

Fraction

, which contains information about the rational fraction (e.g. ¼ or ½). Define a static method

Parse()

to create a fraction from a sting (for example -3/4). Define the appropriate

properties and constructors of the class. Also write property of type

Decimal

to return the decimal value of the fraction (e.g. 0.25).

26. Write a class

FractionTest

, which tests the functionality of the class

Fraction

from previous task. Pay close attention on testing the function Parse with different input data.

27. Write a function to cancel a fraction (e.g. if numerator and denominator are respectively 10 and 15, fraction to be cancelled to 2/3).

Dalam dokumen Chapter 14. Defining Classes (Halaman 112-115)

Dokumen terkait