CM10135 / Programming II: Tutorial 2
This is an optional second tutorial with several objectives:
There are at least two good reasons to work in pairs on a problem.
is just that you learn better. Extensive research in learning has
that if you have to explain what you are doing, you will learn better
an exercise than if you don't, even if you produce the same answer on
exercise. Coming up with an answer isn't always the same as
understanding the answer, but if you talk about it you probably will
- To make certain you understand the sorting algorithms presented
- to make sure everyone is feeling comfortable programming in Java,
- to give you a feel for a relatively recent innovation in software
engineering, pair programming.
The other argument is the software engineering one. The idea is
most development time is spent not so much in writing new code, but in
it & trying to get it to work. With two people looking at one
of code, it's quite likely that many bugs will be caught before you
try to compile.
One thing that you need to know for pair programming is another lesson
Fredrick Brook's "Mythical Man Month" --- there are many ways to solve
code any problem, and a lot of time can be wasted arguing about which
best. Often, the amount of improvement from this discussion may
be worth the time or emotional cost of the discussion. The
to this situation is to make one person in charge for the duration of a
During this tutorial, the person who is holding the keyboard will
the one who decides what to do when the two of you can't agree.
worry, you'll get to take turns holding the keyboard.
Again, you will be learning more about agile programming methods in
second year. This tutorial (your last that's not just about
is exploiting the methodology to help make sure everyone really
the sorting algorithms, and that everyone is really happy developing
What to do:
Be sure both partners save copies of all the code you write -- you
might want to refer to it some time in the future.
- Choose a partner.
- Choose which partner goes first at the keyboard. You're
to be doing two sorting algorithms, so you each get to code one.
- Create some objects to sort. Create a class with some key
you will sort on. If you can't think of anything you like better,
create a class "Child" which has an attribute "age" that you can sort
- Create a "main" function for your class that takes an argument
is a length and creates an array full of your objects with different
might want to use the "random" function in the math library.
It will give you a value between 0 & 1. To make that
a child's age, multiply it by 18 (now the number is between 0 & 18)
cast it to an integer (you should probably use java.lang.Math's round
to be explicit about what happens rather than just a cast). That
way you can easily test your sorting algorithms
over a variety of values.
- Make sure your main function can print out the key values of
the objects in the order they appear in the array.
- Create a sorting method for your class, using either selection
sort or insertion sort (you choose).
- First discuss the algorithm with drawings & make sure both
agree on how it will work, and what variables will be needed.
- It's not cheating to look at the course
notes, but it is cheating
to just download a java version.
- Give the other
partner the keyboard.
- Run your sort function from the main program. Make sure it
for odd & even numbers of objects, as well as empty arrays and
with only one thing in them.
- Create a quick sort for your class.
- Run the same tests over your quick sort.
- Final bonus thing to
(you can change the keyboard again now if you want, up to you) --- look
how conventional java programmers (not computer scientists) would be
- Put your objects into an ArrayList, and then figure out how to
use Collections.sort() to sort them.
- To do this properly, you should probably implement the
interface Comparable for your class, using your attribute
- For example, if you used my default, order Child objects by
- You might want to look at the code for Collections.sort() too.
- You may also want to look at java.util.random. The way I told
above to get a random integer will work in any language, but
is slightly easier to use
because of the interface, and more importantly is a better random
page author: Joanna Bryson
20 Feb 2005