Ch 6 Lab: Multiplication Table

Authors: Ralph Morelli and Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 6

Brief description

This is a simple lab that is suitable for Chapter 6. It requires a nested counting loops, which are used to print out a multiplication table. It uses a simple applet interface, including TextArea and TextField components. It can be used to illustrate some of the basic TextArea methods, such as append() and setText(). It also uses escape sequences to format the output in the TextArea.

Objectives

The objectives of this lab are:

Problem Statement

Write a Java applet that displays a variable sized triangular multiplication table. The table should consist of from 1 to 12 rows. The number of rows is decided by letting the user input a number into a TextField. The first row should contain 1 column, the second row should contain two columns, and, in general, row N should contain N colums. The rows should be neatly formatted so that each column of the table is properly aligned.

Before Lab

Read chapter 6 of Java, Java, Java, 3E.

Object Oriented Design

The computation involved in this project can easily be handled in the applet's actionPerformed() method. Therefore, you need only one class for this project, the applet class.

GUI Design

Follow the GUI design shown in the demo program. This involves an input TextField and an output TextArea. The TextField should be associated with an ActionListener. Whenever the user types a value into the TextField, the applet should clear the TextArea and display the properly formatted multiplication table.

Note: To clear a TextArea, use the setText(String) method. To append text to the text already contained in a TextArea, use the append(String) method. Recall that the escape sequence \t will place a tab into the TextArea, and the \n will put a new line into the TextArea.

Create a MultTableApplet.html file that accesses the the MultTableApplet.class file in an applet tag to use when running your applet program.

Algorithm Design

This algorithm requires a nested for loop, one loop for the rows of the table and the other for the columns in each row. See section 6.4 for examples of these kinds of loops. The algorithm can be implemented right in the actionPerformed() method or it can be encapsulated into a method that is called from actionPerformed().

Optional: Error Checking

Revise your applet so that it checks the validity of the user's input. The value input into the TextField should be between 1 and 12. Display an error message if an erroneous input is given.

Handin

Have your worked check before leaving lab. Hand in copies of your properly documented source code. For documentation guidelines, see documentation specifications.

You're done. Great work!