Question

The following individual two programs are using while loop in order to print a specific output:...

The following individual two programs are using while loop in order to print a specific output:

The outputs of the first program are:

10

9

8

5

4

The outputs of the second program are:

123456789

Using the Table 3.1 below, allocate the error(s) of each line of two programs and type of error:

The first program:

  1. public Main
  2. {
  3. public static void main(String args[]){
  4. int i=10
  5. while(i>4){
  6. System.out.print(i);
  7. i++;
  8. }
  9. }
  10. }

The second program:

  1.   public class Main
  2.   {
  3.   public static void main(String args[]){
  4.   int i=10;
  5.   while(i<1){
  6.   System.out.println(i);
  7.   i--;
  8.   }
  9.   }
  10.   }

Table 3.1

Line Number

Error

Type of error(logical error or Syntax error)

111

Miss word class

Syntax error

22

     no

no

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Program 1:

Line Number

Error

Type of Error

Line 1

Class word is Missing

Syntax Error

Line 2

No error

No error

Line 3

No error

No error

Line 4

Semi colon ; is Missing

Syntax Error

Line 5

Condition has to change since we need 4 also.

i.e while (i>=4)

Logical error

Line 6

No error

No error

Line 7

i value should be decrement since output needed is

10 9 8 like that

Logical Error

Line 8

No error

No error

Line 9

No error

No error

Line 10

No error

No error

Program 2:

Line Number

Error

Type of Error

Line 1

No error

No Error

Line 2

No error

No error

Line 3

No error

No error

Line 4

i value should initialize to 1 because output needed is like

1 2 3 4 5 6 7 8 9

Logical Error

Line 5

Condition has to change

I.e while (i < 10)

Logical error

Line 6

No error

No error

Line 7

i value should be increment

Logical Error

Line 8

No error

No error

Line 9

No error

No error

Line 10

No error

No error


Modified Program 1:

public class Main

{

public static void main(String args[]){

int i=10;

while(i>=4){

System.out.print(i);

i--;

}

}

}

Modified Program 2:

public class Main

{

public static void main(String args[]){

int i=1;

while(i<10){

System.out.println(i);

i++;

}

}

}

Note : if you have any queries please post a comment thanks a lot..always available to help you..

Add a comment
Know the answer?
Add Answer to:
The following individual two programs are using while loop in order to print a specific output:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following individual two programs are using while loop in order to print a specific output:...

    The following individual two programs are using while loop in order to print a specific output: The outputs of the first program are: 10 9 8 5 4 The outputs of the second program are: 123456789 Using the Table 3.1 below, allocate the error(s) of each line of two programs and type of error: The first program: public Main { public static void main(String args[]){ int i=10 while(i>4){ System.out.print(i); i++; } } } The second program:   public class Main   {...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • What is the output of the following codes? Line1: public class ArrayLength { Line2: public static...

    What is the output of the following codes? Line1: public class ArrayLength { Line2: public static void main(String args[]) { Line 3: int a[] = {45, 44, 39, 48, 37); Line 4: System.out.print(a.length()); Line5: Line 6: } w Compiling error Oo 05 04

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

  • Recursion Tree Goal: Predict the output of a recursive method call using a recursion tree. Draw...

    Recursion Tree Goal: Predict the output of a recursive method call using a recursion tree. Draw the recursion tree of the following source code, showing all method calls and outputs: public class Main { public static void main(String[] args) { f(3, 4); } public static void f(int x, int y) { if(x + y > 1) { f(x - 2, y - 1); System.out.print(x + " "); f(y, x - 2); System.out.print(2 * x + y + " "); }...

  • output What is the output of the following: class Access public int x; private int y...

    output What is the output of the following: class Access public int x; private int y public void cal(int x, int y) { this.x = x + 1; this.y = y; } public void print() { System.out.print(""+y); } } class AccessSpecifier { public static void main(String args[]) { Access obj = new Access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } } 33 Compilation error 23 None of the available choices Runtime error

  • I need a java program that prints the following: 1* 2** 3*** 4**** 5***** 6****** 7*******...

    I need a java program that prints the following: 1* 2** 3*** 4**** 5***** 6****** 7******* 8******** 9********* This is what I have so far, but I cannot figure out how to fix it. public class HelloWorld{ public static void main(String []args){ System.out.println("Pattern Two");    for (int line=1; line<10; line++){ System.out.println(); for (int j=1; j System.out.print(line); for (int i=1; i System.out.print("*"); } } } } }

  • Complete the do-while loop to output 0 to countLimit. Assume the user will only input a...

    Complete the do-while loop to output 0 to countLimit. Assume the user will only input a positive number. import java.util.Scanner; public class CountToLimit { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); int countLimit = 0; int printVal = 0; // Get user input countLimit = scnr.nextInt(); printVal = 0; do { System.out.print(printVal + " "); printVal = printVal + 1; } while ( /* Your solution goes here */ ); System.out.println(""); return; } }

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT