Tuesday, February 11, 2014

avgApp

Programming Fundamentals
CS1336

Assignment #3 - Basic Calculations Continued

Program #1 – Calculating Averages

For this problem, please implement Problem 3 on page 143 of the text. A scan of the problem is provided below.

In this problem, the program should obtain five test scores from the user. As always, the program should print a prompt before it gets the user input from the keyboard. You can either print out five separate prompts or one prompt, but there should be enough information in the prompt so that it is clear to the user what he/she needs to do. Your program will then calculate the average of the five scores and print out the result.

Please print out the result to one decimal place. Your output can look like:

The average of these test scores is: 94.5.


import java.util.Scanner;
import java.text.*;
 
public class Assignment3 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
 
  double test;
  DecimalFormat df = new DecimalFormat("#.#");
  System.out
    .println("In this program you will find the average of 5 test scores...");
  System.out.println("");
 
  double test1;
  double test2;
  double test3;
  double test4;
  double test5;
 
  System.out.print("Please enter test score 1: ");
  test1 = input.nextDouble();
 
  System.out.print("Please enter test score 2: ");
  test2 = input.nextDouble();
 
  System.out.print("Please enter test score 3: ");
  test3 = input.nextDouble();
 
  System.out.print("Please enter test score 4: ");
  test4 = input.nextDouble();
 
  System.out.print("Please enter test score 5: ");
  test5 = input.nextDouble();
 
  double average = (test1 + test2 + test3 + test4 + test5) / 5;
  System.out.println("Your average test score is: "+ (df.format(average)));
 
 }
 
}

No comments:

Post a Comment