CPS 130: Solutions to Homework #8
The User Interface, Forms & Event Procedures, and Output Files
Do this homework with your partner.
Name Section ______
Name Section ______
Staple this page to your homework as the cover page.
If the grader has any difficulty reading your homework, you will lose points.
If you have any doubts about the readability of your handwriting, please use a word processor.
CPS 130, Fall 1996, Homework #8, Page 1
1.Tracing (3 points)
Consider the following Visual Basic code.
‘Main
OPEN “A:TESTING.DAT” FOR OUTPUT AS #1
OPEN “A:TESTING2.DAT” FOR OUTPUT AS #2
LET StringValue$ = “Be happy”
LET Counter = 0
DO WHILE (Counter < 8)
SELECT CASE Counter
CASE IS > 6
WRITE #2, Counter
WRITE #1, StringValue$
LET Counter = Counter - 2
LET StringValue$ = “Don’t worry”
CASE ELSE
WRITE #1, Counter, StringValue$
WRITE #2, StringValue$
LET Counter = Counter + 3
LET StringValue$ = “Be happy”
END SELECT
LOOP
CLOSE #1
CLOSE #2
END
What will be the contents of the files A:TESTING.DAT and A:TESTING2.DAT after the program is executed?
TESTING.DATTESTING2.DAT
0, “Be happy” “Be happy”
3, “Be happy” “Be happy”
6, “Be happy” “Be happy”
2.Legalities (2 points)
State whether each of the following assignment statements is legal or illegal. In each case, provide a brief (absolutely no more than two sentences) explanation of your answer.
a) LET MyForm.NameBox.text$ = “My Name”
This assignment statement is ILLEGAL, the text property shouldn’t be followed
by a dollar sign.
b) LET NameBox.text = YourForm.caption
This assignment statement is LEGAL (a string is being assigned to a string location, in this case a text property), aside from the fact that the form name
may be needed on the left side to clarify which NameBox is being referred to.
If a form name is not given, Visual Basic will assume the current form has
the NameBox control.
c) LET Cash$ = 59.99
This assignment statement is ILLEGAL, a numeric constant is being assigned
to a string variable.
d) LET “My String” = MyVariable
This assignment statement is ILLEGAL, constants are not permitted on the
left side of a LET statement, and it makes no sense to assign the value of a variable
to a constant, since a constant cannot change its value during execution of a program.
e) LET Sandwichbox.text$ = “Ham and Cheese”
This assignment statement is ILLEGAL, again, the dollar sign is not needed
after the text property.
f) LET Counter$ = Counter$ + 1
This assignment statement is ILLEGAL, a numeric constant of 1 is trying to
be added to a string variable and assigned back to a string variable. This is clearly
a problem of matching data types.
3.Please help us and vote for CPS 130! :) (4 points)
MSU officials are interested in knowing the most popular course among the students. They have given you the Form on the next page for this purpose and would like you to write event procedures for the form. A student should be able to enter his/her name, student number, and the course number he/she would like to vote for. The user should have the option to save the contents of the Form to a file, clear the Form so that all the fields are blank, and be able to quit the Form.
When the student wishes to save the information to the file, the data should be saved in a file called "U:STUDATA.DAT". (Psst.. How will you save data in the file so that the previous contents of the file are not destroyed?)
a) Write the event procedure for saving data in the file. (2 points)
SUB SaveButton_Click ( )
‘Note that we open the file for APPEND, so that the previous data
‘will be retained in the file, and the new data will be written to the end
‘of the file
OPEN “U:STUDATA.DAT” FOR APPEND AS #63
‘Write the name, student number, and course number from the text properties
‘of the text boxes out to the file
WRITE #63, VotingForm.NameBox.text
WRITE #63, VotingForm.PIDBox.text
WRITE #63, VotingForm.CourseVoteBox.text
‘The following three lines are not required for the saving process, but they
‘make the form more user-friendly by clearing the text boxes after the saving
‘process is complete.
LET VotingForm.NameBox.text = “”
LET VotingForm.PIDBox.text = “”
LET VotingForm.CourseVoteBox.text = “”
END SUB
b) Write the event procedure for clearing the Form. (1 point).
SUB ClearButton_Click ( )
‘All we need to do to clear the form is put empty strings in each of the
‘text boxes.
LET VotingForm.NameBox.text = “”
LET VotingForm.PIDBox.text = “”
LET VotingForm.CourseVoteBox.text = “”
END SUB
c) Write the event procedure for quitting the Form. (1 point)
SUB QuitButton_Click ( )
UNLOAD VotingForm
END SUB
Note: Recall the format for event procedures. The following is a sample of a click event for a control named MyButton.
SUB MyButton_Click()
‘Any code for the MyButton_Click event procedure would be ‘entered here.
END SUB
Name: [A] PID: [B] Course Vote: [C]
[D] [E] [F]
CLEARSAVE QUIT
[G] [H] [I]
Control / Type / Property / ValueA / Label / Control Name / NameLabel
Caption / “Name:”
B / Label / Control Name / PIDLabel
Caption / “PID:”
C / Label / Control Name / CourseVoteLabel
Caption / “Course Vote:”
D / Text Box / Control Name / NameBox
Text / “”
E / Text Box / Control Name / PIDBox
Text / “”
F / Text Box / Control Name / CourseVoteBox
Text / “”
G / Command Button / Control Name / ClearButton
Caption / “CLEAR”
H / Command Button / Control Name / SaveButton
Caption / “SAVE”
I / Command Button / Control Name / QuitButton
Caption / “QUIT”
J / Form / FormName / VotingForm
Caption / “Most Popular Course Voting Form”
CPS 130, Fall 1996, Homework #8, Page 1