Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSC 1311- Computer Programming II Pre-Course Assessment Caldwell Spring 2018 Pro

ID: 3878089 • Letter: C

Question

CSC 1311- Computer Programming II Pre-Course Assessment Caldwell Spring 2018 Problem: Develop a Java program to allow a user to enter to a series of numbers separated by space from the keyboard. The last number entered is always-1. Display the list of numbers entered then find and display the sum and average of all positive integers entered from the keyboard. Run your program using the following data: Run 1: 0,-2,4,-5,-1 Run 2: -1 Run 3: 4, 8, 10, 12,-5, 20,-1 Display the output for each sample run. Submit the following to blackboard for grading: Java program Sample output e

Explanation / Answer

ReadNosUntilMinus1.java

import java.util.Scanner;

public class ReadNosUntilMinus1 {

public static void main(String[] args) {

//Declaring variables

int num, count = 0, sum = 0;

double avg = 0;

String str = "";

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.print("Enter Numbers :");

num = sc.nextInt();

//This loop continues to execute until the user enter -1 as user input

while (num != -1) {

//calculating the sum

sum += num;

count++;

str += String.valueOf(num) + " ";

num = sc.nextInt();

}

str += String.valueOf(num);

//Displaying the user entered numbers

System.out.println("User entered Numbers are :" + str);

//Displaying the sum

System.out.println("Sum :" + sum);

//calculating the Average

avg = ((double) sum) / count;

//Displaying the Average

System.out.printf("Average :%.2f", avg);

}

}

______________________

Output#1:

Enter Numbers :0 -2 -4 -5 -1
User entered Numbers are :0 -2 -4 -5 -1
Sum :-11
Average :-2.75

_________________

Output#2:

Enter Numbers :-1
User entered Numbers are :-1
Sum :0
Average : 0

________________

Output#3:

Enter Numbers :4 8 10 12 -5 20 -1
User entered Numbers are :4 8 10 12 -5 20 -1
Sum :49
Average :8.17

_______________Could you plz rate me well.Thank You