# 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:

 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

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:
• A boolean method which returns true if a given integer belongs to the set.
• A method which inserts an element into the set.
• A method which deletes an element from the set.
• A method which tests if the set is equal to another set.
• A method which returns the union of two sets.
• A method which returns the intersection of two sets.
• A method which returns the difference of two sets. (A - B is all the elements of A which are not in B.)
• A method which returns the xor (exclusive or) of two sets.
• A toString() method which returns a string representation of the set: e.g., "{ 0, 15, 87 }".
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:
• A method which returns the length of the vector as a double.
• A method which returns the sum of two vectors.
• A method which returns the difference between two vectors.
• A method which returns a scalar multiple of the vector (multiplying each component by the given scalar).
• A method which returns the dot product of two vectors.
• A method which returns the cross product of two vectors: (a,b,c) x (x,y,z) = (bz-cy, cx-az, ay-bx)
• A toString() method which returns a string representation of the vector in a form like, e.g., "2.3i - 0.5j + 17.25k". If a component is zero, it should not print the corresponding term. If the whole vector is zero, it should just print "0".
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:

• An inventory database: for each item, record
• A description of the item
• Price of the item
• Whether GST applies to the item
• Whether PST applies to the item
• Quantity in stock
• A transaction log that is appended to whenever a customer buys something. Each transaction record consists of
• A transaction ID code (if a customer buys multiple items in one transaction, there will be multiple records with the same ID code)
• The ID of the item (index into the inventory database)
• Quantity purchased
You will need to provide three end-user GUI applications:
• An inventory control program to allow the user to
• Edit existing items
• Delete existing items
• Save the inventory database
• A checkout register program that a cashier can use to record a customer's purchases:
• Select items to be purchased and quantity desired of each item
• Display a receipt showing
• For each item, its description, price per unit, and quantity,
• The subtotal for this transaction, and
• The grand total including any GST/PST
• Append to the transaction database
• Update the stock count in the inventory database
• A book-keeping program that allows the user to
• View the transaction database, and
• Generate a report summarizing all the transactions in the database:
• A list of items sold (including quantities)
• Total income (including GST/PST) from all sales
You may wish to use some advanced Swing widgets to interact with the databases.