CSE142 Sample Programs handout #10
Log of Execution for Athletes Program
-------------------------------------
This program reads in a series of judges' scores
for two different athletes and computes an
average score for each.
Enter next athlete information:
judge #1 score? 8.8
judge #2 score? 9.2
judge #3 score? 8.5
judge #4 score? 9.7
judge #5 score? 8.4
Enter next athlete information:
judge #1 score? 9.2
judge #2 score? 8.2
judge #3 score? 8.6
judge #4 score? 8.8
judge #5 score? 8.0
Athlete #1 average score = 8.92
Athlete #2 average score = 8.559999999999999
Unstructured version Judge1.java (not a good model to follow)
-------------------------------------------------------------
// Stuart Reges
// 10/18/04
//
// This program finds the average score received by each of two athletes.
import java.util.*;
public class Judge1 {
public static void main(String[] args) {
System.out.println("This program reads in a series of judges' scores");
System.out.println("for two different athletes and computes an");
System.out.println("average score for each.");
System.out.println();
Scanner console = new Scanner(System.in);
System.out.println("Enter next athlete information:");
double total = 0;
for (int i = 1; i <= 5; i++) {
System.out.print(" judge #" + i + " score? ");
double score = console.nextDouble();
total += score;
}
System.out.println();
double score1 = total/5;
System.out.println("Enter next athlete information:");
total = 0;
for (int i = 1; i <= 5; i++) {
System.out.print(" judge #" + i + " score? ");
double score = console.nextDouble();
total += score;
}
System.out.println();
double score2 = total/5;
System.out.println("Athlete #1 average score = " + score1);
System.out.println("Athlete #2 average score = " + score2);
}
}
Structured version Judge2.java (follow this model)
--------------------------------------------------
// Stuart Reges
// 10/18/04
//
// This program finds the average score received by each of two athletes.
import java.util.*;
public class Judge2 {
public static final int NUM_JUDGES = 5;
public static void main(String[] args) {
giveIntro();
Scanner console = new Scanner(System.in);
double score1 = getScore(console);
double score2 = getScore(console);
reportResults(score1, score2);
}
// introduces the program to the user
public static void giveIntro() {
System.out.println("This program reads in a series of judges' scores");
System.out.println("for two different athletes and computes an");
System.out.println("average score for each.");
System.out.println();
}
// prompts for one athlete's scores, returning the average score
public static double getScore(Scanner console) {
System.out.println("Enter next athlete information:");
double total = 0;
for (int i = 1; i <= NUM_JUDGES; i++) {
System.out.print(" judge #" + i + " score? ");
double score = console.nextDouble();
total += score;
}
System.out.println();
return total/NUM_JUDGES;
}
// reports the overall athlete scores to the user
public static void reportResults(double score1, double score2) {
System.out.println("Athlete #1 average score = " + score1);
System.out.println("Athlete #2 average score = " + score2);
}
}