Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a java program. The winter olympic paragraph just describes the problem.

ID: 656439 • Letter: T

Question

This is a java program.

The winter olympic paragraph just describes the problem.

The winter olympic console part is done. There is a second part where the output of the console side is written to a file and then read by the gui class which is called medal standings. The output of olympicstest should write to a file then the gui class will read the file and then populate the gui drop down box with the data from the olympic test. The gold winners in each event should show up in the gui when the event is clicked on. I am having trouble with getting the file output to write to a file. Thank you.

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.

The second part of this assignment is to have the output of the winter olympics file printed to a text file. The gui class then reads the file into an array or some type of object and that object will then be able to populate the set text parts of the drop down box with the output of the console side of the program.

Program below.

Main file

package winterolympics1;

import java.io.*;
import java.util.Scanner;
public class WinterOlympics1Test
{
public static void main(String[] args) throws IOException{
{


//We need 6 names 8 scores and 2 times
//1 competitor will be input to test program.


final int NAMES = 6;
final int SCORES = 8;
final int TIMES = 2;
  
WinterOlympics1[] competitors = new WinterOlympics1[NAMES];
Scanner input = new Scanner(System.in);
  
//Events for the olympics below.
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 scores and times below.   
String[] cnames = {"John", "Mike", "Steve", "Chars", "Smith", "James"};
int[] cs1 = new int[8];
for(int i = 0; i < 8; i++){
System.out.print("Please enter Score for John: ");
cs1[i] = input.nextInt();
}
// Competitor 1 scores will be input by user.
  
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
//Competitor 1 times will be input by user.

int[] ct1 = new int[2];
for(int i = 0; i < 2; i++){
System.out.print("Please enter time for John: ");
ct1[i] = input.nextInt();
}

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
//Sets data to a chart below.
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 loops for displaying proper output.
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);
}
}
  
//Stores data for later use for both scores and times.
public static void setData(WinterOlympics1[] competitors, int index, String cname, int[] cs, int[] ct)
{
competitors[index] = new WinterOlympics1(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]);
}
//Results will be sorted and output as shown below with the medals and names only being shown for scored events.
public static void displayResultsForScoredGame(WinterOlympics1[] 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();

}
// Sorts scores from highest to lowest.
public static void sortScores(WinterOlympics1[] 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)
{
WinterOlympics1 temp = competitors[minPos];
competitors[minPos] = competitors[i];
competitors[i] = temp;
}
}
}
//Results will be sorted and output as shown below for timed games.
public static void displayResultsForTimedGame(WinterOlympics1[] competitors, int index, String event, String[] medals)
{
sortTimes(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();
}
// Sorts scores from lowest to highest.
public static void sortTimes(WinterOlympics1[] 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)
{
WinterOlympics1 temp = competitors[minPos];
competitors[minPos] = competitors[i];
competitors[i] = temp;
}
}
  
}
}

Class file

package winterolympics1;

/**
*
* @author chapmanr
*/
public class WinterOlympics1
// String Name, int array for scores and int array for times.
{
private String name;
private int[] scores;
private int[] times;
  
public WinterOlympics1(String cname)
{// Competitor name, scores for 8 events, times for 2 events.
name = cname;
scores = new int[8];
times = new int[2];
}
  
public void setScore(int index, int score)
{// Index is between 0 and 7.
if(index >= 0 && index < 8)
scores[index] = score;
}
  
public void setTime(int index, int time)
{// index is between 0 and 1 for Time results.
if(index >= 0 && index < 2)
times[index] = time;
}
  
public String getName()
{
return name;
}
  
public int getScore(int index)
{
// Returns scores the scores array.
if(index >= 0 && index < 8)
return scores[index];
  
return -1;
}
  
public int getTime(int index)
{
// Returns scores for the times array.
if(index >= 0 && index < 2)
return times[index];
  
return -1;
}
}

class for gui for data to be serialized to.

