Search code examples
javaarraysjoptionpane

Diving Competition - Java Program


I am working on a diving competition program that is similar to another problem on the site. The goal of the program is to ask the user to input the diver's name, the seven judges scores, and the difficulty rating of the dive. Once that information is input, I create a Diver object based on my user created Diver class. Then using the insertDiver method, I am supposed to be adding divers to an array of divers in the order such that the highest finalScore is first. I am not supposed to be sorting the array. Using JOptionPane showMessageDialog output, display the rankings in order from highest finalScore to the lowest finalScore. Since, I am passing an array object to the insertDiver method, the array is dead after completion of the method. I tried making the Diver[] array a global variable but those results were not good either. So bottom line, I don't know how to add the divers to the Divers array by highest finalScore and I don't know how to display the results either. Here is the code I have so far:

import javax.swing.JOptionPane;

public class DivingCompetition
{


public static void main(String[] args)
{
  String[] choices = {"Add Diver/Scores", "View Diver Standings", "Search for a diver", "Exit Program"};
  String input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition", 
                 JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
  while(!input.equals("Exit Program"))
  {
     if (input.equals("Add Diver/Scores"))
        addDiver();
     else if (input.equals("View Diver Standings"))
        viewStandings();
     else if (input.equals("Search for a diver"))
        searchDiver();
     else
        System.exit(0);
     input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition", 
                 JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);      
  }
}
public static void addDiver()
{
  String scoreInvalid = "The score must be between 0 and 10";
  String difficultyInvalid = "The difficulty must be between 1.2 and 3.8";
  double[] scores = new double[7];
  int i = 0;
  String tempScore = "";
  String tempDifficulty = "";
  double realDifficulty = 0;
  double realScore = 0;
  int numScores = 0;

  String diverName = JOptionPane.showInputDialog("Please enter the diver's name:");
  while (numScores < 7)
  {
     tempScore = JOptionPane.showInputDialog("Please enter the seven judge's scores one at a time");
     realScore = Double.parseDouble(tempScore);
     while (realScore < 0 || realScore > 10)
     {
        JOptionPane.showMessageDialog(null, scoreInvalid, "Invalid Score", JOptionPane.WARNING_MESSAGE);
        tempScore = JOptionPane.showInputDialog("Please enter the corrected score");
        realScore = Double.parseDouble(tempScore);
     }

     scores[i] = realScore;
     i++;
     numScores++;
  }
  tempDifficulty = JOptionPane.showInputDialog("Please enter the difficulty rating of the dive");
  realDifficulty = Double.parseDouble(tempDifficulty);

  while (realDifficulty < 1.2 || realDifficulty > 3.8)
  {
     JOptionPane.showMessageDialog(null, difficultyInvalid, "Invalid Difficulty", JOptionPane.WARNING_MESSAGE);
     tempDifficulty =  JOptionPane.showInputDialog("Please enter the                 corrected difficulty rating of the dive");
     realDifficulty = Double.parseDouble(tempDifficulty);
  }
  Diver aDiver = new Diver(diverName, scores, realDifficulty);
  Diver[] dArray = new Diver[30];
  insertDiver(aDiver, dArray);             
}
public static insertDiver(Diver newElement, Diver[] diverArray)
{
  int addpos = 0;
  int currentSize = 0;

  if (diverArray[addpos] == null)
  {
     diverArray[addpos] = newElement;
     currentSize++;
     addpos++;
  }   
  else
     while (addpos < diverArray.length)
     {
        if (newElement.getFinalScore() > diverArray[addpos].getFinalScore())
        {
           diverArray[addpos] = newElement;
           currentSize++;
           break;
        }   
        else
           addpos++;
     }      
  if (addpos >= diverArray.length)
     diverArray[addpos] = newElement;
  else
  {
     currentSize++;
     for (int i = currentSize - 1; i > addpos; i--)
     {
        diverArray[i] = diverArray[i-1];
        diverArray[addpos] = newElement;
     }           

  }
for (int i = 0; i<diverArray.length; i++)
  {
     System.out.println(diverArray[i]);//just to see the contents of the array printed
  }

}
public static void viewStandings()
{

}
public static void searchDiver()
{

}    
}

Here is the Diver class that I created:

