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

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Advertisements

Chapter 1: An Introduction to Visual Basic 2012
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
Summary of text to be covered Create your own class Create and use objects of your own class Control access to object instance variables Use keyword private.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 15 – Digital Clock Application: Building Your.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Chapter 8: String Manipulation
Programming with Microsoft Visual Basic th Edition
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Teacher: Ms. Olifer MICROSOFT VISUAL STUDIO 2010: PROPERTIES OF WINDOWS FORM OBJECT.
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
Microsoft Visual C++.NET Chapter 51 Introduction to Classes.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Microwave Oven Application Building Your Own Classes and Objects.
Applications Development
Chapter 4 Introduction to Classes, Objects, Methods and strings
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Programming with Microsoft Visual Basic th Edition
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Chapter 27 Getting “Web-ified” (Web Applications) Clearly Visual Basic: Programming with Visual Basic nd Edition.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Object-Oriented Application Development Using VB.NET 1 Chapter 11 Using Multiple Forms with Problem Domain Classes.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
14 Shipping Time App Using Dates and Timers
Tutorial 14 – Shipping Time Application Using DateTimes and Timers
Creating Your Own Classes
3 Introduction to Classes and Objects.
Chapter 1: An Introduction to Visual Basic 2015
Object-Oriented Programming: Classes and Objects
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
Multiple Classes and Inheritance
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
Variables and Arithmetic Operations
Tutorial 12 – Security Panel Application Introducing the Select Case Multiple-Selection Statement Outline Test-Driving the Security Panel Application.
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Additional Topics in VB.NET
Object-Oriented Programming: Inheritance and Polymorphism
Classes and Objects Systems Programming.
Presentation transcript:

Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline 19.1 Test-Driving the Microwave Oven Application 19.2 Designing the Microwave Oven Application 19.3 Adding a New Class to the Project 19.4 Initializing Class Objects: Constructors 19.5 Properties 19.6 Completing the Microwave Oven Application 19.7 Controlling Access to Members 19.8 Using the Debugger: The Autos and Locals Windows 19.9 Wrap-Up

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.

19.1 Test-Driving the Microwave Oven Application

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

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 Application Figure 19.3 Microwave Oven application with invalid input.

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.

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

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

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 Application

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

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

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

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

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

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.3 Adding a New Class to the Project Figure 19.14 Default class declaration. Empty class definition added by Visual Studio .NET

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

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

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

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

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

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

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

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

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

19.5 Properties Figure 19.22 Second property. Property Second

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

19.5 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

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

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

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

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

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

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

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

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

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 Application Figure 19.32 Clearing the Microwave Oven input. Clearing the input

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 Application Figure 19.33 Modifying invalid user input.

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

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

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 Members Figure 19.36 Time’s instance variables are private.

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

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

MicrowaveOven.cs (1 of 11)

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

MicrowaveOven.cs (3 of 11)

MicrowaveOven.cs (4 of 11)

MicrowaveOven.cs (5 of 11)

MicrowaveOven.cs (6 of 11)

MicrowaveOven.cs (7 of 11)

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

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

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

MicrowaveOven.cs (11 of 11)

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

Time.cs (2 of 4)

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

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 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 Windows Figure 19.41 Microwave Oven application with breakpoints added.

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 Windows Figure 19.44 Autos window displaying the state of m_objTime. Properties of m_objTime Property values Property types

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

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.

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

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