|
CS 535 Object-Oriented Programming
Fall Semester, 2003
Assignment 2
|
|
|
   
Assignment Index
       
© 2003, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 15-Sep-03
|
|
Assignment
2
Due
September 18
1.
Remove the parentheses in the following expressions that are not needed. That
is remove all parentheses you can without changing the result of the expression.
a.
(3+5) raisedTo: ((3 squared) + 7)
b.
(5 factorial) between: (3 squared) and: (3 * 5 * 9)
2.
Assume we have the following class definitions and methods:
Class A
Superclass Object
Methods
value
^’1’
foo1
^self value
|
Class B
Superclass A
Methods
value
^’2’
|
Class C
Superclass B
Methods
value
^’3’
foo2
^super value
foo3
^self value
foo4
^self foo1
foo5
^super foo1
|
Class D
Superclass C
Methods
value
^’4’
|
Given
the following assignments
exampleC := C new.
exampleD := D new.
Explain
what happens with each of the following expressions:
exampleC foo1
exampleC foo2
exampleD foo2
exampleC foo3
exampleD foo3
exampleD foo4
exampleD foo5
3a
What protocol of the Number class contains the method raisedTo:
3b
Which subclasses of Number implement raisedTo:?
3c
Why does Number implement raisedTo: when subclasses override the method?
4
Create a method in the Number class called tax that returns the amount of sales
tax on the receiver. Use 0.0725 has the sales tax rate. Use the following code
to test your method. (For this problem just turn in your source code for the
method.)
| price |
price := (Dialog request: 'Please enter price' initialAnswer: '100') asNumber.
Transcript
clear;
show: 'price: ', price printString; cr;
show: 'tax: ', price tax printString; cr
5.
To see how scoping of different variables works, create a new class called
Bar
and
give it as many different variables of the same name as possible. (E.g., an
instance variable named “
bar”,
a shared variable named “
Bar”.)
Also, add a method of the same name as instance variable. Besides answering the questions below turn
in your source code for the class.
5a
How many different variables with the same name can you access in one method?
b.
Can you access an instance variable and a local variable with the same name in
one method? Explain what happens when you try this.
   
visitors since 08-Sep-03