Глава 6
Задача 6.10 "Из какой страны судья?"
В примере 6.2 вместо номеров судей мы хотели бы выводить названия стран, из которых они прибыли. Используйте массив для создания перечня стран, представители которых занимаются судейством, и модифицируйте программу так, чтобы ее вывод был более информативным. Подумайте что лучше - иметь в классе Dive массив названий стран вместе с массивом выставленных баллов, или создать другой класс, включающий названия стран и баллы?
Решение
Создадим внутри класса Dive класс Judge (судьи), который будет содержать имя судьи, страну из которой он прибыл и оценку.
class Judge{
В классе Dive создадим массив судей j[] и инициализируем его нулевыми значениями и пустыми строками.
private int noOfJudges;//number of Judges
Judge j[]; //array of judges
Dive (int n) {
void setJudge (int score, String country, String name, int index) {
void assessJudge() {
Код программы:
import java.io.*;
class Dive {
int minJudge, maxJudge, minScore, maxScore, sum;
private int noOfJudges;//number of Judges
Judge j[]; //array of judges
Dive (int n) {
noOfJudges = n;
j = new Judge[noOfJudges];
for(int i = 0; i < noOfJudges;i++){
j[i] = new Judge("","",0);
}
}
void setJudge (int score, String country, String name, int index) {
j[index].score = score;
j[index].name = name;
j[index].country = country;
}
void assessJudge() {
minScore = 10;
maxScore = 0;
sum = 0;
for (int i = 0; i < noOfJudges; i++) {
sum += j[i].score;
if (j[i].score <= minScore) {
minScore = j[i].score;
minJudge = i;
}
if (j[i].score > maxScore) {
maxScore = j[i].score;
maxJudge = i;
}
}
minJudge++;
maxJudge++;
}
class Judge{
int score;
String name;
String country;
Judge(String n, String c, int s){
score = s;
name = n;
country = c;
}
}
}
import java.io.*;
import javagently.*;
class DivingCompetition {
Display d = new Display ("Dving Competition");
int noOfDives = 3;//number of dives
int noOfJudges = 4;//numbeer of judges
DivingCompetition () throws IOException {
String country[] = {"Italy","Germany","Russia","Israel"};
String name[] = {"Pavarotti","Backenbauer","Sokolov","Guberman"};
headings (country);
double result, total = 0;
Dive dive = new Dive (noOfJudges);
int score;
for (int i=0; i < noOfDives; i++) {
d.println("Dive no: "+(i+1));
d.println("Enter the scores for the judges");
d.ready("Press ready to continue");
for (int j = 0; j < noOfJudges; j++) {
score = d.getInt("Judge "+ country[j]);
dive.setJudge(score,country[j],name[j],j);
}//end for
dive.assessJudge();
result = (double) (dive.sum
- dive.minScore - dive.maxScore)
/ (double) (noOfJudges - 2);
total += result;
d.println("Scores " + dive.minScore + " judge "+ dive.j[dive.minJudge - 1].name + " from "
+ dive.j[dive.minJudge - 1].country + "\n and " + dive.maxScore +
" judge "+ dive.j[dive.maxJudge - 1].name + " from "
+ dive.j[dive.maxJudge - 1].country + " excluded.");
d.println("Result is: " + Text.writeDouble(result,5,1));
}
d.println("Diving average is : "+ Text.writeDouble(total/noOfDives,5,3));
}
void headings (String country[]) {
d.println ("Diving Score Calculator\n" +
"=======================\n");
d.println("For each of " + noOfDives +
" dives give scores for "+noOfJudges+" judges.");
for (int i = 1; i<=noOfJudges; i++)
d.prompt ("Judge "+ country[i-1],0);
}
public static void main (String [] args) throws IOException {
new DivingCompetition ();
}
}
Результат :
Назад |
Начало урока |
Вверх |
Вперед
Содержание