The Elements of Computing  Systems / Nisan & Schocken / www.idc.ac.il/tecs 

Project 9:
High Level Programming

Objective: Practice high level programming in the Jack language.  Jack is a simple, modern, object-based programming language.  In the next project we will build a compiler for it.  Before we do so, it helps getting acquainted with the language by writing some programs in it.

Contract: Adopt or invent an application idea like a simple computer game or some other interactive program.  Then design and build the application.

Resources: You will need several tools: the Jack Compiler or the Jack IDE, to translate your program into a set of .vm files, the VM Emulator, to run and test your program, and the Jack Operating System (OS). To help you get started, we provide a sample Jack program, stored in  project 09.zip. Start by creating a directory named projects/09 on your computer, and extract this zip file into it (preserving the directory structure embedded in the zip file).

The  Jack OS

The executable Jack Operating System is a set of .vm files that implement the standard library of the Jack programming language specified in Chapter 9.  In order for any Jack program to execute properly, the compiled .vm files of the program must reside in a directory that also contains all the .vm files of the Jack OS.  Here is a list of the Jack OS Error Codes and their textual meaning.

Compiling and Running a Jack Program

0.      It is best to store all the class files associated with a Jack program in a single directory, say Xxx.  Start by creating this directory, then copy all the files from tools/OS into it. 

1.      Write your program -- a set of one or more Jack classes -- each stored in a separate ClassName.jack text file. Put all these .jack files in the Xxx directory.

2.      Compile your program using the supplied Jack Compiler (or the Jack IDE). This is best done by applying the compiler to the name of the program directory (Xxx). This will cause the compiler to translate all the .jack classes found in the directory into corresponding .vm files. If a compilation error is reported, debug the relevant class file and re-compile Xxx until no error messages are issued.

3.      At this point the Xxx directory should contain three sets of files: your source .jack files, the .vm files that the compiler generated from them, and the .vm files comprising the Jack OS. To test your program, load the entire Xxx directory into the VM Emulator and, then run the program. In case of run-time errors or undesired program behavior, fix the program and go to stage 2.

A Sample Jack Program: Square Dance 

Description: This simple interactive program allows the user to move a square around the screen: 

 The user can  also change the square size during the movement.

 

When the program starts running, a square of size 30 by 30 pixels is placed at the top left corner of the screen.  The program then listens, and responds to, the following keyboard keys:

  • right arrow:    move the square to the right;
  • left arrow:       move the square to the left;
  • up arrow:        move the square up;
  • down arrow:   move the square down;
  • x:                      increment the square size by 2 pixels;
  • z:                      decrement the square size by 2 pixels;
  • q:                     quit the program.

Movement speed: to control it, you can change the delay constant in the moveSquare method in class SquareGame.

Here is the source code of the Square Dance program (also available in project 09.zip):

Class Description

Main.Jack

Initializes and starts a new "square moving session."

Square.jack

Implements an animated square. A square object has properties that characterizes its size and screen location, and methods for drawing, erasing, moving, and size changing.

SquareGame.jack

Runs the program according to the game rules.