package winterolympics1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MedalStanding implements ActionListener{
   JFrame frame;
   JPanel contentPane;
   JComboBox eventNames;
   JLabel eventListPrompt, medalResult;
  
public MedalStanding(){
  
/* Create and set up the frame */
frame = new JFrame("Event Results");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* Create a content pane with a BoxLayout and empty borders */
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 200));
  
       /* Create a combo box and a descriptive label */
   eventListPrompt = new JLabel("Select an event name: ");
   eventListPrompt.setAlignmentX(JLabel.LEFT_ALIGNMENT);
   contentPane.add(eventListPrompt);
  
   String[] names = {"Ice hockey", "Aerials", "Curling", "Short half-pipe", "Half-pipe", "Figure skating", "Ice dancing", "Pairs figure skating", "Bobsled", "Speed skating"};
   eventNames = new JComboBox(names);
   eventNames.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
   eventNames.setSelectedIndex(0);
   eventNames.addActionListener(this);
   contentPane.add(eventNames);

       /* Create and add a label that will display the Latin names */
   medalResult = new JLabel("Olympic Results"
+ " "
+ " "
+ " ");
   medalResult.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
   contentPane.add(medalResult);

       /* Add content pane to frame */
frame.setContentPane(contentPane);

/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
  
  
   /**
   * Handle a selection from the combo box
   * pre: none
   * post: The for the selected event has been displayed.
   */

  
  
public void actionPerformed(ActionEvent event) {
  
       JComboBox comboBox = (JComboBox)event.getSource();
       String eventName = (String)comboBox.getSelectedItem();
      
  
       if (eventName == "Ice hockey") {
           medalResult.setText("<html>Gold: Steve<br>" +
"Silver: Mike <br>" +
"Bronze: John</html>");
       } else if (eventName == "Aerials") {
           medalResult.setText("<html>Gold: Smith<br>" +
"Silver: Steve <br>" +
"Bronze: Chars</html>");
       } else if (eventName == "Curling") {
           medalResult.setText("<html>Gold: James <br>" +
"Silver: Smith <br>" +
"Bronze: Chars</html>");
       } else if (eventName == "Short half-pipe") {
           medalResult.setText("<html>Gold: Chars <br> " +
"Silver: John <br>" +
"Bronze: Steve <html>");
       } else if (eventName == "Half-pipe") {
           medalResult.setText(
"<html>Gold: Steve<br>" +
"Silver: John <br>" +
"Bronze: Mike</html>");
       } else if (eventName == "Figure skating") {
           medalResult.setText("" +
"<html>Gold: James <br>" +
"Silver: Smith <br>" +
"Bronze: Mike</html>" +
"");
       }else if (eventName == "Ice dancing") {
           medalResult.setText(
"<html>Gold: Chars <br>" +
"Silver: Mike <br>" +
"Bronze: James</html>" +
"");
       }else if (eventName == "Pairs figure skating") {
           medalResult.setText("" +
"<html>Gold: Smith <br>" +
"Silver: John <br>" +
"Bronze: James</html>");
       }else if (eventName == "Bobsled") {
           medalResult.setText("<html>Gold: Steve <br>"+
"Silver: Mike <br>"+
"Bronze: John</html>");
       }else if (eventName == "Speed skating") {
           medalResult.setText("<html>"+
"Gold: Smith <br>" +
"Silver: Steve <br>" +
"Bronze: Chars</html>");
       }
}

/**
* Create and show the GUI.
*/
private static void runGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);

MedalStanding winners = new MedalStanding();
}

public static void main(String[] args) {
/* Methods that create and show a GUI should be
run from an event-dispatching thread */
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
runGUI();
}
});
}
}

Explanation / Answer

package winterolympics1;

