Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program that sorts an ArrayList in decreasing order so that the most valuable coin is at the beginning of the array. Use a
Write a program that sorts an ArrayList
You need to supply the following class in your solution:
ReverseCoinComparator
Use the following class in your solution:
/** A coin with a monetary value. */ public class Coin { private double value; private String name; /** Constructs a coin. @param aValue the monetary value of the coin. @param aName the name of the coin */ public Coin(double aValue, String aName) { value = aValue; name = aName; } /** Gets the coin value. @return the value */ public double getValue() { return value; } /** Gets the coin name. @return the name */ public String getName() { return name; } public String toString() { return "Coin[value=" + value + ",name=" + name + "]"; } }
Use the following class as your tester class:
import java.util.Collections; import java.util.Comparator; import java.util.ArrayList; /** This program tests sorting an array list of coins. */ public class CoinSortTester { public static void main(String[] args) { ArrayLista = new ArrayList (); a.add(new Coin(0.05, "Nickel")); a.add(new Coin(0.05, "Nickel")); a.add(new Coin(0.25, "Quarter")); a.add(new Coin(0.01, "Penny")); a.add(new Coin(0.1, "Dime")); Comparator comp = new ReverseCoinComparator(); Collections.sort(a, comp); System.out.println(a); System.out.println("Expected: [Coin[value=0.25,name=Quarter], Coin[value=0.1,name=Dime], Coin[value=0.05,name=Nickel], Coin[value=0.05,name=Nickel], Coin[value=0.01,name=Penny]]"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started