CS 313 - Homework 5 - Object-oriented programming
Part 1 due: Monday 3/16 in class
Part 2 due: Friday 3/20 in class
This homework consists of two programming problems in Smalltalk
and one in Python.
The first problem is due this coming Monday. It is fairly short,
and it will give you valuable experience in Smalltalk before the
midterm exam on Wednesday.
The other two problems are due Friday before break. You may work on
problems 2 and 3 only in groups of two, or by yourself.
As usual, on the first page of your printouts for either
problem, write your name(s), the names of the students with whom
you collaborated, and how much time it took you.
There is no electronic submission for either part of this homework.
Starting Smalltalk/X
Here's a quick review on how to start Smalltalk/X. First, edit the
file ".bashrc" in your home directory and add the line
export STX_LIBDIR=/home/share/smalltalkX/stx/projects/smalltalk
to the bottom of this file. Then, open a new terminal window to make
the change take effect. You only need to do this once.
You should do all your work in the same directory
(e.g. ~/cs313/smalltalk) since smalltalk save large image files. To
start Smalltalk/X, move your terminal window to the left bottom corner
of your screen, go to your smalltalk directory and type "smalltalk".
The first time you do this, accept the license conditions and close
the "Hello" window. Move the "Launcher" window to the top left, and
open a new Workspace window (from the Tools menu). Move the Workspace
window underneath the launcher, making sure you can still see your console
window below (that's where your print output will appear). In the
Launcher window, select "Exit Smalltalk" from the File menu, and
click on "Exit & Save". This saves the current image "st.img" in
your smalltalk directory. The next time you start smalltalk, you'll be
right where you left off!
Part 1 (due Monday 3/16)
-
Implement a new method primesUpTo for the class Integer
that returns an OrderedCollection of all prime numbers less or equal
to the receiver.
Sample output:
29 primesUpTo.
OrderedCollection(2 3 5 7 11 13 17 19 23 29)
Specific instructions:
- Implement the sieve of Eratosthenes as discussed in class.
- Use an object of class OrderedCollection to return the
list of primes. Use "OrderedCollection new" to create an
empty collection, and use the message "add: anObject" to add
new elements to the collection.
- Use an object of class Array to hold an array of
booleans that represent whether the number at the current index is
prime or not. Supposing that N is an integer, use "Array new:
N" to create an array with N elements (indexed from 1 to N).
Use the messages "at: i" to get the element at index
i, "at: i put: x" to set the element at index
i to x, and "atAllPut: x" to set all
elements to x.
For printing, cut and paste your method definition into a text file.
Don't forget to document your method, and to include sample
output (which you can cut and paste from a workspace window).
Also, don't forget to include your name, names of collaborators, and
how much time it took you.
Part 2 (due Friday 3/20 in class)
-
Implement a binary search tree in an object-oriented fashion in
Smalltalk. This is similar to Sethi, p. 296, problem 7.6, where you
can find some code to get started. However, your "Node" class should
also have an instance variable "data" (which will hold an integer),
and members of the "Tree" class should understand the messages
"insert:" and "member:" (just like in homeworks 2 and 3). "Tree"
objects should also understand the message "values", which should
return an OrderedCollection of all the values in the tree, collected
using an in-order traversal. Here is some sample output:
|t| t:= Tree new. t insert: 3. t insert: 1. t insert: 2.
t member: 1. -> true
t member: 4. -> false
t values. -> OrderedCollection(1 2 3)
Tips: In my sample solution, I use the following instance methods:
- for class Tree: "member: anInt", "insert: anInt", "values",
as well as "isEmpty" and "do: aBlock".
- for class Node: accessor methods for all 3 instance variables,
as well as "do: aBlock".
The "do:" methods are used to perform a block on each data element in the tree
using an in-order traversal. Once you have those, writing "values" is easy.
I also define my own class method "new" for one of the two classes (I won't
tell you which), to make sure everything is properly initialized.
-
Implement a Fraction class in Python to represent numbers such as 2/3
and -4/5. Your class should support creating new fractions with given
integer numerators and denominators, the operators +, -, *, /, and
conversion to float and to string. Fractions should always be stored
in reduced form, i.e. 1/4 instead of 2/8.
I suggest you proceed in three stages:
- Implement a first version with methods add, mul, toFloat, and
toString, and without reducing. Given the sample testing code
a = Fraction(6,8)
b = Fraction(5,4)
c = a.add(b)
print a.toString(), "+", b.toString(), "=", c.toString(), "=", c.toFloat()
d = c.mul(a)
print c.toString(), "*", a.toString(), "=", d.toString(), "=", d.toFloat()
your program should produce the following output:
6/8 + 5/4 = 64/32 = 2.0
64/32 * 6/8 = 384/256 = 1.5
- Add code to ensure that fractions are always reduced. Hint: a
GCD function might come in handy here. Also omit denominators of 1
when converting to strings. The code above should now produce the
following output:
3/4 + 5/4 = 2 = 2.0
2 * 3/4 = 3/2 = 1.5
- Add "sub" and "div" methods that implement the operations - and
/, and make sure everything works for negative fractions. Also allow
omitting the denominator of 1 when constructing integer fractions,
e.g., a = Fraction(3). Finally, define the operators +, -, *,
/ to work directly with Fraction objects. This is easily accomplished
in Python by renaming your "add" method into "__add__" (i.e., add two
leading and two trailing underscores). Do the same for "mul", "sub",
"div". Also rename "toFloat" into "__float__" and "toString" into
"__str__". (See the Python
documentation on special method names for more information on why this
works.) Now you should be able to run code like this:
a = Fraction(2)
b = Fraction(13, 4)
c = a-b
print a, "-", b, "=", c, "=", float(c)
a = Fraction(2, -3)
b = Fraction(-5, 6)
c = a/b
print a, "/", b, "=", c, "=", float(c)
which should produce the following output:
2 - 13/4 = -5/4 = -1.25
-2/3 / -5/6 = 4/5 = 0.8
You are allowed to work in groups of two on problems 2 and 3. Hand in
one printout per group. To print Smalltalk code, create a text file
containing your class and method definitions.
You can start by selecting "FileOut -> as" from the System Browser's
"Category" menu. But don't just hand in the resulting file, but clean it
up and organize it nicely!
(See my IntList file for an
example.)
For both Smalltalk and Python, don't forget to document your methods,
to include your name(s), names of collaborators, and how much time it
took you. Please include testing code and sample output! The
IntList file is also an example for good sample output. In any case,
give me more than just the testing code included above.