92
Tutorial 19
Microwave
Oven
A
pplica
t
io
n
411
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
the application, and the other to represent the microwave oven’s glass window. The
template application provided for you contains one of these
Panel
s.
The Microwave Oven application contains a class (called
Time
) whose objects
store the cook time in minutes and seconds. All the controls you have used (includ-
ing the
Form
itself) are defined as classes. You’ll create the
Time
class before you
create the class for the Microwave Oven. The following pseudocode describes the
basic operation of class
Time
:
When the time object is created:
Assign input to variables for number of minutes and number of seconds
When setting the number of minutes:
If the number of minutes is less than 60
Set the number of minutes to specified value
Else
Set the number of minutes to 0
When setting the number of seconds:
If the number of seconds is less than 60
Set the number of seconds to specified value
Else
Set the number of seconds to 0
When an object of class
Time
is created, the number of minutes and number of
seconds are initialized. Invalid data for the number of minutes or the number of
seconds is set to 0. The following pseudocode describes the basic operation of your
Microwave Oven class:
When the user clicks a numeric Button:
Sound beep
Display the formatted time
When the user clicks the Start Button:
Store the minutes and seconds
Display the formatted time
Begin countdown—Start timer
Turn the microwave light on
When the timer ticks (once per second):
Decrease time by one second
Display new time
If new time is zero
Stop the countdown
Sound beep
Display text “Done!”
Turn the microwave light off
When the user clicks the Clear Button:
Display the text “Microwave Oven”
Clear input and time data
Stop the countdown
Turn the microwave light off
The user enters input by clicking the numeric
Button
s. Each time a numeric
Button
is clicked, the number on that
Button
is appended to the end of the cook
time displayed in the GUI’s
Label
. At most, four digits can be displayed. After
entering the cook time, the user can click the Start
Button
to begin the cooking
process or click the Clear
Button
and enter a new time. Each
Button
makes a
beeping sound when clicked. If the Start
Button
is clicked, a countdown using a
Timer
control begins, and the microwave oven’s window changes to yellow, indicat-
ing that the oven’s light is on (so that the user can watch the food cook). Each sec-
185
412
Bu
ildi
n
g
Y
o
ur Own C
la
ss
e
s
a
n
d
O
bjec
ts
Tutorial 19
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
ond, the display is updated to show the remaining cooking time. When the
countdown finishes, another beep is sounded, the display
Label
displays the text
Done! and the microwave oven’s light is turned off by changing the window’s color
back to its default gray.
Now that you’ve test-driven the Microwave Oven application and studied its
pseudocode representation, you’ll use an ACE table to help you convert the
pseudocode to Visual Basic. Figure 19.7 lists the actions, controls and events that
will help you complete your own version of this application.
A
c
ti
o
n
/
C
o
ntr
o
l
/
Ev
e
nt
(ACE)
Tab
l
e
f
o
r th
e
Microwave
Oven
A
pp
li
ca
ti
o
n
Input is sent to the application when the user clicks one of the numeric
But-
ton
s. Values are displayed in
displayLabel
as they are entered. Once all input has
been entered, the user clicks the Start
Button
to begin the countdown. The
Form
’s
windowPanel
background color is set to yellow to simulate the microwave oven’s
light being turned on, and
clockTimer
updates
displayLabel
each second during
the countdown. To clear the input and start over, the user can click the Clear
But-
ton
. In the following box, you begin creating your Microwave Oven application by
adding the second
Panel
to the
Form
and viewing the template code.
A
c
ti
o
n
C
o
ntr
o
l
/
O
b
j
ec
t
Ev
e
nt
oneButton
,
twoButton
,
three-
Button
,
fourButton
,
fiveBut-
ton
,
sixButton
,
sevenButton
,
eightButton
,
nineButton
,
zeroButton
Click
Sound beep
Display the formatted time
displayLabel
startButton
Click
Store the minutes and seconds
timeObject
Display the formatted time
displayLabel
Begin countdown—Start timer
clockTimer
Turn microwave light on
windowPanel
clockTimer
Tick
Decrease time by one second
timeObject
Display new time
displayLabel
If new time is zero
timeObject
Stop the countdown
clockTimer
Sound beep
Display text “Done!”
displayLabel
Turn the microwave light off
windowPanel
clearButton
Click
Display the text “Microwave Oven”
displayLabel
Clear input and time data
timeIs
,
timeObject
Stop the countdown
clockTimer
Turn microwave light off
windowPanel
Figure 19.7
ACE t
able
f
o
r th
e
Microwave
Oven
applica
t
io
n.
418
Tutorial 19
Microwave
Oven
A
pplica
t
io
n
413
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
A
dd
in
g
a
Panel
C
o
ntr
o
l
t
o
th
e
Microwave
Oven
A
pp
li
ca
ti
o
n
1.
Copying the template to your working directory. Copy the
C:\Examples\
Tutorial19\TemplateApplication\MicrowaveOven
directory to your
C:\SimplyVB2008
directory.
2.
Opening the Microwave Oven application’s template file. Double click
MicrowaveOven.sln
in the
MicrowaveOven
directory to open the applica-
tion in the Visual Basic IDE.
3.
Adding a
Panel
to the
Form
. Add a
Panel
control to the
Form
by double
clicking the
Panel
control (
) in the Toolbox. Name the control
windowPanel
because this
Panel
represents your microwave oven’s win-
dow. Set the
Panel
’s
Size
property to
328,
224
and its
Location
property
to
14,
16
. Set the
BorderStyle
property to
FixedSingle
, to display a thin
black rectangle surrounding your
Panel
.
4.
Viewing the template code. Before you add code to this application, switch to
code view, and examine the code provided. Line 4 of Fig.19.8 declares
instance variable
timeIs
, a
String
that will store user input.
Figure 19.8
V
a
r
iable
timeIs
co
nt
ai
ns th
e
us
e
r’s
i
n
p
ut.
The template code also contains event handlers for the numeric
Button
s’
Click
events. Each
Button
is clicked when the user wants to append the
current
Button
’s digit to the amount of cooking time. Let’s look at one of
these event handlers closely (Fig.19.9). Line 10 calls function
Beep
, which
causes your computer to make a beeping sound. Each event handler for the
numeric keypad
Button
s begins with a call to
Beep
, appends the current
Button
’s number to
timeIs
(line 11) and calls method
DisplayTime
(line
12), which displays the current cooking time in the application’s
Label
.
There are 10 of these event handlers—one for each digit from 0 to 9.
MicrowaveOven.vb
contains four more methods that you define in this
tutorial. The first is the
startButton_Click
event handler in lines 96–100
of Fig. 19.10. This event handler starts the microwave oven’s cooking pro-
cess, which in this simulation consists of a time countdown and changing the
window’s color to yellow, simulating the oven’s light being on.
Event handler
clearButton_Click
(lines 102–106) clears the time
entered. The Clear
Button
is used to change the time entered or terminate
cooking early. The event handler resets the time to all zeros and displays the
text Microwave Oven. Method
DisplayTime
(lines 108–111) displays the
cooking time as it’s being entered. Event handler
clockTimer_Tick
(lines
113–117) changes the application’s
Label
during the countdown.
5.
Saving the project. Select File > Save All to save your modified code.
GUI Design Tip
A
l
th
o
u
g
h
i
t
i
s
po
ss
ible
t
o
h
a
v
e
a
Panel
w
i
th
o
ut
a
bo
r
de
r (
b
y s
e
tt
i
n
g
th
e
BorderStyle
p
r
ope
rty t
o
None
),
us
e
bo
r
de
rs
o
n y
o
ur
Panel
s t
o
i
m
p
r
o
v
e
us
e
r
i
nt
e
rf
ace
r
eadabili
ty
a
n
d
o
r
ga
n
i
z
a
t
io
n.
Good Programming
Practice
Us
e
th
e
Pane
l
suff
i
x wh
e
n n
a
m
i
n
g
pa
n
el
s.
GUI Design Tip
A
Panel
ca
n
di
s
pla
y s
c
r
ollba
rs wh
e
n
th
e
i
t
i
s n
o
t
la
r
ge
e
n
o
u
g
h t
o
di
s
pla
y
all
o
f
i
ts
co
ntr
ol
s.
To
i
n
c
r
ea
s
e
us
abil
-
i
ry, w
e
su
gge
st
a
v
oidi
n
g
th
e
us
e
o
f
s
c
r
ollba
rs
o
n
Panel
s.
I
f
a
Panel
i
s n
o
t
la
r
ge
e
n
o
u
g
h t
o
di
s
pla
y
all
o
f
i
ts
co
nt
e
nts,
i
n
c
r
ea
s
e
th
e
s
i
z
e
o
f th
e
Panel
.
262
414
Bu
ildi
n
g
Y
o
ur Own C
la
ss
e
s
a
n
d
O
bjec
ts
Tutorial 19
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
SE
L
F
-
REVIEW
1.
A
Panel
is different from a
GroupBox
in that a
.
2.
Function
Beep
causes the computer to
.
Answers: 1)
c.
2)
b.
19.3 A
dd
in
g
a
N
e
w Cl
a
ss t
o
th
e
Pr
o
j
ec
t
Next, you learn how to add a class to your application. This class is used to create
objects that contain the time in minutes and seconds.
Figure 19.9
T
y
pical
num
e
r
ic
e
v
e
nt h
a
n
dle
r.
Figure 19.10 0 Microwave
Oven
applica
t
io
n’s r
e
m
ai
n
i
n
g
e
v
e
nt h
a
n
dle
rs.
(cont.)
Wh
e
n
a
num
be
r
i
s
e
nt
e
r
ed
,
pla
y
a
beep
,
appe
n
d
th
e
num
be
r t
o
th
e
timeIs
a
n
d
di
s
pla
y th
e
n
e
w t
i
m
e
startButton_Click
c
r
ea
t
e
s
a
n
objec
t t
o
st
o
r
e
th
e
t
i
m
e
a
n
d
begi
n
coo
k
i
n
g
clearButton_Click
r
e
s
e
ts
v
a
r
iable
s
a
n
d
Label
DisplayTime
f
o
rm
a
ts t
i
m
e
i
nf
o
rm
a
t
io
n f
o
r
di
s
pla
y
clockTimer_Click
pe
rf
o
rms
co
unt
do
wn
a
n
d
u
pda
t
e
s
di
s
pla
y
a)
GroupBox
can be used to organize controls, whereas a
Panel
cannot
b)
Panel
contains a caption, whereas a
GroupBox
does not
c)
GroupBox
contains a caption, whereas a
Panel
does not
d)
Panel
can be used to organize controls, whereas a
GroupBox
cannot
a) make three beeping sounds in sequence
b) make a beeping sound
c) display a message dialog and make a beeping sound
d) set off the system alarm and pause the application
276
Tutorial 19
Microwave
Oven
A
pplica
t
io
n
415
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
A
dd
in
g
a
Cl
a
ss t
o
th
e
Microwave
Oven
A
pp
li
ca
ti
o
n
1.
Adding a new class to the project. Select Project >Add Class. In the dialog
that appears (Fig. 19.11), enter the class name (
Time
) in the Name: field and
click Add. Note that the class name (ending with the
.vb
file extension)
appears in the Solution Explorer below the project name (Fig. 19.12).
[
Figure 19.11 1 Add
New
Item
dialog
allo
ws y
o
u t
o
c
r
ea
t
e
a
n
e
w
cla
ss.
Figure 19.12 2 Solution
Explorer
di
s
pla
y
i
n
g
th
e
n
e
w
cla
ss f
ile
.
2.
Viewing the code that has been added to this class. If
Time.vb
does not
open for you when it is created, double click the file in the Solution
Explorer. Note that a few lines of code have been added for you (Fig. 19.13).
Line 1, which begins the
Time
class definition, contains the keywords
Public
and
Class
, followed by the name of the class (in this case,
Time
). Keyword
Class
indicates that what follows is a class definition. You’ll learn about
keyword
Public
in Section 19.7. The keywords
End
Class
(line 3) indicate
the end of the class definition. Any code placed between these two lines
forms the class definition’s body. Any methods or variables defined in the
body of a class are considered to be members of that class.
3.
Adding instance variables to your application. Add lines 1–2 of Fig.19.14
to
Time.vb
, above the class definition. Always add comments indicating the
name and purpose of your class files. Add lines 6–8 to the
Time
class defini-
tion.
Lines 7–8 declare each of the two
Integer
instance variables—
minute-
Value
and
secondValue
. The
Time
class stores a time value containing min-
utes and seconds—the value for minutes is stored in
minuteValue
, and the
value for seconds is stored in
secondValue
. Finally, be sure to add a com-
ment in line 10 where the class definition is terminated.
S
elec
t
Class
a
s n
e
w
i
t
e
m
N
a
m
e
o
f n
e
w
cla
ss
N
e
w f
ile
di
s
pla
y
ed
i
n
Solution
Explorer
N
e
w f
ile
di
s
pla
y
ed
i
n
Solution
Explorer
Good Programming
Practice
A
dd
co
mm
e
nts
a
t th
e
begi
nn
i
n
g
o
f
y
o
ur
cla
ss
e
s t
o
i
n
c
r
ea
s
e
r
eadabili
ty.
T
h
e
co
mm
e
nts sh
o
u
ld
i
n
dica
t
e
th
e
n
a
m
e
o
f th
e
f
ile
th
a
t
co
nt
ai
ns th
e
cla
ss
a
n
d
th
e
p
ur
po
s
e
o
f th
e
cla
ss
bei
n
g
de
f
i
n
ed
.
195
416
Bu
ildi
n
g
Y
o
ur Own C
la
ss
e
s
a
n
d
O
bjec
ts
Tutorial 19
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
SE
L
F
-
REVIEW
1.
To add a class to a project in the Visual Basic IDE, select
.
2.
A class definition ends with the keyword(s)
.
Answers: 1)
c.
2)
b.
19.4 Initi
a
lizin
g
Cl
a
ss O
b
j
ec
ts: C
o
nstru
c
t
o
rs
A class can contain methods as well as instance variables. You have already used
method
Format
from class
String
and method
Next
from class
Random
. A con-
structor is a special method within a class definition that is used to initialize a class’s
instance variables. In the following box, you create a constructor for your
Time
class
that allows clients to create
Time
objects and initialize their data.
D
e
finin
g
a
C
o
nstru
c
t
o
r
Figure 19.13
Em
p
ty
cla
ss
de
f
i
n
i
t
io
n.
Figure 19.14 4 Time
’s
i
nst
a
n
ce
v
a
r
iable
s.
4.
Saving the project. Select File > Save All to save your modified code.
(cont.)
Em
p
ty
cla
ss
de
f
i
n
i
t
io
n
added
b
y th
e
I
DE
I
nst
a
n
ce
v
a
r
iable
s
st
o
r
e
m
i
nut
e
a
n
d
s
eco
n
d
i
nf
o
rm
a
t
io
n
a) File > Add Class
b) File > Add File > Add Class
c) Project > Add Class
d) Project > Add File > Add Class
a)
Class
End
b)
End
Class
c)
EndClass
d)
End
1.
Adding a constructor to a class. Add lines 10–11 of Fig. 19.15 to the body of
class
Time
, then press Enter. The keywords
End
Sub
are added for you, just
as with the other
Sub
procedures you’ve created in this text.
New
is the constructor method. You write code for the constructor that is
invoked whenever an object of that class is instantiated (created). This con-
structor method then performs the actions in its body, which you add in the
next few steps. A constructor’s actions consist mainly of statements that ini-
tialize the class’s instance variables.
303
Tutorial 19
Microwave
Oven
A
pplica
t
io
n
417
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
Figure 19.15
Em
p
ty
co
nstru
c
t
o
r.
Constructors can take arguments (you’ll see how to provide arguments
to constructors momentarily) but cannot return values. An important dif-
ference between constructors and other methods is that constructors can-
not specify a return data type—for this reason, Visual Basic constructors
are implemented as
Sub
procedures rather than
Function
procedures,
because
Sub
procedures cannot return values. A class’s instance variables
can be initialized in the constructor or when they are defined in the class
definition. Variable
secondValue
, for instance, can be initialized where it is
declared (line 8) or it can be initialized in
Time
’s constructor.
2.
Initializing variables in a constructor. Add lines 13–14 of Fig. 19.16 to the
constructor. These lines initialize
Time
’s instance variables to the values of
the constructor’s parameter variables (line 11 of Fig.19.15). When client of
a class creates an object of that class, values are often specified for that
object. A
Time
object can now be created with the statement
timeObject = New Time(5, 3)
This
Time
object is created and the constructor executes. The values
5
and
3
are assigned to the constructor’s parameters, which are used to initialize
secondValue
and
minuteValue
.
Figure 19.16
C
o
nstru
c
t
o
r
i
n
i
t
iali
z
i
n
g
i
nst
a
n
ce
v
a
r
iable
s.
3.
Creating a
Time
object. After defining the class, you can use it as a type
(just as you would use
Integer
or
Double
) in declarations. View
MicrowaveOven.vb
by selecting the MicrowaveOven.vb tab above the
code editor. Add lines 6–7 of Fig.19.17 to your application. Note the use of
the class name,
Time
, as a type. Just as you can create many variables from
a data type, such as
Integer
, you can create many objects from class types.
You can create your own class types as needed; this is one reason why
Visual Basic is known as an extensible language—the language can be
“extended” with new data types. Note that, after you type
As
in line 7,
IntelliSense displays a window of available types. Your
Time
class is dis-
played in the IntelliSense window (Fig.19.18).
New
i
s th
e
co
nstru
c
t
o
r m
e
th
od
(cont.)
Common
Programming Error
Att
e
m
p
t
i
n
g
t
o
decla
r
e
a
co
nstru
c
t
o
r
a
s
a
Function
p
r
oced
ur
e
i
nst
ead
o
f
a
s
a
Sub
p
r
oced
ur
e
a
n
d
a
tt
e
m
p
t
i
n
g
t
o
Return
a
v
al
u
e
fr
o
m
a
co
nstru
c
-
t
o
r
a
r
e
bo
th synt
a
x
e
rr
o
rs.
Error-Prevention Tip
Pr
o
v
idi
n
g
a
co
nstru
c
t
o
r t
o
e
nsur
e
th
a
t
e
v
e
ry
objec
t
i
s
i
n
i
t
iali
z
ed
w
i
th
m
ea
n
i
n
g
fu
l
v
al
u
e
s
ca
n h
elp
eli
m
i
-
n
a
t
e
logic
e
rr
o
rs.
I
n
i
t
iali
z
e
i
nst
a
n
ce
v
a
r
iable
s
169
418
Bu
ildi
n
g
Y
o
ur Own C
la
ss
e
s
a
n
d
O
bjec
ts
Tutorial 19
simplyvb2008_19.fm
February 2, 2008 4:32 pm
© 2008 by Deitel & Associates, Inc. All Rights Reserved.
SE
L
F
-
REVIEW
1.
A(n)
language is one that can be “extended” with new data types.
2.
Variables can be initialized
.
Answers:
1)
b.
2)
d.
19.5 Pr
ope
rti
e
s
Clients of a class usually want to manipulate that class’s instance variables. For
example, assume a class (
Person
) that stores information about a person, including
age information (stored in
Integer
instance variable
age
). Clients who create an
object of class
Person
might want to modify
age
—perhaps incorrectly, by assigning
a negative value to
age
, for example. Classes often provide properties to allow cli-
ents to access and modify instance variables safely. The syntax used to access prop-
erties is the same as the syntax used to access instance variables. You’ve already
seen and used several properties in previous tutorials. For instance, many GUI con-
trols contain a
Text
property, used to retrieve or modify the text displayed by a
control. When a value is assigned to a property, the code in the property definition
is executed. The code in the property typically checks the value to be assigned and
rejects invalid data. In this tutorial, you learn how to create your own properties to
help clients of a class read and modify the class’s instance variables. You create two
properties,
Minute
and
Second
, for your
Time
class.
Minute
allows clients to access
Figure 19.17
D
ecla
r
i
n
g
a
n
objec
t
o
f ty
pe
Time
.
Figure 19.18 8 Time
appea
r
i
n
g
a
s
a
ty
pe
i
n
a
n
I
nt
elli
S
e
ns
e
w
i
n
do
w.
4.
Saving the project. Select File > Save All to save your modified code.
(cont.)
D
ecla
r
e
timeObject
o
f
p
r
og
r
a
mm
e
r-
de
f
i
n
ed
ty
pe
Time
Time
appea
rs
a
s
a
ty
pe
i
n th
e
I
DE
a) data
b) extensible
c) typeable
d) extended
a) when they are declared
b) to their default values
c) in a constructor
d) All of the above.
Documents you may be interested
Documents you may be interested