1 / 72

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects.

arlais
Download Presentation

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 19 - Microwave Oven ApplicationBuilding Your Own Classes and Objects Outline19.1 Test-Driving the Microwave Oven Application19.2 Designing the Microwave Oven Application19.3 Adding a New Class to the Project19.4 Initializing Class Objects: Constructors19.5 Properties19.6 Completing the Microwave Oven Application19.7 Controlling Access to Members19.8 Using the Debugger: The Autos and Locals Windows19.9 Wrap-Up

  2. 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. Microwave’s glass window Numeric keypad (Buttons appear flat) 19.1 Test-Driving the Microwave Oven Application Figure 19.1 Microwave Oven application’s Form.

  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 StartButton clicked.

  8. Color yellow simulates the microwave light 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).

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

  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. When a number is entered, add the number to the input and display the new time 19.2 Designing the Microwave Oven Application Figure 19.10 Typical numeric event handler.

  15. btnStart_Clickcreates an object to store time and begin cooking btnClear_Clickresets variables and Label DisplayTimeformats time information for display tmrClock_Clickperforms countdown and updates display 19.2 Designing the Microwave Oven Application Figure 19.11 Microwave Oven application’s remaining event handlers.

  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. SelectClassas new item Name of new class 19.3 Adding a New Class to the Project Figure 19.12 Add New Item dialog allows you to create a new class.

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

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

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

  21. 19.4 Initializing Class Objects: Constructors • Constructor • 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 • – Timem_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. Timeis the constructor method 19.4 Initializing Class Objects: Constructors Figure 19.16 Empty constructor.

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

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

  25. 19.5 Properties • Properties • 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. getaccessor retrieves data setaccessor stores data 19.5 Properties Figure 19.19 Empty Minute property. • The accessor methods are meant to keep the property in a consistent state (that is, valid)

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

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

  29. Property Second 19.5 Properties Figure 19.22 Second property.

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

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

  32. 19.6 Completing the Microwave Oven Application • Stringmethods • 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. Ensure m_strTime has four characters for conversion purposes 19.6 Completing the Microwave Oven Application Figure 19.25 Declaring variables for second and minute values.

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

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

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

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

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

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

  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. Clearing the input 19.6 Completing the Microwave Oven Application Figure 19.32 Clearing the Microwave Oven 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. ModifyTimeappropriately during countdown 19.6 Completing the Microwave Oven Application Figure 19.35 Modifying the display 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)

More Related