*A coin with a monetary value. public class Coin implements Comparable { private double value; private String name; /** Constructs a coin. eparam 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. * ereturn the value public double getValue() { return value; /** * Gets the coin name. ereturn the name */ public String getName() { return name; } @override public String tostring() { return ""; // FIX ME @Override public boolean equals(Object otherobject) { return false; // FIX ME } /** Compares two Coin objects. @param otherobject the object to be ompared @return a negative integer, zero, or a positive integer as this coin is less than, equal to, or greater than the specified coin public int compareTo(Object otherobject) { return -1; // FIX ME }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java. Please use the template, thank you!

30 /**
A coin with a monetary value.
*/
4
6 public class Coin implements Comparable {
7
private double value;
private String name;
8.
9.
10
110
/**
12
* Constructs a coin.
13
@param aValue the monetary value of the coin.
@param aName
*/
*
14
15
the name of the coin
16
public Coin(double aValue, String aName) {
value = aValue;
170
18
19
name = aName;
20
21
22
230
/**
24
* Gets the coin value.
*
25
26
@return the value
27
* /
public double getValue() {
return value;
}
280
29
30
31
32
330
/**
34
* Gets the coin name.
*
35
*
36
@return the name
37
*/
380
public String getName () {
39
return name;
40
}
41
42
@Override
public String toString() {
430
A44
45
return ""
; |/ FIX ME
46
}
47
48
490
@Override
public boolean equals(Object otherObject) {
50
return false; // FIX ME
}
51
52
53
54
550
/**
56
Compares two Coin objects.
57
@param otherObject the object to be compared
@return a negative integer, zero, or a positive integer as this coin is less
58
59
60
than, equal to, or greater than the specified coin
61
*/
public int compareTo(Object otherObject) {
return -1; // FIX ME
}
620
63
64
65
66 }
67
Transcribed Image Text:30 /** A coin with a monetary value. */ 4 6 public class Coin implements Comparable { 7 private double value; private String name; 8. 9. 10 110 /** 12 * Constructs a coin. 13 @param aValue the monetary value of the coin. @param aName */ * 14 15 the name of the coin 16 public Coin(double aValue, String aName) { value = aValue; 170 18 19 name = aName; 20 21 22 230 /** 24 * Gets the coin value. * 25 26 @return the value 27 * / public double getValue() { return value; } 280 29 30 31 32 330 /** 34 * Gets the coin name. * 35 * 36 @return the name 37 */ 380 public String getName () { 39 return name; 40 } 41 42 @Override public String toString() { 430 A44 45 return "" ; |/ FIX ME 46 } 47 48 490 @Override public boolean equals(Object otherObject) { 50 return false; // FIX ME } 51 52 53 54 550 /** 56 Compares two Coin objects. 57 @param otherObject the object to be compared @return a negative integer, zero, or a positive integer as this coin is less 58 59 60 than, equal to, or greater than the specified coin 61 */ public int compareTo(Object otherObject) { return -1; // FIX ME } 620 63 64 65 66 } 67
1. Make it implement the Comparable interface. In particular, compare coins first by their value (smaller values first),
and then by their name (lexicographically, in the way the Java String class compare strings).
2. Add a toString method that includes the value and name of the coin, formatted like the end of the example
below.
3. Add an equals method that considers two coins equal if their value and name (case sensitive) match.
Example:
Coin coinl = new Coin (.25, "Quarter");
Coin coin2 = new Coin (.10, "Dime");
Coin coin3 = new Coin (.10, "Dime");
Coin coin4 = new Coin (.10, "My Dime");
Coin coin5 = new Coin (.01, "Penny");
Coin coin6 = new Coin (.01, "Different Penny");
Coin coin7 = new Coin (.05, "NICKEL");
Coin coin8 = new Coin (.05, "nickel");
Coin coin9
= new Coin(.05, "Nickel");
coinl.compareTo (coin2); // returns a positive int
coin2.compareTo (coin3) ; // returns 0
coin3.compareTo (coinl); // returns a negative int
coin3.compareTo (coin4); // returns a negative int
coin4.compareTo (coin3); // returns a positive int
List<Coin> coins = new ArrayList<> () ;
coins.add (coinl);
coins.add (coin2);
coins.add (coin3);
coins.add (coin4);
coins.add (coin5);
coins.add (coin6);
coins.add (coin7);
coins.add (coin8);
coins.add (coin9);
Collections.sort (coins);
for (Coin c : coins) {
System.out.println (c);
}
// The above prints:
Value: 0.01, Name: Different Penny
Value: 0.01, Name: Penny
Value: 0.05, Name: NICKEL
Value: 0.05, Name: Nickel
Value: 0.05, Name: nickel
Value: 0.1, Name: Dime
Value: 0.1, Name: Dime
Value: 0.1, Name: My Dime
Value: 0.25, Name: Quarter
Transcribed Image Text:1. Make it implement the Comparable interface. In particular, compare coins first by their value (smaller values first), and then by their name (lexicographically, in the way the Java String class compare strings). 2. Add a toString method that includes the value and name of the coin, formatted like the end of the example below. 3. Add an equals method that considers two coins equal if their value and name (case sensitive) match. Example: Coin coinl = new Coin (.25, "Quarter"); Coin coin2 = new Coin (.10, "Dime"); Coin coin3 = new Coin (.10, "Dime"); Coin coin4 = new Coin (.10, "My Dime"); Coin coin5 = new Coin (.01, "Penny"); Coin coin6 = new Coin (.01, "Different Penny"); Coin coin7 = new Coin (.05, "NICKEL"); Coin coin8 = new Coin (.05, "nickel"); Coin coin9 = new Coin(.05, "Nickel"); coinl.compareTo (coin2); // returns a positive int coin2.compareTo (coin3) ; // returns 0 coin3.compareTo (coinl); // returns a negative int coin3.compareTo (coin4); // returns a negative int coin4.compareTo (coin3); // returns a positive int List<Coin> coins = new ArrayList<> () ; coins.add (coinl); coins.add (coin2); coins.add (coin3); coins.add (coin4); coins.add (coin5); coins.add (coin6); coins.add (coin7); coins.add (coin8); coins.add (coin9); Collections.sort (coins); for (Coin c : coins) { System.out.println (c); } // The above prints: Value: 0.01, Name: Different Penny Value: 0.01, Name: Penny Value: 0.05, Name: NICKEL Value: 0.05, Name: Nickel Value: 0.05, Name: nickel Value: 0.1, Name: Dime Value: 0.1, Name: Dime Value: 0.1, Name: My Dime Value: 0.25, Name: Quarter
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY