Question: The code below shows two Java classes: Dollar and Euro and a main method that demonstrates how they might be used. (a) What does the
The code below shows two Java classes: Dollar and Euro and a main method that demonstrates how they might be used.

(a) What does the keyword final mean in the class declarations?
(b) Create an instance method called add that enables a Euro/Dollar object to receive another Euro/Dollar obtect as an argument and return a new Euro/Dollar object with the sum of their values (e.g. see lines 12 and 13).
(c) After line 13, what will happen to the Dollar and Euro objects created on lines 9 and 10? Briefly explain your answer.
(d) Show the code you would write to appropriately handle the situation shown in the code below. [4]

(e) The code in the Euro/Dollar classes shows a high degree of redundancy. Show how you might refactor the code to reduce this redundancy.
(f) Euro/Dollar objects should be sortable according to their value. Using the Euro class as an example, show how you would use the Comparable interface to achieve this.
(g) Write the additional code required to enable the System.out.println methods on lines 15 and 16 to print out the output indicated.
34 public class Dollart 35 36 37 private final static String abbr = "USD". private final double amount; 8 public Dollar double amt amount amt ; 41 2 public 43 public double getAmountOt return amount; 45 1 20 public class Euro t 21 private final static String abbr"EUR": private final double amount; 23 24 25 public Euro(double amt ) 26 27 28 29e public double getAmount Ot amount = ant; return amount; 31 32 public static void main (String [] args){ Dollar dol new Dollar(100.50) Euro eur = new Euro(34.00); 12 dol dol.add(dol); eur = eur.add(eur); 15 16 System.out.println do); //prints out 'USD201.00 System.out.println(eur) //prints out EUR68.00
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
