Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline 19.1 Test-Driving the Microwave Oven Application 19.2 Designing.

Similar presentations


Presentation on theme: "Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline 19.1 Test-Driving the Microwave Oven Application 19.2 Designing."— Presentation transcript:

1 Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects
Outline Test-Driving the Microwave Oven Application Designing the Microwave Oven Application Adding a New Class to the Project Initializing Class Objects: Constructors Properties Completing the Microwave Oven Application Controlling Access to Members Using the Debugger: The Autos and Locals Windows Wrap-Up

2 In this tutorial, you will learn to:
Objectives In this tutorial, you will learn to: Create your own classes. Create and use objects of your own classes. Control access to object instance variables. Use keyword private. Create your own properties. Use the Panel control. Use String methods PadLeft and Substring.

3 19.1 Test-Driving the Microwave Oven Application

4 19.1 Test-Driving the Microwave Oven Application
Figure 19.1 Microwave Oven application’s Form. Numeric keypad (Buttons appear flat) Microwave’s glass window

5 19.1 Test-Driving the Microwave Oven Application
Figure 19.2 Microwave Oven application accepts only four digits.

6 19.1 Test-Driving the Microwave Oven Application
Figure 19.3 Microwave Oven application with invalid input.

7 19.1 Test-Driving the Microwave Oven Application
Figure 19.4 Microwave Oven application after invalid input has been entered and the Start Button clicked.

8 19.1 Test-Driving the Microwave Oven Application
Figure 19.5 Microwave Oven application with valid time entered and inside light turned on (it’s now cooking). Color yellow simulates the microwave light

9 19.1 Test-Driving the Microwave Oven Application
Figure 19.6 Microwave Oven application after the cooking time has elapsed. Label displays Done! when cooking is finished Color returns to default color to simulate that cooking has finished

10 19.2 Designing the Microwave Oven Application
Panel control Can group controls as do GroupBoxes Cannot display a caption

11 19.2 Designing the Microwave Oven Application

12 19.2 Designing the Microwave Oven Application
Figure 19.8 Rearranging and commenting the new control declaration.

13 19.2 Designing the Microwave Oven Application
Figure 19.9 Variable m_strTime contains the user’s input.

14 19.2 Designing the Microwave Oven Application
Figure 19.10 Typical numeric event handler. When a number is entered, add the number to the input and display the new time

15 19.2 Designing the Microwave Oven Application
Figure 19.11 Microwave Oven application’s remaining event handlers. btnStart_Click creates an object to store time and begin cooking btnClear_Click resets variables and Label DisplayTime formats time information for display tmrClock_Click performs countdown and updates display

16 19.3 Adding a New Class to the Project
Adding a class file to your project Select Project > Add Class In the Add New Item dialog, select Class, and enter a name for the class

17 19.3 Adding a New Class to the Project
Figure 19.12 Add New Item dialog allows you to create a new class. Select Class as new item Name of new class

18 19.3 Adding a New Class to the Project
Figure 19.13 Solution Explorer displaying new class file. New file displayed in Solution Explorer

19 19.3 Adding a New Class to the Project
Figure 19.14 Default class declaration. Empty class definition added by Visual Studio .NET

20 19.3 Adding a New Class to the Project
Figure 19.15 Time’s instance variables. Instance variables store minute and second information Instance variables of a class are defined within its class definition

21 19.4 Initializing Class Objects: Constructors
Special method within a class definition that is used to initialize a class’s instance variables. Can take arguments but cannot return values Has same name as class containing it Initializing variables in a constructor – Time m_objTimeObject = new Time( 5, 3 ); – Notice the new keyword Extensible languages Languages that can be “extended” with new data types C# is an extensible language

22 19.4 Initializing Class Objects: Constructors
Figure 19.16 Empty constructor. Time is the constructor method

23 19.4 Initializing Class Objects: Constructors
Figure 19.17 Constructor initializing instance variables. Initialize instance variables

24 19.4 Initializing Class Objects: Constructors
Figure 19.18 Declaring an object of type Time. Declare m_objTime of programmer-defined type Time Instantiate (create) an object of type Time

25 19.5 Properties Properties Property definition
Provided to allow clients to access and modify instance variables safely Contain accessors Property definition – Consists of two accessors – set accessor – allows clients to set properties – get accessor – allows clients to get properties

26 Properties Figure 19.19 Empty Minute property. get accessor retrieves data set accessor stores data The accessor methods are meant to keep the property in a consistent state (that is, valid)

27 19.5 Properties Figure 19.20 get accessor definition.
Returning data from a property

28 19.5 Properties Figure 19.21 set accessor definition.
Properties used to validate data

29 Properties Figure 19.22 Second property. Property Second

30 19.5 Properties Figure 19.23 Second property definition.
Second property performs similar data manipulations

31 Properties Figure 19.24 Constructor using properties to initialize variables. Safer to assign data to properties rather than instance variables, because set accessors perform validity checking

