tutorial 19 microwave oven application building your own classes and objects n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects PowerPoint Presentation
Download Presentation
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects

Loading in 2 Seconds...

play fullscreen
1 / 72

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


  • 139 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects' - arlais


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
tutorial 19 microwave oven application building your own classes and objects
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

objectives
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.
19 1 test driving the microwave oven application1

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.

19 1 test driving the microwave oven application2
19.1 Test-Driving the Microwave Oven Application

Figure 19.2 Microwave Oven application accepts only four digits.

19 1 test driving the microwave oven application3
19.1 Test-Driving the Microwave Oven Application

Figure 19.3 Microwave Oven application with invalid input.

19 1 test driving the microwave oven application4
19.1 Test-Driving the Microwave Oven Application

Figure 19.4 Microwave Oven application after invalid input has been entered and the StartButton clicked.

19 1 test driving the microwave oven application5

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).

19 1 test driving the microwave oven application6

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.

19 2 designing the microwave oven application
19.2 Designing the Microwave Oven Application
  • Panel control
    • Can group controls as do GroupBoxes
    • Cannot display a caption
19 2 designing the microwave oven application2
19.2 Designing the Microwave Oven Application

Figure 19.8 Rearranging and commenting the new control declaration.

19 2 designing the microwave oven application3
19.2 Designing the Microwave Oven Application

Figure 19.9 Variable m_strTime contains the user’s input.

19 2 designing the microwave oven application5

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.

19 3 adding a new class to the project
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
19 3 adding a new class to the project1

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.

19 3 adding a new class to the project2

New file displayed inSolution Explorer

19.3 Adding a New Class to the Project

Figure 19.13 Solution Explorer displaying new class file.

19 3 adding a new class to the project4

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
19 4 initializing class objects constructors
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
19 4 initializing class objects constructors2

Initialize instance variables

19.4 Initializing Class Objects: Constructors

Figure 19.17 Constructor initializing instance variables.

19 4 initializing class objects constructors3

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
19 5 properties
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
19 5 properties1

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)
19 5 properties2

Returning data from a property

19.5 Properties

Figure 19.20 get accessor definition.

19 5 properties3

Properties used to validate data

19.5 Properties

Figure 19.21 set accessor definition.

19 5 properties4

Property Second

19.5 Properties

Figure 19.22 Second property.

19 5 properties6

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.

19 6 completing the microwave oven application
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
19 6 completing the microwave oven application2

Extracting secondsand minutes

19.6 Completing the Microwave Oven Application

Figure 19.26 Forming minute and second values from input.

19 6 completing the microwave oven application4

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.

19 6 completing the microwave oven application5

Display time information

19.6 Completing the Microwave Oven Application

Figure 19.29 Displaying time information with separating colon.

19 6 completing the microwave oven application6

Time’sproperties appear inIntellisense

19.6 Completing the Microwave Oven Application

Figure 19.30 Properties of a programmer-defined type also appear in Intellisense.

19 6 completing the microwave oven application7

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.

19 6 completing the microwave oven application8
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
19 6 completing the microwave oven application9

Clearing the input

19.6 Completing the Microwave Oven Application

Figure 19.32 Clearing the Microwave Oven input.

19 6 completing the microwave oven application10
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
19 6 completing the microwave oven application11
19.6 Completing the Microwave Oven Application

Figure 19.33 Modifying invalid user input.

19 6 completing the microwave oven application12
19.6 Completing the Microwave Oven Application

Figure 19.34 Display current input.

19 7 controlling access to members
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
19 7 controlling access to members1
19.7 Controlling Access to Members

Figure 19.36 Time’s instance variables are private.

19 7 controlling access to members2
19.7 Controlling Access to Members

Figure 19.37 FrmMicrowaveOven’s instance variables are private.

19 7 controlling access to members3
19.7 Controlling Access to Members

Figure 19.38  FrmMicrowaveOven’s methods are private.

microwaveoven cs 2 of 11

Declaring instance variable as private

Creating an object of a programmer-defined type

MicrowaveOven.cs(2 of 11)

microwaveoven cs 9 of 11

Use the BackColor property to change thePanel’scolor

Use the SystemColors.Control property to restore the default background color to the Panel

Declaring a method as private

MicrowaveOven.cs(9 of 11)

microwaveoven cs 10 of 11

The Length property returns number of characters in a string

TheSubstringmethod returns a subset of characters in astring

ThePadLeftmethod appends characters to the beginning of astring

MicrowaveOven.cs(10 of 11)

time cs 1 of 4

Constructor name must be the class name

Assign data to properties rather than to instance variables directly

Keywordclassused to define a class

Right brace ends constructor definition

Time.cs(1 of 4)

time cs 4 of 4

Right brace ends property definition

Right brace ends class declaration

Time.cs(4 of 4)

19 8 using the debugger the autos and locals windows
19.8 Using the Debugger: The Autos and Locals Windows
  • AutosandLocalswindows
    • 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
19 8 using the debugger the autos and locals windows1
19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.41 MicrowaveOven application with breakpoints added.

19 8 using the debugger the autos and locals windows2
19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.42 Empty Autos window.

Figure 19.43 Empty Locals window.

19 8 using the debugger the autos and locals windows3

Properties ofm_objTime

Property values

Property types

19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.44 Autos window displaying the state of m_objTime.

19 8 using the debugger the autos and locals windows4

Instance variables ofm_objTime

19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.45 Locals window displaying the state of m_objTime.

19 8 using the debugger the autos and locals windows5

Changed values shown in red

19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.46 Autos window displaying changed variables in red.

Figure 19.47 Locals window displaying changed variables in red.

19 8 using the debugger the autos and locals windows6

Value changed by user

19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.48 Changing the value of a variable in the Autos window.

  • Double clicking a value allows the client to change the value while application is running
19 8 using the debugger the autos and locals windows7

New variables shown inAutoswindow

19.8 Using the Debugger: The Autos and Locals Windows

Figure 19.49 New variables listed in the Autos window.

  • There are new variables in the Autos window because execution has reached a statement that uses different variables