Java Project The bold line needs to be formatted to match the procedure instruct
ID: 3755451 • Letter: J
Question
Java Project
The bold line needs to be formatted to match the procedure instructions
Procedure
1. Output the count in a two column decimal format (%2d)
2. Output the integer in a 5 column decimal format (%5d)
3. Output the running total in a 15 column decimal format (%15d)
4. Remember to change your println to printf to allow formatting
5. Add a blank println statement after the printf, so that a new line is issued
6. Add a declaration for a double variable and use it to hold the calculated average
7. Be sure to cast your average calculate as a double so that a double value is generated
8. Output the average using a format of 15 characters, 2 decimal places (%15.2f)
PROJECT CODE HERE:
/*
*This program will prompt for a list of numbers and add each number to a running total
* sentinel controlled loop
*/
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// instantiate scanner object to read input
Scanner input = new Scanner(System.in);
final int ENDRUN = -99; // set up value to end run
int sum = 0; // initialize running total to zero
int count = 0; // initialize count of integers to zero
System.out.println("Enter a valid integer"); // prompt for integer
System.out.print("Enter " + ENDRUN + " to end: "); // reminder about sentinel value
int number = input.nextInt(); // use scanner to read an integer - priming read
while (number != ENDRUN) { // if number was not sentinel
sum += number; // add it to the running total
count++;
System.out.println("Added " + number + " to sum. Running total is now " + sum); // output the number and running total
System.out.println("Enter a valid integer");
System.out.print("Enter " + ENDRUN + " to end: ");
number = input.nextInt(); // read next number or sentinel
} // end of while
System.out.println("The average of the entered numbers is: " + (sum/count));
} // end of main method
} // end of Main class
Explanation / Answer
Here is the required code formatted according to the procedure instructions. In case of any queries write in the comments.
=============================================================================
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// instantiate scanner object to read input
Scanner input = new Scanner(System.in);
final int ENDRUN = -99; // set up value to end run
int sum = 0; // initialize running total to zero
int count = 0; // initialize count of integers to zero
double avg = 0; //introduce a double variable avg to store average
System.out.println("Enter a valid integer"); // prompt for integer
System.out.print("Enter " + ENDRUN + " to end: "); // reminder about sentinel value
int number = input.nextInt(); // use scanner to read an integer - priming read
while (number != ENDRUN) { // if number was not sentinel
sum += number; // add it to the running total
count++;
System.out.printf(" %2d. Added %5d to sum. Running total is now %15d" , count, number, sum); // output the number and running total
System.out.println(); //Add a newline statement since printf doesn't print newline
System.out.println("Enter a valid integer");
System.out.print("Enter " + ENDRUN + " to end: ");
number = input.nextInt(); // read next number or sentinel
} // end of while
avg = sum/count; //store average in double variable avg
System.out.printf("The average of the entered numbers is: %15.2f" , avg); //formatted output of average
} // end of main method
} // end of Main class
============================================================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.