Scripting Languages CS 5142, Fall 2013 hw02
- Dates:
Assigned Thursday 8/6/2013, due Friday 6/13/2013 at 6pm.
- Topic:
Objects, properties, call-backs (VBA)
- Total points:
40
- Instructions:
Please put your solutions to VBA programming problems in a
PowerPoint presentation. Put your answers to all remaining questions
in a plain-text file, such as what you get when using Emacs, Vi,
Notepad, or the “save as text only” feature in Word.
Reading Assignments
- For lecture on 8/9/2013:
Kirrily “Skud” Robert. perlintro(1) man page. Perl 5 documentation.
Available on machines that have Perl properly installed, and also
available online at http://perldoc.perl.org/perlintro.html
Concept Questions
hw02_1 Call-backs
- 1a.
- (3 points) Give a short example for how in VBA forms,
call-backs use mangled names.
- 1b.
- (2 points) What is a disadvantage of the mangled-names
approach to call-backs?
- 1c.
- (3 points) Why are call-backs common in graphical user
interfaces (GUIs)?
- 1d.
- (2 points) Where else are call-backs common besides GUIs?
hw02_2 Properties
- 2a.
- (3 points) Consider the VBA class Vector with the
following code:
Public X As Double, Y As Double
Sub Class_Initialize()
X = 0: Y = 0
End Sub
Public Property Get Length() As Double
Length = Sqr(X ^ 2 + Y ^ 2)
End Property
Public Property Let Length(NewLen As Double)
OldLen = Length
If OldLen = 0 Then
X = NewLen
Y = 0
Else
X = X * NewLen / OldLen
Y = Y * NewLen / OldLen
End If
End Property
Give a short example for how writing the Length property affects
the X and Y properties.
- 2b.
- (4 points) If VBA would support only passive attributes, no
active properties, how would you implement the Vector class?
- 2c.
- (3 points) What is an advantage of the version with properties?
Programming exercises
hw02_3 Priority queue class
(10 points) Write a VBA class that implements a priority queue of
Doubles. Your class should have two methods: Insert(key
As Double) and ExtractMax() As Double. Your
implementation should be robust with respect to queue size, growing
the underlying data structures if necessary. Your code does not need
to be efficient, so you do not need to find the fastest possible
algorithm to solve this. Consider the following driver routine:
Sub Main_hw02_3()
Dim Q As PriorityQueue
Set Q = New PriorityQueue
Q.Insert 3.1
Q.Insert 4.2
Q.Insert 0.5
Q.Insert 1.9
Debug.Print Q.ExtractMax
Debug.Print Q.ExtractMax
Debug.Print Q.ExtractMax
Debug.Print Q.ExtractMax
End Sub
This should print 4.2, 3.1, 1.9, and 0.5, in other words, it should
output the previously-inserted numbers in descending order by value.
hw02_4 Temperature conversion user form
(10 points) Implement a VBA user form that converts temperatures from
Celsius to Fahrenheit and vice versa. Your form should look like this:
When the user enters a value in the “Celsius” field and
presses the “To Fahrenheit” button, that should trigger a
VBA call-back that shows the converted temperature in the
“Fahrenheit” field, and vice versa. To solve this
question, you will need to create a user form, and edit both the
visual portion (with the controls) and the code sheet (with the
call-backs). You can use the following driver to display the form:
Sub Main_hw02_3()
Load frmTempConvert
frmTempConvert.Show
Unload frmTempConvert
End Sub
http://www.cs.cornell.edu/Courses/cs5142/2013fa/