McGill University
School of Computer Science

Computer Science 308-203A
Introduction to Computing II

Fall Session, 2000

Examples (mostly) from Class


The following are examples either presented in class or provided as a "Java supplement" for those who have not seen Java. Feel free to use them for reference or as a template.


Simple Class Hierarchy
Some Minimalistic Classes

These three files define a very simple class hierarchy that does little other than create some objects and call some methods, printing out what is executing as it goes along. A.java contains a main function which runs the test, and the other two form a class hierarchy of just two classes (C.java extends B.java).

A.java, B.java, C.java


Skeleton Java Program
assignment.java

This little ".java" file has the minimum skeleton for a real program, into which you can insert your own code. Remember that the filename and the name of the "public class" must match.

Download source


Skeleton Java Program
io.java

This one gives the "magic incantation" for reading input from the keyboard. If you want to use some package found elsewhere to provide that functionality, that's fine but make sure you include the necessary code (java and class files) with your submission of assignments.

Download source


Sept 7, 2000
(in class, but revised)
boolean parens(String s)

This program determines whether the parentheses in a String are well-matched, i.e. if there is a '(' for every ')', properly nested.

Examples:

"((a) b ((c)))" has well-matched parentheses
")a("  does not
Two implementations are provided: one which is recursive and one which is not.

As I said in class, the problem cannot "really" be solved non-recursively, and the iterative routine below actually suffers from a theoretical limitation on the size of the input String (< 8GB in length!). But for most of us, that's not really a problem, so either version would be acceptable (say, on an assignment).

Download iterative version
Download recursive version


Sept 12, 2000
(in class)
String add(String a, String b)

This function takes two Strings (assumed to contain String representations of integers) and adds them together recursively, one digit at a time.

Recursion ends when both Strings are empty (""). If one is empty and the other not, the empty one is (cleverly) replaced with "0" at the beginning of the routines to avoid trying to take a character out of a null-string (which would make the program crash).

 Download source


Sept 12, 2000
(supplemental)
int coins(int cents)

This function counts (recursively) the number of ways to make up a certain number of cents using pennies and/or nickels and/or dimes and/or quarters.

It works as follows: the recursive function takes quarters first, then dimes, then nickels and lastly pennies. The denomination we're on is passed in as an additional argument.

Java tricks: It uses integer constants to represent the different denominations of coins and a constant array to give the value in cents of each of those denominations.

Download source


Sept 19, 2000
(For HW #2)
abstract class UserIO

This class provides the basic functionality of reading a String from an input stream. It has three abstract methods which are meant to encapsulate prompting a user for input and then converting the input to the right format. In the HW, you are to extend this class to fill in those methods (a skeleton for the subclass PromptIO  is provided as well).

UserIO.java
PromptIO.java (skeleton)


Sept 19, 2000
(For HW #2)
Text-based Windows Package

This set of classes performs some of the work of a "window manager" of the kind used in visual programming APIs (like the AWT for Java or the window manager from Microsofts Win32 API), but greatly simplified and using text. There is a simple command line interface for testing code, located in WindowCmdLine.java, which you should not need to modify.

You should download the whole thing compressed as a jar-file and then decompress it. If running in a command shell, use the following command, which will create a directory "windows" in the current working directory:

jar -xf windows.jar
It contains a skeleton module called IconifiableWindow.java which you will fill in for HW #2.

Compressed archive: Windows.jar

As individual files (if you must download them this way... I advise figuring out how to use jar):
 
 ConcreteDrawingArea.java Coordinate.java Desktop.java Drawable.java
DrawableVector.java DrawingArea.java EmbeddedDrawingArea.java Iconifiable.java
IconifiableWindow.java Screen.java TextWindow.java  TextWindow.java
Window.java  WindowCmdLine.java


Sept 19, 2000
(For HW #1)
class Keyboard

This is (purportedly) the source code for the Keyboard class used in 202. Google found it for me on some random server somewhere in Connecticut and I haven't tried to make it run, so "no warrenty expressed or implied."

Keyboard.java


Sept 26, 2000
(implements concepts discussed in class)
Lists Package

In class we discussed Lists, Stacks and Queues as data structures which are all variations on a simple theme. These basic concepts are implemented by the following code. Note that a class SimpleList was used to define the minimal functionality of a List, and this class was extended to get the three types in question. Note also that SimpleList is not abstract: it can be instantiated. It just doesn't provide much functionality on its own.

lists.jar
(contains SimpleList.java, List.java, Queue.java, Stack.java)
 


Oct 3, 2000
(implements concepts discussed in class)
Growable Arrays

Growable arrays were provided in class as another simple example of Big-Oh notation (and a practical one since the JDK class Vector is implemented in this way). Here's the files:

GrowableArray.java, AddToGrowArray.java, DoubleToGrowArray.java


Oct 19, 2000
(USEFUL FOR PROJECT)
Examples of Parsing Expressions

Here are two examples of using a tokenizer, in particular the TwoLayerTokenizer which you are to use for the term project, to parse some simple kinds of strings.

The first is a command line to perform a couple simple calculations on vectors, e.g. get the distance between two points.

The second parses expressions with integers, additions, multiplications and parentheses using the standard order of operations. The parsing is a little more complicated than what you need to do on the project, actually, and this is why for the calculation project operations are expressed, for example as (+ (* 3 4) 2) rather than as (3*4 + 2)...

PointCommandLine.java, simpleParser.java



 

Send comments/questions to wrenner@acm.org
[ McGill University ][ Academic Programs ] [ SOCS HOME

September 12, 2000