Following on from
plinden and for illustration purposes, below is a competitor (athlete).
A competitor has a name, a number of scores, an average score a min score and a max score.
Code:
public class Competitor implements Comparable<Competitor>
{
private String name;
private double[] scores;
private double averageScore;
private double minScore;
private double maxScore;
public Competitor()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void setScores(double[] scores)
{
this.scores = scores;
averageScore = calculateTotalScore() / scores.length;
// work out the max/min scores
minScore = scores[0];
for(int i = 0; i < scores.length; i++)
{
if(scores[i] < minScore)
{
minScore = scores[i];
}
if(scores[i] > maxScore)
{
maxScore = scores[i];
}
}
}
public double getAvergeScore()
{
return averageScore;
}
private double calculateTotalScore()
{
double sum = 0;
for (int i = 0; i < scores.length; i++)
{
sum += scores[i];
}
return sum;
}
public String toString()
{
return name + "'s lowest score is: "+ minScore + "\n" +
name + "'s highest score is: "+ maxScore + "\n" +
name + "'s average is: " + averageScore;
}
public int compareTo(Competitor thatCompetitor)
{
if(thatCompetitor.getAvergeScore() > getAvergeScore())
{
// that competitor has a higher score
return 1;
}
if(thatCompetitor.getAvergeScore() < getAvergeScore())
{
// this competitor has a higher score
return -1;
}
// scores are equal
return 0;
}
}
Each GymCompetition then contains an array (list) of competitors.
The competitor is
responsible for knowing its own name.
The competitor is also
responsible for keeping its own score.
You can then greatly simplify your main method.
Code:
public static void main(String[] args)
{
GymCompetition competition = new GymCompetition(3);
competition.readCompetiorsNames();
competition.readCompetiorsScores();
competition.printResults();
}
All it does is this:
[1] Sets up a competition with 3x athletes (Competitor).
[2] Reads in their names (as you code was doing before).
[3] Reads in their scores (as you code was doing before).
Here's the good bit:
[4] It then loops through each competitor and asks them to print what they scored (their min, max and average).
As a final feature, the competitors are sorted based on their score. The competitor with the highest score will now the at index 0 in the array. This athlete is the winner.
It's not perfect and there are some improvements which could be made - but I hope it give you some ideas.
Code:
import java.util.Scanner;
import java.util.Arrays;
public class GymCompetition
{
private Scanner console;
private Competitor[] comepetitors;
public GymCompetition(int competitorCount)
{
console = new Scanner(System.in);
comepetitors = new Competitor[competitorCount];
for(int i = 0; i < comepetitors.length; i++)
{
// create all of the competitors
comepetitors[i] = new Competitor();
}
}
public static void main(String[] args)
{
GymCompetition competition = new GymCompetition(3);
competition.readCompetiorsNames();
competition.readCompetiorsScores();
competition.printResults();
}
public void printResults()
{
for(int i = 0; i < comepetitors.length; i++)
{
System.out.println(comepetitors[i]);
}
// sort from highest -> lowest
Arrays.sort(comepetitors);
System.out.println(comepetitors[0].getName() + " is the winner!");
}
public void readCompetiorsNames()
{
System.out.println("Enter the names of the contestants.");
for(int i = 0; i < comepetitors.length; i++)
{
comepetitors[i].setName(console.next().trim());
}
}
public void readCompetiorsScores()
{
for(int i = 0; i < comepetitors.length; i++)
{
Competitor thisComepetitor = comepetitors[i];
System.out.println("Enter the judges scores for "+ thisComepetitor.getName());
double[] scores = new double[8];
for(int j = 0 ; j < scores.length; j++)
{
scores[j] = console.nextDouble();
}
thisComepetitor.setScores(scores);
}
}
}