Winter olympic program Create a program that stores 6 competitor names for 10 wi
ID: 656140 • Letter: W
Question
Winter olympic program
Create a program that stores 6 competitor names for 10 winter olympic events. The program should be able to accept time and scores. The top three in each event should be given a bronze, silver, or gold medal. A sort that goes from the dop down should be used for the top 3 scores of the events that are scored . For the time events the sort will will need to go from the lowest time to the highest time for gold, silver, and bronze. It should sort the scores or times of the events showing the medal winners only with the medal they won.
The scored events are ice hockey, aerials, curling, short half-pipe, half-pipe, figure skating, ice dancing, and pairs figure skating. The timed events are the bobsled and speed skating,
After the console program is done. Write a gui that has a drop down box in the middle that is labeled Olympic events. The event will need to be selected and then the corresponding medals for that event will show up on the bottom of the drop down list. Thanks for your help. 9 events can be hard coded in but for testing purposes 1 event should be able to be input.
Explanation / Answer
Complete Program:
// File: Competitor.java
public class Competitor
{
private String name;
private int[] scores;
private int[] times;
public Competitor(String cname)
{
name = cname;
scores = new int[8];
times = new int[2];
}
public void setScore(int index, int score)
{
if(index >= 0 && index < 8)
scores[index] = score;
}
public void setTime(int index, int time)
{
if(index >= 0 && index < 2)
times[index] = time;
}
public String getName()
{
return name;
}
public int getScore(int index)
{
if(index >= 0 && index < 8)
return scores[index];
return -1;
}
public int getTime(int index)
{
if(index >= 0 && index < 2)
return times[index];
return -1;
}
}
-----------------------------------------------------------------------------------
// File: CompetitorTest.java
import java.util.Scanner;
public class CompetitorTest
{
public static void main(String[] args)
{
final int NAMES = 6;
final int SCORES = 8;
final int TIMES = 2;
Competitor[] competitors = new Competitor[NAMES];
Scanner input = new Scanner(System.in);
String[] scoredEvents = {"Ice hockey", "Aerials", "Curling", "Short half-pipe", "Half-pipe", "Figure skating", "Ice dancing", "Pairs figure skating"};
String[] timedEvents = {"Bobsled", "Speed skating"};
String[] medals = {"Gold", "Silver", "Bronze"};
// Competitor names
String[] cnames = {"John", "Mike", "Steve", "Chars", "Smith", "James"};
int[] cs1 = {5, 4, 3, 7, 8, 2, 6, 8}; // Competitor 1 scores
int[] cs2 = {8, 2, 4, 3, 7, 6, 8, 5}; // Competitor 2 scores
int[] cs3 = {9, 8, 2, 6, 9, 4, 3, 6}; // Competitor 3 scores
int[] cs4 = {2, 6, 6, 8, 5, 2, 9, 3}; // Competitor 4 scores
int[] cs5 = {3, 9, 7, 5, 4, 7, 3, 9}; // Competitor 5 scores
int[] cs6 = {4, 5, 8, 2, 5, 9, 7, 7}; // Competitor 6 scores
int[] ct1 = {26, 38}; // Competitor 1 times
int[] ct2 = {18, 45}; // Competitor 2 times
int[] ct3 = {63, 36}; // Competitor 3 times
int[] ct4 = {29, 63}; // Competitor 4 times
int[] ct5 = {19, 19}; // Competitor 5 times
int[] ct6 = {81, 17}; // Competitor 6 times
setData(competitors, 0, cnames[0], cs1, ct1);
setData(competitors, 1, cnames[1], cs2, ct2);
setData(competitors, 2, cnames[2], cs3, ct3);
setData(competitors, 3, cnames[3], cs4, ct4);
setData(competitors, 4, cnames[4], cs5, ct5);
setData(competitors, 5, cnames[5], cs6, ct6);
for(int i = 0; i < SCORES; i++)
displayResultsForScoredGame(competitors, i, scoredEvents[i], medals);
for(int i = 0; i < TIMES; i++)
displayResultsForScoredGame(competitors, i, timedEvents[i], medals);
}
public static void setData(Competitor[] competitors, int index, String cname, int[] cs, int[] ct)
{
competitors[index] = new Competitor(cname);
for(int i = 0; i < cs.length; i++)
competitors[index].setScore(i, cs[i]);
for(int j = 0; j < ct.length; j++)
competitors[index].setTime(j, ct[j]);
}
public static void displayResultsForScoredGame(Competitor[] competitors, int index, String event, String[] medals)
{
sortScores(competitors, index);
System.out.println("Event: " + event);
System.out.println(medals[0] + ": " + competitors[0].getName());
System.out.println(medals[1] + ": " + competitors[1].getName());
System.out.println(medals[2] + ": " + competitors[2].getName());
System.out.println();
}
public static void sortScores(Competitor[] competitors, int index)
{
for(int i = 0; i < competitors.length - 1; i++)
{
int minPos = i;
for(int j = i + 1; j < competitors.length; j++)
{
if(competitors[j].getScore(index) > competitors[minPos].getScore(index))
{
minPos = j;
}
}
if(minPos != i)
{
Competitor temp = competitors[minPos];
competitors[minPos] = competitors[i];
competitors[i] = temp;
}
}
}
public static void displayResultsForTimedGame(Competitor[] competitors, int index, String event, String[] medals)
{
sortTimes(competitors, index);
System.out.println("Event: " + event);
System.out.println(medals[0] + ": " + competitors[0].getName() + " " + competitors[0].getTime(index));
System.out.println(medals[1] + ": " + competitors[1].getName() + " " + competitors[1].getScore(index));
System.out.println(medals[2] + ": " + competitors[2].getName() + " " + competitors[2].getScore(index));
System.out.println();
}
public static void sortTimes(Competitor[] competitors, int index)
{
for(int i = 0; i < competitors.length - 1; i++)
{
int minPos = i;
for(int j = i + 1; j < competitors.length; j++)
{
if(competitors[j].getTime(index) < competitors[minPos].getTime(index))
{
minPos = j;
}
}
if(minPos != i)
{
Competitor temp = competitors[minPos];
competitors[minPos] = competitors[i];
competitors[i] = temp;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.