32 19.6 Completing the Microwave Oven Application
String methods Length property – returns the number of characters in a string Padleft – Adds characters to the beginning of the string until the length of the string equals the specified length Substring – returns specified characters from a string

33 19.6 Completing the Microwave Oven Application
Figure 19.25 Declaring variables for second and minute values. Ensure m_strTime has four characters for conversion purposes

34 19.6 Completing the Microwave Oven Application
Figure 19.26 Forming minute and second values from input. Extracting seconds and minutes

35 19.6 Completing the Microwave Oven Application
Figure 19.27 Creating a Time object. Use keyword new to create a new object

36 19.6 Completing the Microwave Oven Application
Figure 19.28 Time appearing as a type in an Intellisense window. Time appears as a type in the Intellisense window

37 19.6 Completing the Microwave Oven Application
Figure 19.29 Displaying time information with separating colon. Display time information

38 19.6 Completing the Microwave Oven Application
Figure 19.30 Properties of a programmer-defined type also appear in Intellisense. Time’s properties appear in Intellisense

39 19.6 Completing the Microwave Oven Application
Figure 19.31 Starting the microwave oven countdown. Start timer and turn “light” on to indicate microwave oven is cooking

40 19.6 Completing the Microwave Oven Application
Clearing the cook time Set application’s Label to Microwave Oven Clear m_strTime Reset Time object to zero minutes and zero seconds Stop the countdown by disabling Timer Set Panel’s background to the Panel’s original color Simulates turning off light

41 19.6 Completing the Microwave Oven Application
Figure 19.32 Clearing the Microwave Oven input. Clearing the input

42 19.6 Completing the Microwave Oven Application
Displaying data as it is being input Declare int variables for storing minute and second Declare string variable Displays current input in proper format Remove extra digits entered by user

43 19.6 Completing the Microwave Oven Application
Figure 19.33 Modifying invalid user input.

44 19.6 Completing the Microwave Oven Application
Figure 19.34 Display current input.

45 19.6 Completing the Microwave Oven Application
Figure 19.35 Modifying the display during countdown. Modify Time appropriately during countdown

46 19.7 Controlling Access to Members
Member-access modifiers public – Specifies that instance variables and methods are accessible wherever the application has a reference to that object private – Specifies that instance variables or methods are accessible only to methods, properties and events of that class

47 19.7 Controlling Access to Members
Figure 19.36 Time’s instance variables are private.

48 19.7 Controlling Access to Members
Figure 19.37 FrmMicrowaveOven’s instance variables are private.

49 19.7 Controlling Access to Members
Figure 19.38  FrmMicrowaveOven’s methods are private.

50 MicrowaveOven.cs (1 of 11)

51 MicrowaveOven.cs (2 of 11) Declaring instance variable as private Creating an object of a programmer-defined type

52 MicrowaveOven.cs (3 of 11)

53 MicrowaveOven.cs (4 of 11)

54 MicrowaveOven.cs (5 of 11)

55 MicrowaveOven.cs (6 of 11)

56 MicrowaveOven.cs (7 of 11)

57 MicrowaveOven.cs (8 of 11) Creating a new object of a programmer-defined type Accessing properties of a programmer-defined type

58 Use the BackColor property to change the Panel’s color
MicrowaveOven.cs (9 of 11) Use the SystemColors.Control property to restore the default background color to the Panel Declaring a method as private

59 The Length property returns number of characters in a string
MicrowaveOven.cs (10 of 11) The Substring method returns a subset of characters in a string The PadLeft method appends characters to the beginning of a string

60 MicrowaveOven.cs (11 of 11)

61 Time.cs (1 of 4) Keyword class used to define a class Constructor name must be the class name Assign data to properties rather than to instance variables directly Right brace ends constructor definition

62 Time.cs (2 of 4)

63 Time.cs (3 of 4) get accessor returns data set accessor modifies data

64 Right brace ends property definition
Right brace ends class declaration Time.cs (4 of 4)

65 19.8 Using the Debugger: The Autos and Locals Windows
Allow the client to view the values stored in an object’s instance variables Autos window • Displays the contents of the properties used in the next and last statement to be executed Locals window • Displays the state of the variables in the current scope

66 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.41 Microwave Oven application with breakpoints added.

67 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.42 Empty Autos window. Figure 19.43 Empty Locals window.

68 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.44 Autos window displaying the state of m_objTime. Properties of m_objTime Property values Property types

69 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.45 Locals window displaying the state of m_objTime. Instance variables of m_objTime

70 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.46 Autos window displaying changed variables in red. Changed values shown in red Figure 19.47 Locals window displaying changed variables in red.

71 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.48 Changing the value of a variable in the Autos window. Value changed by user Double clicking a value allows the client to change the value while application is running

72 19.8 Using the Debugger: The Autos and Locals Windows
Figure 19.49 New variables listed in the Autos window. New variables shown in Autos window There are new variables in the Autos window because execution has reached a statement that uses different variables


Download ppt "Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline 19.1 Test-Driving the Microwave Oven Application 19.2 Designing."

Similar presentations


Ads by Google