Starting from:

$30

EE328-Project 1: Snake-Like Increment Tables Solved

1 PROBLEM
1.1 DESCRIPTION
This program will create small, snake-like increment tables, up to 9x9 in size. It starts from 1, and the value keep increasing by one. For the first row, it increase from left to right; for the second row, it increase from right to left; the third row, from left to right, ... , etc. Just like a snake.

1.2 SAMPLE
>java SnakeTable

Enter table size 1-9, 0 to exit: 9

1            2        3        4        5        6        7        8        9

17 16           15           14           13           12           11           10
20 21           22           23           24           25           26           27
35 34           33           32           31           30           29           28
38 39           40           41           42           43           44           45
53 52           51           50           49           48           47           46
56 57           58           59           60           61           62           63
71 70           69           68           67           66           65           64
74 75           76           77           78           79           80           81
Enter table size 1-9, 0 to exit: 3

1        2     3

5 4
8 9
Enter table size 1-9, 0 to exit: 1

1

Enter table size 1-9, 0 to exit: 746

please enter a number in the range 0-9

Enter table size 1-9, 0 to exit: -231 please enter a number in the range 0-9

Enter table size 1-9, 0 to exit: 0

1.3 SPECIFICATION
The program should repeatedly request input in the range 0-9 (sample code to get console input is below). Numbers outside this range should be politely rejected. 0 will stop execution of the program. Numbers not parseable as integers should also cause the program to exit. For input in the 1-9 range, the program should produce a multiplication table as shown above. The formatting does not have to precisely match the above, but the numbers should be in columns. You may use any combination of tabs and spaces for formatting. I would avoid Java’s NumberFormatter classes for this one, but if you’re feeling daring, go for it.

2 ALGORITHM
2.1 BASIC ALGORITHM
After read in the user input we use a function printSnake() to print the snake table. It’s easy to learn that in odd rows the number is increasing while the number is decreasing in even rows. Considering the row is denoted as i and the column is denoted as j, the algorithm is designed below.

For odd rows the numbers are set to be n * (i - 1) + j.
For even rows the numbers are set to be n * i + 1 - j.
2.2 ROBUSTNESS CONSIDERATION
In order to improve the robustness of user input we use regular expression [0-9] to catch correct input. If the input is out of bound or characteristics other than numbers the command line window will display "please enter a number in the range 0-9". Particularly, when the input equals ’0’ the function printSnake() will not be executed and the program will exit.

3 RESULTS
3.1 ENVIRONMENT
Windows 10
Java Development Kit 1.8.0_131
Eclipse
3.2 SCREENSHOTS OF THE RESULT
We use command line to compile and execute the program. The result is shown in Fig. 3.1.

Figure 3.1: Screenshot of Snake-Like Increment Table

3.3 THOUGHTS
It is the first Java program I have ever written except "hello world!". Java has a lot of interesting features and I will try to make full use of them in the future.

More products