Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, these are my question. I already made iterator, But I don't know how to start with a comparator. Here are my source codes and

image text in transcribed

Hello, these are my question. I already made iterator, But I don't know how to start with a comparator.

Here are my source codes and Could you please give me some help?

OlympicResults.java

 public class OlympicResults implements Iterable{ /** List of medal counts for each nation */ private ArrayList mMedalCountList = new ArrayList(); /** * Add a medal country entry for a nation. * @param countryName is the name of the nation * @param nGoldMedals is the number of gold medals * @param nSilverMedals is the number of silver medals * @param nBronzeMedals is the number of bronze medals */ public void addNationResults(String countryName, int nGoldMedals, int nSilverMedals, int nBronzeMedals) { MedalCount mc = new MedalCount(countryName, nGoldMedals, nSilverMedals, nBronzeMedals); mMedalCountList.add(mc); } // TODO: Add code so we can iterate over list of medal counts public Iterator iterator(){ return new Iterator() { int i = 0; @Override public boolean hasNext() { if( i  

MedalCounts

/** * Keeps track of the number of Olympic medals for a nation. * */ public class MedalCount { String name; int goldMedals; int silverMedals; int bronzeMedals; int totalMedals; /** * Creates a new MedalCount instance. * * @param name is the name of the nation * @param nGoldMedals is the number of gold medals * @param nSilverMedals is the number of silver medals * @param nBronzeMedals is the number of bronze medals */ public MedalCount(String name, int nGoldMedals, int nSilverMedals, int nBronzeMedals) { this.name = name; this.goldMedals = nGoldMedals; this.silverMedals = nSilverMedals; this.bronzeMedals = nBronzeMedals; totalMedals = nGoldMedals + nSilverMedals + nBronzeMedals; } /** * Return a string describing the medal count information for the country */ public String toString() { return name + ":\t" + goldMedals + ",\t" + silverMedals + ",\t" + bronzeMedals + ",\t" + totalMedals; } /** * Compare instance of medal count with another. * Nations compared based on number of total medals (descending order). */ // TODO: create comparator public static final Comparator comparator = new Comparator() { @Override public int compare(MedalCount totalMedals1, MedalCount totalMedals2) { if (totalMedals1.totalMedals >= totalMedals2.totalMedals) { return 1; } if (totalMedals1.totalMedals  

TestModelSort.java

import java.util.Collections; /** * Test the comparators added to the MedalCount class. * * */ public class TestMedalSort { /** * Display the list of nations and their medal counts * @param medalList is a list of nations and their medal counts */ public static void displayCountries(OlympicResults olympicResults) { System.out.format("%-15s %8s %8s %8s %8s ", "Nation", "Gold", "Silver", "Bronze", "Total"); // TODO: add code to output medal counts for each nation. // (This is where your iterator code pays off) // HINT: see test code in OlympicResults.java. for (MedalCount mc : olympicResults) { System.out.format("%-15s %8d %8d %8d %8d ", mc.name, mc.goldMedals, mc.silverMedals, mc.bronzeMedals, mc.totalMedals); } } /** * Test the comparators from MedalCount. * * @param args is an array of command line arguments */ public static void main(String[] args){ OlympicResults olympicResults = new OlympicResults(); // TODO: Add medal counts for 10 nations olympicResults.addNationResults("China", 38, 28, 22); olympicResults.addNationResults("Great Britain", 29, 17, 19); olympicResults.addNationResults("USA", 46, 29, 29); olympicResults.addNationResults("South Korea", 13,8, 7); olympicResults.addNationResults("Germany", 11,19, 14); olympicResults.addNationResults("Russia", 24,25, 32); olympicResults.addNationResults("France", 11,8, 12); olympicResults.addNationResults("Italy", 8,9, 11); olympicResults.addNationResults("Hungary", 8,4, 6); olympicResults.addNationResults("Australia", 7,16, 12); // display list of nations and their medals displayCountries(olympicResults); // sort list by nation name // olympicResults.sortByName(); System.out.println(" Sorted by name:"); displayCountries(olympicResults); // sort list by gold medals System.out.println(" Sorted by Gold medals:"); // TODO: Sort nations by gold medals and display results Collections.sort(mMedalCountList); // sort list by total medals System.out.println(" Sorted by Total Medals:"); // TODO: Sort nations by total medals and display results MedalCount.comparator(mMeda) } } 

Thank you so much

1. (25 pts) Iterating through Olympic medal counts. The file O1ympicResults.java contains a collection of medal counts for nations participating in the Olympics. Medal count information for a nation is defined in MedalCount.java. The list of medal counts is private to OlympicResults so that it may only be modified through its public functions. Make O1ympicResults iterable so that one may access the individual medal counts without directly accessing the list. The iterator should return the next MedalCount object in the list. 2. (25 pts) Sorting nations and their Olympic medal counts. The file MedalCount.java defines a class that stores the Olympic gold, silver, bronze and total medal counts for a given nation. However, this file is incomplete. Modify this class so that it may be sorted using the built-in java system sort Collections.sort() Make MedalCount comparable so an ArrayList of MedalCount objects mMedalCountList may be sorted using Collections.sort (mMedalCountList); This statement should sort the list in descending order by total medal counts. Add the function sortByTotalMedals to OlympicResults. This function should sort the ArrayList of MedalCount objects as previously stated 3. (25 pts) Additional ways to sort nations. Add Comparators to MedalCount, so that a list of instances can be sorted: Alphabetically, by nation name. By number of gold medals in descending order. A nation with more gold medals should come before a nation with fewer. Just as you did before, add the functions sortByName and sortByGoldMedals to 01ympicResults These function should sort the ArrayList of MedalCount objects using the system sort with the comparators you just added to MedalCount 4. (25 pts) Testing your code. Complete the file TestMedalSort.java. In this file, the main method creates an instance of OlympicResults, adds results for 10 nations, then sorts them using each of the comparators You need to add the following: (a) Complete the function displayCountries by outputting the medal counts for each nation (b) Add medal count entries for 10 nations (c) Sort the array list using each of the sort functions that you created in MedalCount

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Databases On The Web Learn To Create Web Pages That Interface With Database Engines

Authors: Robert Papaj, Donald Burleson

11th Edition

1576100995, 978-1576100998

More Books

Students also viewed these Databases questions

Question

72,800 50,540 35,700 14,840

Answered: 1 week ago