public class Diver
{//begin class

private final double MULTIPLIER = 0.6;
private String diverName;
private double[] scores = new double[7];
private double difficulty, finalScore;
//---------------------------------------------------   
public Diver(String dN, double[] s, double d)
{
  setDiver(dN, s, d);
}
//---------------------------------------------------   
public String getName()
{
  return diverName;
}
//---------------------------------------------------   
public double getDifficulty()
{
  return difficulty;
}
//---------------------------------------------------
public double[] getScores()
{
  return scores;
}
//---------------------------------------------------   
public double getFinalScore()
{
  return finalScore;
}
//---------------------------------------------------   
public String toString()
{
  return "Diver Name: " + diverName + " Judge's Scores: " + scores +
         " Dive Difficulty: " + difficulty;
}
//---------------------------------------------------   
public void setDiverName(String name)
{
  diverName = name.toUpperCase();
}
//---------------------------------------------------   
public void setDifficulty(double diff)
{
   difficulty = diff;
}
//---------------------------------------------------   
public void setScores(double[] sc)
{
   for(int i=0; i < 7; i++)
     scores[i] = sc[i];
}
//---------------------------------------------------   
public void setDiver(String nme, double[] score, double dif)
{
  setDiverName(nme);
  setScores(score);
  setDifficulty(dif);
  computeFinalScore();  
}
//---------------------------------------------------
private void computeFinalScore()
{
  double smallest = scores[0];
  double largest = scores[0];
  double scoreSum = 0;

  for(int i=1; i< scores.length; i++)
  {
     if(scores[i] > largest)
        largest = scores[i];
     else if (scores[i] < smallest)
        smallest = scores[i];                 
  }

  for( double i : scores)
  {
     scoreSum += i;
  }

  finalScore =  (scoreSum - smallest - largest) * difficulty * MULTIPLIER;         
} 
//---------------------------------------------------   
}//end class

Solution

    1. Write a Comparator for Diver Class and then you can use a collection like TreeSet which will make your life little easy.
    2. For Display you Can use JTable.

    Here is sample running code for you :

    import java.util.Comparator;
    import java.util.Set;
    import java.util.TreeSet;
    
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    
    public class DivingCompetition
    {
        static DiverComparator comp = new DiverComparator();
        static Set<Diver> divers = new TreeSet<Diver>(comp); 
    
        public static void main(String[] args)
        {
            String[] choices = {"Add Diver/Scores", "View Diver Standings", "Search for a diver", "Exit Program"};
            String input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition", 
                    JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
            while(!input.equals("Exit Program"))
            {
                if (input.equals("Add Diver/Scores"))
                    addDiver();
                else if (input.equals("View Diver Standings"))
                    viewStandings();
                else if (input.equals("Search for a diver"))
                    searchDiver();
                else
                    System.exit(0);
                input = (String) JOptionPane.showInputDialog(null, "What do you want to do?", "Diving Competition", 
                        JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);      
            }
        }
        public static void addDiver()
        {
            String scoreInvalid = "The score must be between 0 and 10";
            String difficultyInvalid = "The difficulty must be between 1.2 and 3.8";
            double[] scores = new double[7];
            int i = 0;
            String tempScore = "";
            String tempDifficulty = "";
            double realDifficulty = 0;
            double realScore = 0;
            int numScores = 0;
    
            String diverName = JOptionPane.showInputDialog("Please enter the diver's name:");
            while (numScores < 7)
            {
                tempScore = JOptionPane.showInputDialog("Please enter the seven judge's scores one at a time");
                realScore = Double.parseDouble(tempScore);
                while (realScore < 0 || realScore > 10)
                {
                    JOptionPane.showMessageDialog(null, scoreInvalid, "Invalid Score", JOptionPane.WARNING_MESSAGE);
                    tempScore = JOptionPane.showInputDialog("Please enter the corrected score");
                    realScore = Double.parseDouble(tempScore);
                }
    
                scores[i] = realScore;
                i++;
                numScores++;
            }
            tempDifficulty = JOptionPane.showInputDialog("Please enter the difficulty rating of the dive");
            realDifficulty = Double.parseDouble(tempDifficulty);
    
            while (realDifficulty < 1.2 || realDifficulty > 3.8)
            {
                JOptionPane.showMessageDialog(null, difficultyInvalid, "Invalid Difficulty", JOptionPane.WARNING_MESSAGE);
                tempDifficulty =  JOptionPane.showInputDialog("Please enter the                 corrected difficulty rating of the dive");
                realDifficulty = Double.parseDouble(tempDifficulty);
            }
            Diver aDiver = new Diver(diverName, scores, realDifficulty);
    
            divers.add(aDiver);
        }
        public static void viewStandings()
        {
            Object[][] rows = new Object[divers.size()][]; 
            int i = 0;
            for(Diver d : divers) {
                rows[i] = new Object[3];
                rows[i][0] = i+1;
                rows[i][1] = d.getName();
                rows[i][2] = d.getFinalScore();
                i++;
            }
                Object[] cols = {
                    "Rank","Name","Final Score"
                };
                JTable table = new JTable(rows, cols);
                JOptionPane.showMessageDialog(null, new JScrollPane(table));
    
        }
        public static void searchDiver()
        {
    
        }    
    }
    class DiverComparator implements Comparator<Diver> {
    
        @Override
        public int compare(Diver o1, Diver o2) {
    
            // TODO Auto-generated method stub
            return (int) (o2.getFinalScore() - o1.getFinalScore());
        }
    
    }