CMPT 167 Spring 2007 Lab Assignments

Lab 1a: Selection (due Wed 24 Jan) (20pts)

Write a Java applet or Swing program that uses the if or switch structures to print the song, "The Twelve Days of Christmas". The user must be asked to enter a day from 1 to 12, and the program must display the corresponding message for that day. The strings for each day should only appear in your code once each (e.g., the string "five golden rings" should only appear in your code once). Google for the lyrics, or make up your own (keep it clean, please).

Lab 1b: Repetition (due Wed 31 Jan) (20pts)

The proper divisors of an integer n are the positive divisors less than n. A positive integer is said to be deficient, perfect, or abundant if the sum of its proper divisors is less than, equal to, or greater than the number, respectively. For example, 8 is deficient, because its proper divisors are 1, 2 and 4, and 1+2+4 < 8; 6 is perfect, because 1+2+3 = 6; and 12 is abundant, because 1+2+3+4+6 > 12. (The number 1 has no proper divisors and hence is deficient.) Write an application that classifies n as being deficient, perfect, or abundant for n = 20 to 30, then for n = 490 to 500, and finally for n = 8120 to 8130.

Lab 2: Arrays (due Wed 7 Feb) (40pts)

A magic square is an n x n matrix in which each of the integers 1, 2, 3, ...., n2 appears exactly once and all column sums, row sums, and diagonal sums are equal. For example, the following is a 5 x 5 magic square in which all the rows, columns, and diagonals add up to 65:

17241815
23571416
46132022
101219213
11182529

The following is a procedure for constructing an n x n magic square for any odd integer n. Place 1 in the middle of the top row. Then after integer k has been placed, move up one row and one column to the right to place the next k + 1, unless one of the following occurs:

  1. If a move takes you above the top row in the jth column, move to the bottom of the jth column and place the integer there.
  2. If a move takes you outside to the right of the square in the ith row, place the integer in the ith row at the left side.
  3. If a move take you to an already filled square or if you move out of the square at the upper-right hand corner, place k + 1 immediately below k.

Write a GUI program in JAVA (applet or standalone, AWT or Swing) to construct an n x n magic square for any odd value of n.

For this lab, it's okay to do the output as one big long string (e.g., JOptionPane.showMessageDialog()), but do make sure that the rows and columns line up properly -- the non-fixed-width font makes this hard. It's a better option (also for future labs) to learn a Swing layout manager and make each cell a separate widget.

Lab 3: Recursion (due Wed 21 Feb) (40pts)

Write a GUI program in Java to render a fractal L-system using a recursive algorithm. The shape need not be a tree.

The FractalTree directory contains a small Swing application that demonstrates the Java2D graphics API, including applying transformations and drawing lines. You may use this code as a starting point. The solution subdirectory contains a sample solution program demonstrating a fractal tree. Your program need not have the cool mouse interaction, but you may add something similar for up to 10% extra credit.

Lab 4: Object-Oriented Concepts (due Wed 14 Mar) (40pts)

Part A: Sets

Create a class IntegerSet. Each object of the class can hold integers in the range 0 through 99. Internally, represent the set as an array of booleans: the i-th element of the array is true iff the integer i is in the set. The no-parameter constructor initializes the set to the empty set. Provide public methods for each of the following: Write a small testbed class (either command-line or GUI) to show off your IntegerSet class and its methods.

Part B: Vectors

Create a class Vector. Each object of the class has three members which represent components of the vector along the three coordinate axes. The no-parameter constructor initializes the vector to the zero vector (all components zero). Provide public methods for each of the following: Extend your Vector class to a subclass Vector2D, which sets the z-component to zero. Provide an additional method to draw the vector on the screen. Create a testbed program (AWT or Swing, applet or standalone) that graphically demonstrates Vector2D sum, difference, and scalar multiplication, using this method.

Lab 5: Files and I/O (due Wed 11 Apr) (60pts)

In this lab you will be implementing an inventory and point-of-sale system for a fictional store. Your store may sell whatever items you wish; be creative!

The objective of this lab is to gain experience in file I/O in Java. You are free to use any technique you like from ch14 for the databases (sequential, random-access, text, object-serialized, etc.), but think about what format would work best for each database. Discuss the pros and cons of your file format choice in your lab write-up: how well would it work as the number of items/records scales up? Is there a tradeoff of disk space, searching time (cash register), time to add/delete (inventory control), etc.? As this lab is fairly complex, it is worth more points. For the sake of our TA's time, no late labs will be accepted past Fri 13Apr.

There are two databases (files) that you will need to manipulate:

You will need to provide three end-user GUI applications: