# Java Program Q1b: Adding Positive Integers ## Task Description Complete the program `Q1b` that reads a set of integer values into the integer array `numbers` (this part is already done for you). Your task is to add up the positive values in the `numbers` array until the program encounters the value `999`. The number `999` is not included in the sum. At that point, the program should print the sum of the positive numbers and their average. ### Example 1 When the input is: `java Q1b 5 -3 8 999 7 1` - The program outputs: - Sum: `13` - Average: `6.5` ### Example 2 When the input is: `java Q1b 5 -3 8 7 1` - The program outputs: - Sum: `21` - Average: `5.25` ## Java Class Code ```java public class Q1b { public static void main (String[] args) { int[] numbers = new int[args.length]; for (int i = 0; i < args.length; i++) { numbers[i] = Integer.parseInt(args[i]); } // WRITE YOUR CODE HERE } } ``` ### Instructions - Initiate a loop through the `numbers` array. - Check each element; if it's positive, add it to a running total. - Stop processing when you encounter the number `999`. - Calculate and output the sum and average of the positive numbers encountered before `999`. This task helps reinforce the implementation of basic loops, conditional statements, and arithmetic calculations in Java. ```java public class Q1a { public static void main (String[] args) { int n = 1000; int mySum = 0; for (int i = n; i >= 0; i--) { if (i % 2 == 0) { mySum += i; } } StdOut.println(mySum); } } ``` **Explanation:** This Java program calculates the sum of all even numbers from 1000 down to 0. The program is structured as follows: 1. **Initialization:** - `int n = 1000;`: This variable `n` is set to 1000, representing the starting number. - `int mySum = 0;`: This variable `mySum` is initialized to 0 and will store the cumulative sum of even numbers. 2. **For Loop:** - `for (int i = n; i >= 0; i--)`: This loop iterates from `n` (1000) down to 0, decrementing `i` by 1 in each iteration. 3. **Condition Check:** - `if (i % 2 == 0)`: Inside the loop, an `if` statement checks if `i` is even. This is done by checking if the remainder when `i` is divided by 2 is zero. 4. **Summing Evens:** - `mySum += i;`: If `i` is even, it is added to `mySum`. 5. **Output:** - `StdOut.println(mySum);`: After the loop completes, the total sum of all even numbers from 1000 to 0 is printed. This program demonstrates basic looping and conditional logic in Java.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
# Java Program Q1b: Adding Positive Integers

## Task Description
Complete the program `Q1b` that reads a set of integer values into the integer array `numbers` (this part is already done for you). Your task is to add up the positive values in the `numbers` array until the program encounters the value `999`. The number `999` is not included in the sum. At that point, the program should print the sum of the positive numbers and their average.

### Example 1
When the input is: `java Q1b 5 -3 8 999 7 1`

- The program outputs:
  - Sum: `13`
  - Average: `6.5`

### Example 2
When the input is: `java Q1b 5 -3 8 7 1`

- The program outputs:
  - Sum: `21`
  - Average: `5.25`

## Java Class Code

```java
public class Q1b {

    public static void main (String[] args) {

        int[] numbers = new int[args.length];

        for (int i = 0; i < args.length; i++) {
            numbers[i] = Integer.parseInt(args[i]);
        }

        // WRITE YOUR CODE HERE
    }
}
```

### Instructions
- Initiate a loop through the `numbers` array.
- Check each element; if it's positive, add it to a running total.
- Stop processing when you encounter the number `999`.
- Calculate and output the sum and average of the positive numbers encountered before `999`.

This task helps reinforce the implementation of basic loops, conditional statements, and arithmetic calculations in Java.
Transcribed Image Text:# Java Program Q1b: Adding Positive Integers ## Task Description Complete the program `Q1b` that reads a set of integer values into the integer array `numbers` (this part is already done for you). Your task is to add up the positive values in the `numbers` array until the program encounters the value `999`. The number `999` is not included in the sum. At that point, the program should print the sum of the positive numbers and their average. ### Example 1 When the input is: `java Q1b 5 -3 8 999 7 1` - The program outputs: - Sum: `13` - Average: `6.5` ### Example 2 When the input is: `java Q1b 5 -3 8 7 1` - The program outputs: - Sum: `21` - Average: `5.25` ## Java Class Code ```java public class Q1b { public static void main (String[] args) { int[] numbers = new int[args.length]; for (int i = 0; i < args.length; i++) { numbers[i] = Integer.parseInt(args[i]); } // WRITE YOUR CODE HERE } } ``` ### Instructions - Initiate a loop through the `numbers` array. - Check each element; if it's positive, add it to a running total. - Stop processing when you encounter the number `999`. - Calculate and output the sum and average of the positive numbers encountered before `999`. This task helps reinforce the implementation of basic loops, conditional statements, and arithmetic calculations in Java.
```java
public class Q1a {
    public static void main (String[] args) {

        int n = 1000;
        int mySum = 0;

        for (int i = n; i >= 0; i--) {
            if (i % 2 == 0) {
                mySum += i;
            }
        }

        StdOut.println(mySum);
    }
}
```

**Explanation:**

This Java program calculates the sum of all even numbers from 1000 down to 0. The program is structured as follows:

1. **Initialization:**
   - `int n = 1000;`: This variable `n` is set to 1000, representing the starting number.
   - `int mySum = 0;`: This variable `mySum` is initialized to 0 and will store the cumulative sum of even numbers.

2. **For Loop:**
   - `for (int i = n; i >= 0; i--)`: This loop iterates from `n` (1000) down to 0, decrementing `i` by 1 in each iteration.

3. **Condition Check:**
   - `if (i % 2 == 0)`: Inside the loop, an `if` statement checks if `i` is even. This is done by checking if the remainder when `i` is divided by 2 is zero.

4. **Summing Evens:**
   - `mySum += i;`: If `i` is even, it is added to `mySum`.

5. **Output:**
   - `StdOut.println(mySum);`: After the loop completes, the total sum of all even numbers from 1000 to 0 is printed.

This program demonstrates basic looping and conditional logic in Java.
Transcribed Image Text:```java public class Q1a { public static void main (String[] args) { int n = 1000; int mySum = 0; for (int i = n; i >= 0; i--) { if (i % 2 == 0) { mySum += i; } } StdOut.println(mySum); } } ``` **Explanation:** This Java program calculates the sum of all even numbers from 1000 down to 0. The program is structured as follows: 1. **Initialization:** - `int n = 1000;`: This variable `n` is set to 1000, representing the starting number. - `int mySum = 0;`: This variable `mySum` is initialized to 0 and will store the cumulative sum of even numbers. 2. **For Loop:** - `for (int i = n; i >= 0; i--)`: This loop iterates from `n` (1000) down to 0, decrementing `i` by 1 in each iteration. 3. **Condition Check:** - `if (i % 2 == 0)`: Inside the loop, an `if` statement checks if `i` is even. This is done by checking if the remainder when `i` is divided by 2 is zero. 4. **Summing Evens:** - `mySum += i;`: If `i` is even, it is added to `mySum`. 5. **Output:** - `StdOut.println(mySum);`: After the loop completes, the total sum of all even numbers from 1000 to 0 is printed. This program demonstrates basic looping and conditional logic in Java.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education