CS 11 Fall 2004 -- Lab 4

Some practice with method calls...


Breaking code into methods

First, we'll begin with your code from the previous lab that printed a pattern of stars. Specifically, we want to break down the code into a number of methods:

After writing these methods, the main method should be modified to call first the getPositiveInt method, and then to pass the result of that method call to printPattern in order to get the pattern to appear.

To get started on this project, you should create a lab-4 directory and then make a copy of your lab-3 code. From your home directory, the commands should be:

mkdir lab-4
cp lab-3/Triangles.java lab-4
cd lab-4

You will now be in your lab-4 directory with a copy of your Triangles.java code to modify.


Calculating exponents

Recall, from class, our recursive method to calculate an exponent. Begin by writing a complete program, within a file named Exponent.java, that does the following:

  1. Accepts a double base value from the user (the value can be anything).

  2. Accepts an int exponent value that must be non-negative. You should write a method that obtains this value.

  3. Calculates the exponent and prints the result. You should, of course, use the calcExp method to perform the calculation.

Once you have this program working, you should add something to it: Specifically, you should add System.out.println method calls within calcExp that show you the order of the recursive method calls. Specifically:

Be sure, with this output in place, that you can see the order of the recursive calls -- is it what you expected? Do you see the pattern?


Submitting your work

When your work is complete, you should submit it using the cs11-submit command, like so:

cs11-submit lab-4 Triangles.java Exponent.java

This lab is due on Thursday, October 7th at 11:59 pm!

Scott F. Kaplan
Last modified: Fri Oct 1 12:16:05 EDT 2004