CS102 - Bits and Pieces
Looking for something?
Almost everything should be on
CS102 Moodle,
but if not, and it isn't here either,
please ask for it to be added.
This page is primarily for class materials used by David in sections 1, 2 & 3.
CS102 webpages for individual sections
Optional extra lab & homework questions:
Classroom examples & presentations:
- CS102 Introduction slides
- (Course organisation)
-
Hangman homework
- sections 1, 2, 3 only - note that the final integration stage is to be done
individually.
-
CS102 CardGame slides - (Review & design exercise)
Homework:
- Rewrite the Card class using a single property - int
cardNumber;
- Try writing the Card, Cards (including createFullPack & Shuffle
methods), Player & ScoreCard classes.
- Add a method to the ScoreCard class that returns an array of the
winners.
- Write an alternative implementation of the Cards class that uses an
ArrayList rather than an array.
- Rewrite the ScoreCard class so that it includes player names as well
as scores. Provide two alternative implementations, one using parallel
arrays for names and scores, the other using a collection of
ScoreCardEntry instances (where ScoreCardEntry is a class having one
name and one score).
- Write the game class. Write a main method to demonstrate its usage.
Provide users with a menu from which to select which player will play a
card (remember, the game should reject cards from players if it is not
their turn.)
- CardGame homework - sections 1, 2, 3 only - to be done individually
-
CS102 OOP slides - (Inheritance & Polymorphism)
Homework:
- Read chapters 8 & 9 of the textbook.
- Take a look at and play with the
JRobo examples.
-
CS102_OOP_2 slides - (Abstract Classes & Interfaces)
Homework:
- Try implementing the Media Library example we looked at in class
- then download and play around with the
06CDExamples. Add a new Media type, say DVD and convince yourself
that it must implement getPrice(), getDuration(), and get & setCategory()
before it can be added to the Library.
-
Java_System_Architectures slides &
notes (...somewhat old & in serious need of an update!)
Homework:
-
JavaCodingOOP-InterfaceBits - ( slides on several standard Java Interfaces,
Inner Classes, etc. -subject to change! )
- JavaCoding-InterfaceExample
Homework:
- Download and experiment with the
AlarmThermostat &
AlarmListener examples we looked at during class.
- Try various alternate solutions for the handleAlarm method's
parameters, eg. none, boolean, Object, Thermostat, and...
- Write an AlarmInfo class that encapsulates the Thermostat event info
and modify the AlarmListener example so it creates a new instance of
this class and passes it to the listener, rather than a direct reference
to the Thermostat itself.
- Extend the AlarmListener example to allow multiple listeners to
respond to the same event, e.g. to allow the thermostat to switch a
refrigeration unit on and sound an alarm bell when the temperature goes
too high.
- Consider how you might modify the example to allow
multiple/different devices to control the heater, for instance, a
thermostat and a remote control device.
-
CS102_GUI slides - (GUI using AWT & Swing) -- under revision!
Homework:
- Ref: Sun's
A Visual Guide to Layout Managers
(be sure to read about the GroupLayout, it is an excellent example of
design/analysis).
- You can download the
GUI event handling demo (approximately what) I wrote in class -but please try doing it
yourself first!
- see also my
awt frame examples - simple sequence of examples similar to those we
did in class!
- Ref: Sun's
Listener API Table &
Listeners supported by Swing Components
- Swing
- MVC (Model, View, Controller) - the design pattern your projects
must follow!
- My
mvc variant examples (zipped)
- try adding the extra variants, including a separate controller
class.
- & my
clock example (zipped)
Homework:
- try making an alarm clock or a count up/down timer using
this.
- Ref: Read this
Java SE Application Design With MVC, and this excellent
background
Swing Architecture Overview article, which discusses Swing's MVC
variant, the so-called "separable model architecture," with its
pluggable look & feel architecture (these are essential for really
understanding the beauty of Swing)!
- Further homework:
- Extend PopQuiz1b to update multiple views. Create a
CircleListener interface. Modify Circle so it maintains an
ArrayList of CircleListeners (views), all of which are informed
of updates to the Circle (model.) Add another view type; a
graphical representation of the corresponding circle. Make this
act as a controller too, by detecting mouse dragging events
which set the circle's radius.
- Why does Java use Vector rather than ArrayList objects for
all its listeners?
- PopQuiz1c manages multiple views using Java's
Observer-Observable. The model class becomes a sub-class of
Observable, thus directly inheriting the necessary
functionality. Unfortunately, this is not always convenient
since Java is a single inheritance language and models are often
already part of an class hierarchy. The alternative is to use
composition (has_a rather than is_a). Modify the example to use
Observable in this way. Note: for some strange reason setChanged()
has protected access making it impossible to use directly--a
clear indication the class was only intended to be used by
sub-classing (wherein lies the, admittedly somewhat crude, way
to resolve the problem)! Another difficulty is that the default
parameter in update is the Observable object, not the model; how
might you resolve that?
- CS102_Recursion slides -
(Recursive methods in Java)
Homework:
- see these
additional resources on recursion
- Write the Java code for each of the basic printReverse &
printForward routines. Make sure you can trace them by hand so you
understand their logic. Execute them in JCreator.
- Implement and test your solution to the Tower of Hanoi problem.
- Write a recursive method to return the location of the maximum
of the first n elements of an int array.
- Write a recursive method to find the minimum value between two
given indexes of an int array.
- Write a recursive method to determine whether two int arrays are
the same, i.e. contain exactly the same values in exactly the same
order.
- Write a recursive method that sums all the values from a given
index in an int array, up to a sentinel value, say -1.
- Design & implement the merge algorithm, then use it in mergeSort.
- Add SelectionSort and Mergesort to Java's sort demo example, so
you can compare their performance visually.
- Given a two dimensional grid containing cells which are either
empty or filled, write a recursive method to determine whether there
is a path from any x, y point in a grid to an exit location in the
grid's bottom right corner, without traversing the filled cells.
- Implement a fractal such as the Koch line.
- Do questions 11.7 & 11.8 from your textbook (on Whoozit's,
Whatzit's & Blurb's).
-
CS102_Data Structures slides - (Data Structures in Java)
Homework:
- Implement and test a simple singly-linked list for Strings (as in
the classroom examples.) Use a private inner class for Node.
- Write a method that, given a reference to a node in a singly-linked
list, returns a reference to the previous node if there is one, or null
if it is the first node in the list.
- Write a recursive method that prints the contents of a singly-linked
list.
- Write a recursive method to print the singly-linked list's contents
in reverse sequence, i.e. last node first.
- Write an iterative method to print a singly-linked list in reverse
sequence. Hint: use a method that returns a reference to the previous
node in the list.
- Write a method that uses a stack to convert an infix expression to
postfix form. Hint: You might try inventing the algorithm yourself (or
simply search the Internet or the library for it!)
- Write a method that will return the minimum value in a binary search
tree.
- Write a method that prints the contents of a binary search tree,
with the proper layout. Hint: Add a parameter that gives the current
node's level within the tree and arrange the indentation accordingly
when printing it. For those who did the extra part of Lab06--printing
the contents of a given disk folder--try modifying your method to pretty
print it in this way too.
- Normal tree traversals are all depth-first (i.e. they go all the way
to the extreme left node before doing much.) An alternative is to
traverse the tree breadth-first. Such a traversal would process the tree
level-by-level, first the root node, then its left and right sub-trees,
then their left & right sub-trees, and so on. You can do this with the help of
a queue. Try to design an algorithm yourself or check the Internet if
you get stuck.
-
CS102_Collection Framework slides - (Overview of Java's
Collection Framework)
Homework:
-
CS102_Exception slides (Exception handling in Java)
Homework:
- Write a stack class & use it to make a postfix stack calculator. Add
exception handling to it.
-
CS102_Files slides (Streams & Files in Java)
- CS102_ToConclude...!
Project Documents:
Labs:
Misc:
Moodle:
- get help and keep track of your assignments, grades on the CS102
Moodle
page.