Java Reference
In-Depth Information
14.9 Lab
Create an application that reads an odd positive integer as a parameter. The application
should then print a diamond. For example, for input 5, the following diamond should be
printed.
∗∗∗
∗∗∗∗∗
∗∗∗
The solution should be purely recursive, that is, no loops are allowed. The following
recursive methods may be useful.
￿ void printTopPart(int n, int stars, int spaces)
￿
void printBottomPart(int n, int stars, int spaces)
￿
void printChars(char c, int frequency)
The printTopPart method should print the top part of the diamond. The variable n
is the user input, which is 5 in the above picture. This number will not change through
the recursive calls. The variables stars and spaces define the number of stars and spaces.
Both variables will change at each recursive call. Since their value changes differently in
the top and bottom part of the diamond, the task can be separated into two methods. The
printChars method prints the character c several times (i.e., frequency number of times).
14.10 Project
In this project you will find the longest increasing sequence in a two-dimensional area of
positive integers that is stored in a file. For example, the following numbers may be stored
in the file matrix.dat .
97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
Your first task is to read the integers from the file in a two-dimensional array. Since you
do not know how many numbers are stored in each line, you will need to use the nextLine
method to read a whole line and then use the StringTokenizer class to find the number
of integers per line. Assuming s is a line that is read from the file, the following code finds
the number of integers in the line and stores the result in the variable length .
 
Search WWH ::




Custom Search