fork download
  1.  
  2. /*-------------------------------------------------------------------------
  3. // AUTHOR: your name
  4. // FILENAME: Lab2.java
  5. // SPECIFICATION: Program to Calculate average of 3 test scores
  6. // FOR: CSE 110- Lab #2
  7. // TIME SPENT: 20 minutes
  8. //-----------------------------------------------------------*/
  9. /*Step 1: Setting up a Scanner for Input*/
  10. import java.util.Scanner;
  11.  
  12. class Lab2 {
  13.  
  14. public static void main(String[] args) {
  15. /*Since you are required to read in the three test grades from the user,
  16.   you will have to use a Scanner object.*/
  17. Scanner in = new Scanner(System.in);
  18. /*Step 2: Declaring Variables*/
  19. //Declare three int variables to hold the three test grades
  20. int test1, test2, test3;
  21. //Declare an int constant to hold the value 3
  22. final int NUM_TESTS = 3;
  23. //declare a double variable to hold the average.
  24. double average;
  25.  
  26. /*Step 3: Getting the Input*/
  27.  
  28. //Write the prompt and read the input for all three tests.
  29. System.out.print("Enter the score on the first test: "); // prompt
  30. test1 = in.nextInt(); // read in the next integer
  31.  
  32. System.out.print("Enter the score on the second test: "); // prompt
  33. test2 = in.nextInt(); // read in the next integer
  34.  
  35. System.out.print("Enter the score on the third test: "); // prompt
  36. test3 = in.nextInt(); // read in the next integer
  37.  
  38. /*Step 4: Calculate the Average*/
  39. average = ((double) test1 + test2 + test3) / NUM_TESTS;
  40.  
  41. /*Step 5: Display the average*/
  42. System.out.println("Average of test scores: " + average);
  43. }
  44. }
  45.  
  46.  
Success #stdin #stdout 0.07s 4386816KB
stdin
90
90
92
stdout
Enter the score on the first test: Enter the score on the second test: Enter the score on the third test: Average of test scores: 90.66666666666667