Some practice with method calls...
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:
getPositiveInt: This method should require the user to input a positive integer. Once it has obtained that integer, it should return it.
printRow: This method should print the number of asterisks (*) provided by the caller on a single row.
printTriangle: This method should print an entire triangle, where the size of the triangle (that is, the number of rows) is provided by the caller.
printPattern: This method should print the full pattern of triangles, where each triangle is one size smaller than the previous one, down to a triangle that is just one asterisk. The number of triangles to print (and thus the size of the first, largest triangle) is provided by the caller.
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.
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:
Accepts a double base value from the user (the value can be anything).
Accepts an int exponent value that must be non-negative. You should write a method that obtains this value.
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:
There should be output at the beginning of the method that shows the values of the parameters for this call.
There should be output just before the method returns indicating what value this call is going to return.
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?
When your work is complete, you should submit it using the cs11-submit command, like so:
cs11-submit lab-4 Triangles.java Exponent.java