import java.io.*;
import java.util.Scanner;
public class WinterOlympics1Test
{
public static void main(String[] args) throws IOException
{
{
  
final int NAMES = 6;
final int SCORES = 8;
final int TIMES = 2;
  
WinterOlympics1[] competitors = new WinterOlympics1[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"};
  
String[] cnames = {"John", "Mike", "Steve", "Chars", "Smith", "James"};
int[] cs1 = new int[8];
for(int i = 0; i < 8; i++)
   {
System.out.print("Please enter Score for John: ");
cs1[i] = input.nextInt();
}
  
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 = new int[2];
for(int i = 0; i < 2; i++)
{
System.out.print("Please enter time for John: ");
ct1[i] = input.nextInt();
}

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(WinterOlympics1[] competitors, int index, String cname, int[] cs, int[] ct)
{
competitors[index] = new WinterOlympics1(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(WinterOlympics1[] 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(WinterOlympics1[] 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)
{
WinterOlympics1 temp = competitors[minPos];
competitors[minPos] = competitors[i];
competitors[i] = temp;
}
}
}

   public static void displayResultsForTimedGame(WinterOlympics1[] competitors, int index, String event, String[] medals)

{
sortTimes(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 sortTimes(WinterOlympics1[] 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)
{
WinterOlympics1 temp = competitors[minPos];
competitors[minPos] = competitors[i];
competitors[i] = temp;
}
}
  
}
}

// creating a Class file

package winterolympics1;

public class WinterOlympics1

// String Name, int array for scores and int array for times.

{
private String name;
private int[] scores;
private int[] times;
  
public WinterOlympics1(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)
{
// Returns scores for the times array.
if(index >= 0 && index < 2)
return times[index];
  
return -1;
}
}

// creating a GUI for winterolympic

package winterolympics1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class MedalStanding implements ActionListener

{
JFrame frame;
JPanel contentPane;
JComboBox eventNames;
JLabel eventListPrompt, medalResult;
  
public MedalStanding()
{
  
/* Create and set up the frame */
frame = new JFrame("Event Results");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* Create a content pane with a BoxLayout and empty borders */
contentPane = new JPanel();
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
contentPane.setBorder(BorderFactory.createEmptyBorder(25, 25, 25, 200));
  
/* Create a combo box and a descriptive label */
eventListPrompt = new JLabel("Select an event name: ");
eventListPrompt.setAlignmentX(JLabel.LEFT_ALIGNMENT);
contentPane.add(eventListPrompt);
  
String[] names = {"Ice hockey", "Aerials", "Curling", "Short half-pipe", "Half-pipe", "Figure skating", "Ice dancing", "Pairs figure skating", "Bobsled", "Speed skating"};
eventNames = new JComboBox(names);
eventNames.setAlignmentX(JComboBox.LEFT_ALIGNMENT);
eventNames.setSelectedIndex(0);
eventNames.addActionListener(this);
contentPane.add(eventNames);

/* Create and add a label that will display the Latin names */
medalResult = new JLabel("Olympic Results"
+ " "
+ " "
+ " ");
medalResult.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
contentPane.add(medalResult);

/* Add content pane to frame */
frame.setContentPane(contentPane);

/* Size and then display the frame. */
frame.pack();
frame.setVisible(true);
}
  

  
public void actionPerformed(ActionEvent event)
{
  
JComboBox comboBox = (JComboBox)event.getSource();
String eventName = (String)comboBox.getSelectedItem();
  
  
if (eventName == "Ice hockey")
    {
medalResult.setText("<html>Gold: Steve<br>" +
"Silver: Mike <br>" +
"Bronze: John</html>");
}

   else if (eventName == "Aerials")

   {
medalResult.setText("<html>Gold: Smith<br>" +
"Silver: Steve <br>" +
"Bronze: Chars</html>");
}

   else if (eventName == "Curling")

   {
medalResult.setText("<html>Gold: James <br>" +
"Silver: Smith <br>" +
"Bronze: Chars</html>");
}

   else if (eventName == "Short half-pipe")
   {
medalResult.setText("<html>Gold: Chars <br> " +
"Silver: John <br>" +
"Bronze: Steve <html>");
}

   else if (eventName == "Half-pipe")

   {
medalResult.setText(
"<html>Gold: Steve<br>" +
"Silver: John <br>" +
"Bronze: Mike</html>");
}   
  
   else if (eventName == "Figure skating")

   {
medalResult.setText("" +
"<html>Gold: James <br>" +
"Silver: Smith <br>" +
"Bronze: Mike</html>" +
"");
}

   else if (eventName == "Ice dancing")

   {
medalResult.setText(
"<html>Gold: Chars <br>" +
"Silver: Mike <br>" +
"Bronze: James</html>" +
"");
}

   else if (eventName == "Pairs figure skating")

   {
medalResult.setText("" +
"<html>Gold: Smith <br>" +
"Silver: John <br>" +
"Bronze: James</html>");
}

   else if (eventName == "Bobsled")

   {
medalResult.setText("<html>Gold: Steve <br>"+
"Silver: Mike <br>"+
"Bronze: John</html>");
}

   else if (eventName == "Speed skating")

   {
medalResult.setText("<html>"+
"Gold: Smith <br>" +
"Silver: Steve <br>" +
"Bronze: Chars</html>");
}
}

  

private static void runGUI()

   {

JFrame.setDefaultLookAndFeelDecorated(true);

MedalStanding winners = new MedalStanding();
   }

public static void main(String[] args)
  
{

   javax.swing.SwingUtilities.invokeLater(new Runnable()
  
   {
public void run()

   {
runGUI();
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote