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

Similar documents
Tutorial 5 Completing the Inventory Application Introducing Programming

Tutorial 6 Enhancing the Inventory Application Introducing Variables, Memory Concepts and Arithmetic

Classes in C# namespace classtest { public class myclass { public myclass() { } } }

CSC 330 Object-Oriented Programming. Encapsulation

Inheriting Windows Forms with Visual C#.NET

User-Defined Controls

Flag Quiz Application

1. Windows Forms 2. Event-Handling Model 3. Basic Event Handling 4. Control Properties and Layout 5. Labels, TextBoxes and Buttons 6.

Polymorphism. Polymorphism. CSC 330 Object Oriented Programming. What is Polymorphism? Why polymorphism? Class-Object to Base-Class.

namespace Tst_Form { private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components;

This is the start of the server code

Web Services in.net (2)

CALCULATOR APPLICATION

You can call the project anything you like I will be calling this one project slide show.

ListBox. Class ListBoxTest. Allows users to add and remove items from ListBox Uses event handlers to add to, remove from, and clear list

Lampiran B. Program pengendali

Introduction to Programming Using Java (98-388)

Professional ASP.NET Web Services : Asynchronous Programming

In order to create your proxy classes, we have provided a WSDL file. This can be located at the following URL:

Arrays. Arrays: Declaration and Instantiation. Array: An Array of Simple Values

Web Services in.net (7)

#pragma comment(lib, "irrklang.lib") #include <windows.h> namespace SuperMetroidCraft {

An array can hold values of any type. The entire collection shares a single name

This is the empty form we will be working with in this game. Look under the properties window and find the following and change them.

Sub To Srt Converter. This is the source code of this program. It is made in C# with.net 2.0.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

and event handlers Murach's C# 2012, C6 2013, Mike Murach & Associates, Inc. Slide 1

CSIS 1624 CLASS TEST 6

Start Visual Studio, create a new project called Helicopter Game and press OK

Start Visual Studio, start a new Windows Form project under the C# language, name the project BalloonPop MooICT and click OK.

Chapter 4 Defining Classes I

Using the Xcode Debugger

Convertor Binar -> Zecimal Rosu Alin, Calculatoare, An2 Mod de Functionare: Am creat un program, in Windows Form Application, care converteste un

Web Services in.net (6) cont d

Main Game Code. //ok honestly im not sure, if i guess its a class ment for this page called methodtimer that //either uses the timer or set to timer..

Hands-On Lab (MBL04) Lab Manual Incorporating COM Objects into Your.NET Compact Framework 2.0 Application

We are going to use some graphics and found a nice little batman running GIF, off course you can use any image you want for the project.

Now find the button component in the tool box. [if toolbox isn't present click VIEW on the top and click toolbox]

Introduction to Microsoft.NET

Microsoft Visual Basic 2005: Reloaded

3 Welcome Application 41 Introduction to Visual Programming

Quick Guide for the ServoWorks.NET API 2010/7/13

IBSDK Quick Start Tutorial for C# 2010

Chapter 6 Dialogs. Creating a Dialog Style Form

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Operatii pop si push-stiva

Before You Begin 1 Graphing Application 1 Introducing Computers, the Internet and Visual Basic.NET

Subject to Change Drawing Application 1 Introducing Computers, the Internet and C#

Objectives. Introduce static keyword examine syntax describe common uses

PS2 Random Walk Simulator

Click on the empty form and apply the following options to the properties Windows.

Blank Form. Industrial Programming. Discussion. First Form Code. Lecture 8: C# GUI Development

Design-Time Integration

UNIT-3. Prepared by R.VINODINI 1

Form Properties Window

The Microsoft.NET Framework

Laboratorio di Ingegneria del Software

Laboratorio di Ingegneria del L-A

Create your own Meme Maker in C#

Event-based Programming

Chapter 12: How to Create and Use Classes

Short Notes of CS201

Instructions for Crossword Assignment CS130

Start Visual Studio and create a new windows form application under C# programming language. Call this project YouTube Alarm Clock.

Standard. Number of Correlations

The Open Core Interface SDK has to be installed on your development computer. The SDK can be downloaded at:

CS201 - Introduction to Programming Glossary By

And Even More and More C++ Fundamentals of Computer Science

2559 : Introduction to Visual Basic.NET Programming with Microsoft.NET

More Language Features and Windows Forms. Part I. Some Language Features. Inheritance. Inheritance. Inheritance. Inheritance.

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

More Language Features and Windows Forms

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

OGSI.NET UVa Grid Computing Group. OGSI.NET Developer Tutorial

CHAPTER 3. Writing Windows C# Programs. Objects in C#

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

BackgroundWorker Component Overview 1 Multithreading with the BackgroundWorker Component 3 Walkthrough Running an Operation in the Background 10 How

Conventions in this tutorial

Software Design and Analysis for Engineers

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Visual Studio.NET.NET Framework. Web Services Web Forms Windows Forms. Data and XML classes. Framework Base Classes. Common Language Runtime

XNA 4.0 RPG Tutorials. Part 11b. Game Editors

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

To start we will be using visual studio Start a new C# windows form application project and name it motivational quotes viewer

Objects and Classes Continued. Engineering 1D04, Teaching Session 10

C# 2008 and.net Programming for Electronic Engineers - Elektor - ISBN

Object Oriented Programming COP3330 / CGS5409

Avoiding KeyStrokes in Windows Applications using C#

CHAPTER 7 OBJECTS AND CLASSES

Object-Oriented Programming

EL-USB-RT API Guide V1.0

Using the Quinn-Curtis.Net Software (QCChart2D and QCRTGraph) with Managed C++ (MC++)

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

Variables. Data Types.

Final Exam Review April 18, 2018

C++ Important Questions with Answers

INFORMATICS LABORATORY WORK #4

JScript Reference. Contents

Transcription:

1 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

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.

19.1 Test-Driving the Microwave Oven Application 3 Application Requirements An electronics company is considering building microwave ovens. The company has asked you to develop an application that simulates a microwave oven. The oven will contain a keypad that allows the user to specify the microwave cook time, which is displayed for the user. Once a time is entered, the user clicks the Start Button to begin the cooking process. The microwave s glass window changes color (from gray to yellow) to simulate the oven s light that remains on while the food cooks, and a timer counts down one second at a time. Once the time expires, the color of the microwave s glass window returns to gray (indicating that the microwave s light is now off) and the microwave displays the text Done!. The user can click the Clear Button at any time to stop the microwave and enter a new time. The user should be able to enter a number of minutes no larger than 59 and a number of seconds no larger than 59; otherwise, the invalid cook time will be set to zero. A beep will be sounded whenever a numeric Button is clicked, and when the microwave oven has finished a countdown.

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

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

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

19.1 Test-Driving the Microwave Oven Application 7 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 8 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 9 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 10 Panel control Can group controls as do GroupBoxes Cannot display a caption

19.2 Designing the Microwave Oven Application 11 Action Control/Object Event bntone, btntwo, btnthree, btnfour, btnfive, btnsix, btnseven, btneight, btnnine, btnzero Click Display the formatted time Store the minutes and seconds Display the formatted time Begin countdown Start timer Turn microwave light on Decrease time by one second If new time is zero Stop the countdown Display text Done! Turn microwave light off else Display new time Display Text Microwave Oven Clear input Stop the countdown Turn microwave light off lbldisplay btnstart m_objtime lbldisplay tmrclock pnlwindow tmrclock m_objtime m_objtime tmrclock lbldisplay pnlwindow lbldisplay btnclear lbldisplay m_strtime tmrclock pnlwindow Click Tick Click Figure 19.7 ACE table for the Microwave Oven application.

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

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

19.2 Designing the Microwave Oven Application 14 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 15 Figure 19.11 btnstart_click creates an object to store time and begin cooking Microwave Oven application s remaining event handlers. 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

19.3 Adding a New Class to the Project 17 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 18 Figure 19.13 Solution Explorer displaying new class file. New file displayed in Solution Explorer

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

19.3 Adding a New Class to the Project 20 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 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 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 22 Figure 19.16 Empty constructor. Time is the constructor method

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

19.4 Initializing Class Objects: Constructors 24 Figure 19.18 Declaring an object of type Time. Declare m_objtime of programmerdefined type Time Instantiate (create) an object of type Time

25 Properties 19.5 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 Properties 26 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 27 Figure 19.20 get accessor definition. Returning data from a property

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

19.5 Properties 29 Figure 19.22 Second property. Property Second

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

19.5 Properties 31 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 32 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 33 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 34 Figure 19.26 Forming minute and second values from input. Extracting seconds and minutes

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

19.6 Completing the Microwave Oven Application 36 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 37 Figure 19.29 Displaying time information with separating colon. Display time information

19.6 Completing the Microwave Oven Application 38 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 39 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 40 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 41 Figure 19.32 Clearing the Microwave Oven input. Clearing the input

19.6 Completing the Microwave Oven Application 42 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 43 Figure 19.33 Modifying invalid user input.

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

19.6 Completing the Microwave Oven Application 45 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

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

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

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

1 using System; 2 using System.Drawing; 3 using System.Collections; 4 using System.ComponentModel; 5 using System.Windows.Forms; 6 using System.Data; 7 8 namespace MicrowaveOven 9 { 10 /// <summary> 11 /// Summary description for FrmMicrowaveOven. 12 /// </summary> 13 public class FrmMicrowaveOven : System.Windows.Forms.Form 14 { 15 // Panel for the microwave's window 16 private System.Windows.Forms.Panel pnlwindow; 17 18 // Panel for the microwave's buttons 19 private System.Windows.Forms.Panel pnlcontrol; 20 21 // Label and Timer for the microwave's timer display 22 private System.Windows.Forms.Label lbldisplay; 23 private System.Windows.Forms.Timer tmrclock; 24 Outline MicrowaveOven.cs (1 of 11) 50

25 // Buttons of the microwave 26 private System.Windows.Forms.Button btnone; 27 private System.Windows.Forms.Button btntwo; 28 private System.Windows.Forms.Button btnthree; 29 private System.Windows.Forms.Button btnfour; 30 private System.Windows.Forms.Button btnfive; 31 private System.Windows.Forms.Button btnsix; 32 private System.Windows.Forms.Button btnseven; 33 private System.Windows.Forms.Button btneight; 34 private System.Windows.Forms.Button btnnine; 35 private System.Windows.Forms.Button btnzero; 36 private System.Windows.Forms.Button btnstart; 37 private System.Windows.Forms.Button btnclear; 38 39 /// <summary> 40 /// Required designer variable. 41 /// </summary> 42 private System.ComponentModel.IContainer components; 43 44 // contains time entered as a string 45 private string m_strtime = ""; 46 47 // contains time entered 48 private Time m_objtime; 49 Declaring instance variable as private Creating an object of a programmerdefined type Outline MicrowaveOven.cs (2 of 11) 51

50 public FrmMicrowaveOven() 51 { 52 // 53 // Required for Windows Form Designer support 54 // 55 InitializeComponent(); 56 57 // 58 // TODO: Add any constructor code after InitializeComponent 59 // call 60 // 61 } 62 63 /// <summary> 64 /// Clean up any resources being used. 65 /// </summary> 66 protected override void Dispose( bool disposing ) 67 { 68 if( disposing ) 69 { 70 if (components!= null) 71 { 72 components.dispose(); 73 } 74 } 75 base.dispose( disposing ); Outline MicrowaveOven.cs (3 of 11) 52

76 } 77 78 // Windows Form Designer generated code 79 80 /// <summary> 81 /// The main entry point for the application. 82 /// </summary> 83 [STAThread] 84 static void Main() 85 { 86 Application.Run( new FrmMicrowaveOven() ); 87 } 88 89 // event handler appends 1 to time string 90 private void btnone_click( 91 object sender, System.EventArgs e ) 92 { 93 m_strtime += "1"; // append digit to time input 94 DisplayTime(); // display time input properly 95 96 } // end method btnone_click 97 Outline MicrowaveOven.cs (4 of 11) 53

98 // event handler appends 2 to time string 99 private void btntwo_click( 100 object sender, System.EventArgs e ) 101 { 102 m_strtime += "2"; // append digit to time input 103 DisplayTime(); // display time input properly 104 105 } // end method btntwo_click 106 107 // event handler appends 3 to time string 108 private void btnthree_click( 109 object sender, System.EventArgs e ) 110 { 111 m_strtime += "3"; // append digit to time input 112 DisplayTime(); // display time input properly 113 114 } // end method btnthree_click 115 116 // event handler appends 4 to time string 117 private void btnfour_click( 118 object sender, System.EventArgs e ) 119 { 120 m_strtime += "4"; // append digit to time input 121 DisplayTime(); // display time input properly 122 123 } // end method btnfour_click Outline MicrowaveOven.cs (5 of 11) 54

124 125 // event handler appends 5 to time string 126 private void btnfive_click( 127 object sender, System.EventArgs e ) 128 { 129 m_strtime += "5"; // append digit to time input 130 DisplayTime(); // display time input properly 131 132 } // end method btnfive_click 133 134 // event handler appends 6 to time string 135 private void btnsix_click( 136 object sender, System.EventArgs e ) 137 { 138 m_strtime += "6"; // append digit to time input 139 DisplayTime(); // display time input properly 140 141 } // end method btnsix_click 142 143 // event handler appends 7 to time string 144 private void btnseven_click( 145 object sender, System.EventArgs e ) 146 { 147 m_strtime += "7"; // append digit to time input 148 DisplayTime(); // display time input properly 149 150 } // end method btnseven_click Outline MicrowaveOven.cs (6 of 11) 55

151 152 // event handler appends 8 to time string 153 private void btneight_click( 154 object sender, System.EventArgs e ) 155 { 156 m_strtime += "8"; // append digit to time input 157 DisplayTime(); // display time input properly 158 159 } // end method btneight_click 160 161 // event handler appends 9 to time string 162 private void btnnine_click( 163 object sender, System.EventArgs e ) 164 { 165 m_strtime += "9"; // append digit to time input 166 DisplayTime(); // display time input properly 167 168 } // end method btnnine_click 169 170 // event handler appends 0 to time string 171 private void btnzero_click( 172 object sender, System.EventArgs e ) 173 { 174 m_strtime += "0"; // append digit to time input 175 DisplayTime(); // display time input properly Outline MicrowaveOven.cs (7 of 11) 56

176 177 } // end method btnzero_click 178 179 // event handler starts the microwave oven's cooking process 180 private void btnstart_click( 181 object sender, System.EventArgs e ) 182 { 183 int intsecond; 184 int intminute; 185 186 // ensure that m_strtime has 4 characters 187 m_strtime = m_strtime.padleft( 4, Convert.ToChar( "0" ) ); 188 189 // extract seconds and minutes 190 intsecond = Int32.Parse( m_strtime.substring( 2 ) ); 191 intminute = Int32.Parse( m_strtime.substring( 0, 2 ) ); 192 193 // create Time object to contain time entered by user 194 m_objtime = new Time( intminute, intsecond ); 195 196 lbldisplay.text = String.Format( "{0:D2}:{1:D2}", 197 m_objtime.minute, m_objtime.second ); 198 199 m_strtime = ""; // clear m_strtime for future input 200 Creating a new object of a programmerdefined type Accessing properties of a programmerdefined type Outline MicrowaveOven.cs (8 of 11) 57

201 tmrclock.enabled = true; // start timer 202 203 pnlwindow.backcolor = Color.Yellow; // turn "light" on 204 205 } // end method btnstart_click 206 207 // event handler to clear input 208 private void btnclear_click( 209 object sender, System.EventArgs e ) 210 { 211 // reset each property or variable to its initial setting 212 lbldisplay.text = "Microwave Oven"; 213 m_strtime = ""; 214 m_objtime = new Time( 0, 0 ); 215 tmrclock.enabled = false; 216 pnlwindow.backcolor = SystemColors.Control; 217 218 } // end method btnclear_click 219 220 // method to display formatted time in timer window 221 private void DisplayTime() 222 { 223 int intsecond; 224 int intminute; 225 Outline MicrowaveOven.cs (9 of 11) Use the BackColor property to change the Panel s color Use the SystemColors.Control property to restore the default background color to the Panel Declaring a method as private 58

226 string strdisplay; // string displays current input 227 228 // if too much input entered 229 if ( m_strtime.length > 4 ) 230 { 231 m_strtime = m_strtime.substring( 0, 4 ); 232 } 233 234 strdisplay = m_strtime.padleft( 4, Convert.ToChar( "0" ) ); 235 236 // extract seconds and minutes 237 intsecond = Int32.Parse( strdisplay.substring( 2 ) ); 238 intminute = Int32.Parse( strdisplay.substring( 0, 2 ) ); 239 240 // display number of minutes, ":" and number of seconds 241 lbldisplay.text = String.Format( "{0:D2}:{1:D2}", 242 intminute, intsecond ); 243 244 } // end method DisplayTime 245 246 // event handler displays new time each second 247 private void tmrclock_tick( 248 object sender, System.EventArgs e ) 249 { 250 // perform countdown, subtract one second The Length property returns number of characters in a string The Substring method returns a subset of characters in a string The PadLeft method appends characters to the beginning of a string Outline MicrowaveOven.cs (10 of 11) 59

251 if ( m_objtime.second > 0 ) 252 { 253 m_objtime.second--; 254 } 255 else if ( m_objtime.minute > 0 ) 256 { 257 m_objtime.minute--; // subtract one minute 258 m_objtime.second = 59; // reset seconds for new minute 259 } 260 else // no more seconds 261 { 262 tmrclock.enabled = false; // stop timer 263 lbldisplay.text = "Done!"; // tell user time is finished 264 pnlwindow.backcolor = SystemColors.Control; 265 return; 266 } 267 268 lbldisplay.text = String.Format( "{0:D2}:{1:D2}", 269 m_objtime.minute, m_objtime.second ); 270 271 } // end method tmrclock_tick 272 273 } // end class FrmMicrowaveOven 274 } Outline MicrowaveOven.cs (11 of 11) 60

1 // Time.cs 2 // Represents time data and contains properties. 3 4 using System; 5 6 namespace MicrowaveOven 7 { 8 /// <summary> 9 /// Summary description for Time. 10 /// </summary> 11 public class Time 12 { 13 // declare ints for minute and second 14 private int m_intminute; 15 private int m_intsecond; 16 17 // Time constructor, minute and second supplied 18 public Time( int minutevalue, int secondvalue ) 19 { 20 Minute = minutevalue; // invokes Minute set accessor 21 Second = secondvalue; // invokes Second set accessor 22 23 } // end constructor Time 24 Keyword class used to define a class Right brace ends constructor definition Constructor name must be the class name Time.cs (1 of 4) Assign data to properties rather than to instance variables directly Outline 61

25 // property Minute 26 public int Minute 27 { 28 // return m_intminute value 29 get 30 { 31 return m_intminute; 32 33 } // end of get accessor 34 35 // set m_intminute value 36 set 37 { 38 // if minute value entered is valid 39 if ( value < 60 ) 40 { 41 m_intminute = value; 42 } 43 else 44 { 45 m_intminute = 0; // set invalid input to 0 46 } 47 48 } // end of set accessor 49 50 } // end property Minute Time.cs (2 of 4) Outline 62

51 52 // property Second 53 public int Second 54 { 55 // return m_intsecond value 56 get 57 { 58 return m_intsecond; 59 60 } // end of get accessor 61 62 // set m_intsecond value 63 set 64 { 65 // if minute value entered is valid 66 if ( value < 60 ) 67 { 68 m_intsecond = value; 69 } 70 else 71 { 72 m_intsecond = 0; // set invalid input to 0 73 } 74 75 } // end of set accessor get accessor returns data set accessor modifies data Time.cs (3 of 4) Outline 63

76 77 } // end property Second 78 79 } // end class Time Right brace ends property 80 } definition Time.cs Right brace ends class (4 of 4) declaration Outline 64

19.8 Using the Debugger: The Autos and Locals Windows 65 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 66 Figure 19.41 Microwave Oven application with breakpoints added.

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

19.8 Using the Debugger: The Autos and Locals Windows 68 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 69 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 70 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 71 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 72 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