Here are the instructions: Program Name: AverageOfThree 1) Open AverageOfThree.j
ID: 3628154 • Letter: H
Question
Here are the instructions:Program Name: AverageOfThree
1) Open AverageOfThree.java. It’s output must be in the exact format as below (notice that the sum has no decimal point and the average always has at least 1 decimal value)
*First number is the line number, not part of the actual output*
1 This program calculates the sum and average of three integers.
2 Enter the first number: 5
3 Enter the second number: 5
4 Enter the third number: 5
5
6 The sum of your input is 15
7 The average of your input is 5.0
* There is a tab with no spaces on lines 2, 3, and 4 before the word Enter *
2) It is important to test your program with multiple inputs. You should also try values 10, 5, and 7 (calculate the average using a calculator and not the interactions pane) and any others that you think might identify failures in the program.
HERE IS THE PROGRAM WE ARE GIVEN TO START WITH (MUST BE FIXED TO ABOVE SPECIFICATIONS)
import java.util.Scanner;
/**
* Calculates the sum and average of three inputs via standard I/O.
*
* @author John Jones
* @version 8/30/11
*/
public class AverageOfThree {
/**
* Calculates the sum and average of three inputs via standard I/O.
*
* @param args User-defined command line arguments (not used).
*/
public static void main(String[] args) {
// do not change variable types
int sum = 0;
Scanner numberInput = new Scanner(System.in);
System.out.println("This program calculates the sum and average "
+ "of three integers.");
// get numerical input from user
System.out.print("Enter the first number: ");
sum+= numberInput.nextInt();
System.out.print("Enter the second number: ");
sum = numberInput.nextInt();
System.out.print("Enter the third number: ");
sum = numberInput.nextInt();
// display sum
System.out.println(" The sum of your input is " + sum);
//display average
System.out.println("The average of your input is " + sum / 3);
}
}
I would appreciate anyone's help here as I am totally stuck...
Explanation / Answer
// working perfectly now !! import java.util.Scanner; /** * Calculates the sum and average of three inputs via standard I/O. * * @author John Jones * @version 8/30/11 */ public class AverageOfThree { /** * Calculates the sum and average of three inputs via standard I/O. * * @param args User-defined command line arguments (not used). */ public static void main(String[] args) { // do not change variable types int sum = 0; Scanner numberInput = new Scanner(System.in); System.out.println("1.This program calculates the sum and average " + "of three integers."); // get numerical input from user System.out.println("2.Enter the first number: "); sum= sum + numberInput.nextInt(); System.out.println("3. Enter the second number: "); sum = sum +numberInput.nextInt(); System.out.println("4.Enter the third number: "); sum = sum + numberInput.nextInt(); System.out.println(); // display sum System.out.println("5. The sum of your input is " + sum); //display average System.out.println("6. The average of your input is " + sum / 3); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.