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

I have a question about chp 7 pp1 Write a program that reads integers, one per l

ID: 3656940 • Letter: I

Question

I have a question about chp 7 pp1

Write a program that reads integers, one per line, and displays their sum. Also, display all the numbers read, each with an annotation giving its percentage contribution to the sum. Use a method that takes the entire array as one argument and returns the sum of the numbers in the array. (Hint: Ask the user for the number of integers to be entered, create an array of that length, and then fill the array with the integers read. A possible dialogue between the program and the user follows:

Explanation / Answer

import java.util.Scanner;


public class IntegerStatistics {

public static void main(String[] args) {

Scanner kb = new Scanner(System.in);


System.out.println("How many numbers will you enter?");

int k = kb.nextInt();

int[] integers = new int[k];

System.out.println("Enter 4 integers, one per line:");

for (int i = 0; i < k; i++) {

integers[i] = kb.nextInt();

}


int sum = calcSum(integers);

System.out.println(String.format("The sum is %d", sum));

double percent;

int number;

System.out.println("The numbers are:");

for (int i = 0; i < k; i++) {

number = integers[i];

percent = number / (double) sum * 100.00;

System.out.println(String.format("%d, which is %.4f%% of the sum.", number, percent));

}

}


public static int calcSum(int[] array) {

int sum = 0;


for (int i = 0; i < array.length; i++) {

sum += array[i];

}
return sum;

}

}