Question: Consider the following Java code and answer the parts of this question. Each line has been given a number so we can refer to it

Consider the following Java code and answer the parts of this question. Each line has been given a number so we can refer to it if needed.

1 public class Something {

2 public static void main(String[] args) {

3 int inputNumber;

4 int sum;

5 int count;

6 double a;

7 sum=0;

8 count = 0;

9 System.out.print("Enter your first positive integer: ");

10 inputNumber = TextIO.getlnInt();

11 while (inputNumber != 0) {

12 sum += inputNumber;

13 count++;

14 System.out.print("Enter your next positive integer, or 0 to end: ");

15 inputNumber = TextIO.getlnInt();

16 }

17 if (count == 0) {

18 System.out.println("You didn't enter any data!");

19 }

20 else {

21 a = ((double)sum) / count;

22 System.out.println();

23 System.out.println("You entered " + count + " positive integers.");

24 System.out.println("The value of a is "+a);

25 }

26 } // end main()

27 } // end class Something

a. What is happening in lines 7 and 8 above?

b. In line 11 above, why is the condition on the while loop (inputNumber != 0) ?

c. There is an if-statement in lines 17-19 before the output, why is this placed there?

d. What do you think the program above is trying to achieve?

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 Databases Questions!