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

The Olympics will begin this summer and several countries will clamor to find ou

ID: 3665648 • Letter: T

Question

The Olympics will begin this summer and several countries will clamor to find out who will win the medal count. However, there are two ways of scoring the games. The first way is called the "American" method. In this method the scoring is based on the number and type of medals a country has won. Gold medals count the most, silver is used to break ties and finally bronze medals are looked at if a country has the same number of gold and silver. Several countries (who usually score fewer gold medals) use the "Canadian" method of scoring instead of the "American" method. Using the "Canadian" method the total number of overall medals determines the winner of the Olympics. Write a Java program that computes the medal winner for the Olympic Games. Do the following: Ask the user what scoring method they would like to use to score the Olympics (a character). Prompt the user for the number of countries competing in the current Olympics. Read in the country names and their medal count. Print out the name of the winning country. In case of a tie, print out the winning countries in input order. Error check. Output should look similar to below. Sample Runs: Please enter the scoring method. American or Canadian -> a Number of countries competing 3 Please enter country and medal count> Germany 3 2 1 Please enter country and medal count> America 2 3 1 Please enter country and medal count> Canada 12 4 The winner is: Germany with 3 gold medals, 2 silver medals, 1 bronze medal Please enter the scoring method. American or Canadian -> C Number of countries competing 3 Please enter country and medal count> Germany 3 2 1 Please enter country and medal count> America 2 3 1 Please enter country and medal count> Canada 12 4 The winner is: Canada with 1 gold medal, 2 silver medals, 4 bronze medals Name the program: OlympicMedals.java, where XX are your initials

Explanation / Answer

import java.util.Scanner;

/**
* @author Srinivas Palli
*
*/
public class OlympicMedals {

   /**
   * @param args
   */
   public static void main(String[] args) {
       Scanner scanner = null;
       try {
           scanner = new Scanner(System.in);
           int goldMedals[] = new int[3];
           int silverMedals[] = new int[3];
           int bronzeMedals[] = new int[3];

           String country[] = new String[3];
           System.out.println("Please enter the scoring Method.");
           System.out.print("A)merica of C)anada-->");
           String choice = scanner.next();

           for (int i = 0; i < country.length; i++) {
               System.out.print("Please enter the country and medal count>");
               country[i] = scanner.next();
               goldMedals[i] = scanner.nextInt();
               silverMedals[i] = scanner.nextInt();
               bronzeMedals[i] = scanner.nextInt();

           }

           if (choice.equalsIgnoreCase("a")) {
               winnerByAmericaMethod(country, goldMedals, silverMedals,
                       bronzeMedals);
           } else if (choice.equalsIgnoreCase("c")) {
               winnerByCanadaMethod(country, goldMedals, silverMedals,
                       bronzeMedals);
           }

       } catch (Exception e) {
           // TODO: handle exception
       }
   }

   /**
   * method to check America scoring method
   *
   * @param country
   * @param goldMedals
   * @param silverMedals
   * @param bronzeMedals
   */
   public static void winnerByAmericaMethod(String country[],
           int goldMedals[], int silverMedals[], int bronzeMedals[]) {
       int max = goldMedals[0];
       int index = 0;
       for (int i = 1; i < country.length; i++) {
           if (max < goldMedals[i]) {
               index = i;
               max = goldMedals[i];
           }

       }

       System.out.println("The Winner is " + country[index] + " with "
               + goldMedals[index] + " gold medals, " + silverMedals[index]
               + " silver medals, " + bronzeMedals[index] + " bronze medals");

   }

   /**
   * method to check Canada scoring method
   *
   * @param country
   * @param goldMedals
   * @param silverMedals
   * @param bronzeMedals
   */
   public static void winnerByCanadaMethod(String country[], int goldMedals[],
           int silverMedals[], int bronzeMedals[]) {
       int max = goldMedals[0] + silverMedals[0] + bronzeMedals[0];
       int index = 0;
       for (int i = 1; i < country.length; i++) {

           if (max < (goldMedals[i] + silverMedals[i] + bronzeMedals[i])) {
               index = i;
               max = goldMedals[i];
           }

       }

       System.out.println("The Winner is " + country[index] + " with "
               + goldMedals[index] + " gold medals, " + silverMedals[index]
               + " silver medals, " + bronzeMedals[index] + " bronze medals");

   }

}

OUTPUT:

Please enter the scoring Method.
A)merica of C)anada-->a
Please enter the country and medal count>Germany 3 2 1
Please enter the country and medal count>America 2 3 1
Please enter the country and medal count>Canada 1 2 4
The Winner is Germany with 3 gold medals, 2 silver medals, 1 bronze medals


Please enter the scoring Method.
A)merica of C)anada-->C
Please enter the country and medal count>Germany 3 2 1
Please enter the country and medal count>America 2 3 1
Please enter the country and medal count>Canada 1 2 4
The Winner is Canada with 1 gold medals, 2 silver medals, 4 bronze medals

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