Part 1: Writing Exercise:

The following are the exercises about the loops in Java. Write the answers in a comment block before the code of Part2.

a. Consider the following code snippet. The programmer expected that it printed 2014, ASU , but it did not. Explain the problem and solution in your word.

Scanner input = new Scanner ("2014nASUn");
int a = input.nextInt();
String b = input.nextLine(); System.out.println(a+ ","+b);

b. The following code generates the pattern (a) below. Explain the way to generate the pattern (b) in your word. It can be done by changing only one expression of for-loop.

for (int i = 0 ; i < 5; i++){
for (int j = 0 ; j < 5; j++) System.out.print("*");
System.out.println();
}
(a)
*****
*****
*****
*****
(a)
*****
****
***
**
*

c. Consider the following code. Write the 2014th output and the values of i and j at the time. The first output is 0 when i is 0 and j is 0. *) Add a variable to keep the count, and add a condition to print out when the count is equal to 2014.

for (int i =0; i<100; i++)
for (int j =0; j< 100; j++){
System.out.println(i*j);
j += 3;
}

Note: The answers to the questions (a through c) above should be typed in the block of comments in the java file such as;

/*
a) The problem is …
b) To generate …
c) XXX when i is xxx, and j is xxx
*/

Part 2: Programming

Write a Java program called Assignment4.java. The program is to display questions and read user inputs, then calculate and print out the requested data with a proper format.

This program will follow a very simple process.

Get Input -> Calculate -> Display Results

The goal is to develop a program to continuously generate a list of numbers with a table format. Each time the user input has two integers. The first integer defines the number of column, and the second the number of row for the output table size. For example, when the input numbers are 4 and 3, it displays the numbers from 1 to 12 in a 4 x 3 table as shown below. Once a table is displayed, the program waits for the next input.

Input: 4 3
Output: * | 1 2 3 4
------------------------
1 | 1 2 3 4
2 | 5 6 7 8
3 | 9 10 11 12

This assignment has only one task, but the development process is decomposed into two steps. Follow the instruction one by one. Don't go to the next step if your program does not return the proper output in a step.

Step1 : The first step is to develop a program to read the user input continuously as long as it is valid. The input should be a single line with two positive numbers such as "4 3". Once one of the input numbers is invalid (not integer or not positive), the program displays the message below and terminates. *** End of Program ***

Step2: This is the main step in this assignment. Create nested loops to print out the numbers in a table format. The first integer ROW defines the number of rows, and the second integer COL defines the number of columns. Inside the loops, write the code to display the positive numbers from 1 to Row*COL Use the System.out.printf() function with "%4d" and "%4s" formats for the integers and Strings to align them right. (See the section 2.3.2)

Test your program in your development environment. If it works, submit the Assignment4.java to the online submission site. Check whether your code returns the correct answer like below.

Set the size of table: 4 3
* | 1 2 3 4
------------------------
1 | 1 2 3 4
2 | 5 6 7 8
3 | 9 10 11 12
Set the size of table: 9 9
* | 1 2 3 4 5 6 7 8 9
--------------------------------------------
1 | 1 2 3 4 5 6 7 8 9
2 | 10 11 12 13 14 15 16 17 18
3 | 19 20 21 22 23 24 25 26 27
4 | 28 29 30 31 32 33 34 35 36
5 | 37 38 39 40 41 42 43 44 45
6 | 46 47 48 49 50 51 52 53 54
7 | 55 56 57 58 59 60 61 62 63
8 | 64 65 66 67 68 69 70 71 72
9 | 73 74 75 76 77 78 79 80 81
Set the size of table: Q
*** End of Program ***

Use only the Java statements that have been covered in class to date.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.