Question: in java 4 . 2 2 ( Tabular Output ) Write a Java application that uses looping to print the following table of values: N

in java
4.22(Tabular Output) Write a Java application that uses looping to print the following table of values:
N 10*N 100*N 1000*N
1101001000
2202002000
3303003000
4404004000
5505005000
4.23(Find the Two Largest Numbers) Using an approach similar to that for Exercise 4.21, find the two largest values of the 10 values entered. [Note: You may input each number only once.]
4.24(Validating User Input) Modify the program in Fig. 4.12 to validate its inputs. For any input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.
4.25 What does the following program print?
1// Exercise 4.25: Mystery2.java
2 public class Mystery2{
3 public static void main(String[] args){
4 int count =1;
5
6 while (count <=10){
7 System.out.println(count %2==1?"****" : "++++++++");
8++count;
9}
10}
11}
4.26 What does the following program print?
1// Exercise 4.26: Mystery3.java
2 public class Mystery3{
3 public static void main(String[] args){
4 int row =10;
5
6 while (row >=1){
7 int column =1;
8
9 while (column <=10){
10 System.out.print(row %2==1?"<" : ">");
11++column;
12}
13
14--row;
15 System.out.println();
16}
17}
18}
4.27(Dangling-else Problem) The Java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). This behavior can lead to what is referred to as the dangling-else problem. The indentation of the nested statement
1 if (x >5)
2 if (y >5)
3 System.out.println("x and y are >5");
4 else
5 System.out.println("x is <=5");
appears to indicate that if x is greater than 5, the nested if statement determines whether y is also greater than 5. If so, the statement outputs the string "x and y are >5". Otherwise, it appears that if x is not greater than 5, the else part of the if...else outputs the string "x is <=5". Beware! This nested if...else statement does not execute as it appears. The compiler actually interprets the statement as
1 if (x >5)
2 if (y >5)
3 System.out.println("x and y are >5");
4 else
5 System.out.println("x is <=5");
in which the body of the first if is a nested if...else. The outer if statement tests whether x is greater than 5. If so, execution continues by testing whether y is also greater than 5. If the second condition is true, the proper string"x and y are >5"is displayed. However, if the second condition is false, the string "x is <=5" is displayed, even though we know that x is greater than 5. Equally bad, if the outer if statements condition is false, the inner if...else is skipped and nothing is displayed. For this exercise, add braces to the preceding code snippet to force the nested if...else statement to execute as it was originally intended.
4.28(Another Dangling-else Problem) Based on the dangling-else discussion in Exercise 4.27, state the output for each of the following code segments when x is 9 and y is 11 and when x is 11 and y is 9. We eliminated the indentation from the following code to make the problem more challenging. [Hint: Apply the indentation conventions youve learned.]
1 if (x <10)
2 if (y >10)
3 System.out.println("*****");
4 else
5 System.out.println("#####");
6 System.out.println("$$$$$");
7 if (x <10){
8 if (y >10)
9 System.out.println("*****");
10}
11 else {
12 System.out.println("#####");
13 System.out.println("$$$$$");
14}
4.29(Another Dangling-else Problem) Based on the dangling-else discussion in Exercise 4.27, modify the following code to produce the output shown. Use proper indentation techniques. You must not make any additional changes other than inserting braces and changing the codes indentation. Weve eliminated the indentation from the following code to make the problem more challenging. [Note: Its possible that no modification is necessary.]
1 if (y ==8)
2 if (x ==5)
3 System.out.println("@@@@@");
4 else
5 System.out.println("#####");
6 System.out.println("$$$$$");
7 System.out.println("&&&&&");

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!