Lab 1.1 – Rounding Off
Goals:
w Continue study of
112 textbook
w Write a simple
program
w Be able to round
off to designated decimal place
Outline:
w Read Chapter 2
w Do most of 112
labs 1.3 & 4.
w Write a piece of
code moving toward Proj. #1
__1. Read Chapter Two of the 112 textbook.
Answer these questions.
w What is the difference between abstraction and
generalization?
Is there any way
in which they are related?
w What is the relationship between structured
programming and
modular
design?
w What does juggling have to do with the management
of complexity?
w With respect to storage on a computer, what is the
relationship
between speed of
access and space?
w What are the 3 basic hardware components of a computer?
w When building a house, how the labor is divided is
dictated by the
logic of the
construction process. What does that
have to do with
computer
programming?
w What is the difference between execution flow and
data flow?
w What is a unit of execution?
w Using the Registrar’s Office as an example,
illustrate the two data
management
principles of program design.
w List 4 key computing (program development) skills.
__2. Do 112 labs 1.3 (steps 1
to 9) and 1.4. Answer these questions.
w What is the difference between a label, a command
button and a
textbox?
w What is the VB statement used to exit a program?
__3. Create the user
interface for your rounding off program.
w Open VB and save your .frm and
.vbp files in a newly created
directory. What name did you give to the files and the
directory?
w You will need to enter two values, a number to be
rounded off
and an integer
indicating to which decimal place to round.
What object
should you place on the form for this purpose?
w You will need to display the result of rounding
off. What object
should you place
on the form for this purpose?
w You may also want labels above your textboxes
declaring their
purpose. In a serviceable program that would be necessary. In this
case it will be
optional.
w And you will need a command button to initiate the
rounding off
process. Let’s call it cmdRound. What caption did you give it?
w Place all these objects onto the form.
__4. Write code for rounding.
w Paste the code below as code for the Click event of cmdRound.
Notice that the
names of the textboxes are assumed to be txtToRound
and txtPlace,
while the name of the label is assumed to be lblResult. If
you have chosen
different names, you will need to change the code to
match the names
you chose.
Dim rval As Double
Dim place As Integer
Dim rounded As Double
Dim addval As Double
rval = Val(txtToRound.Text)
place = Val(txtPlace.Text)
addval = 5 / 10 ^ (place + 1)
rounded = rval + addval
rounded = Int(rounded * 10 ^ place)
rounded = rounded / 10 ^ place
lblResult.Caption = Str(rounded)
w Test your program.
Try rounding off 4798.1288
to 1, 2, 3 and 4
decimal
places. Is it working correctly?
w Why do you think the data types were declared as Double? Would
the program work
just as well if they were of type Single? (You can
read about these
data types on the second page of Chapter Six of the
112
textbook. Or you can also look them up
in Online Help).