The code below shows two Java classes: Dollar and Euro and a main method that de
ID: 3867120 • Letter: T
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 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.00Explanation / Answer
a. In java, final keyword is used to declare constant values. That means, you can only initialize them once and can not be modified later.
b.
Dollar add(Dollar e){
//Creating new dollar object with the sum of amounts
return new Dollar(this.amount+e.getAmount());
}
Euro add(Euro e){
//Creating new Euro object with the sum of amounts
return new Euro(this.amount+e.getAmount());
}
c. Since add method returns a new Dollar/Euro objects, the dol and eur objects which were created initially will get replaced with the new values
d. I could take the input and convert the amount to positive, if a negative values is entered
public Dollar(double amt){
if(amt<0)
amt = -amt;
amount = amt
}
public Euro(double amt){
if(amt<0)
amt = -amt;
amount = amt
}
e. Both the Dollar and Euro classes have the instane variables amount and abbr. So, Instead of having the same attributes in different classes, I could create super class Currency and have the Dollar and Euro classes extend the class
class Amount{
String abbr;
int amount;
Amount(String abbr, int amount){
this.abbr = abbr;
this.amount = amount;
}
}
public class Dollar extends Amount{
Dollar(String abbr, int amount){
super(abbr,amount);
}
}
public class Euro extends Currency{
Euro(String abbr, int amount){
super(abbr,amount);
}
}
f. public class Euro implements Comparable<Euro> {
private final static String abbr = "EUR";
private final double amount;
public Euro(double amt) {
amount = amt;
}
public double getAmount() {
return amount;
}
@Override
public int compareTo(Euro e) {
return Double.compare(this.amount, e.getAmount());
}
}
g. Inorder for printing the objects give the desired result, we must override toString method of Object class
public class Euro implements Comparable<Euro> {
private final static String abbr = "EUR";
private final double amount;
public Euro(double amt) {
amount = amt;
}
public double getAmount() {
return amount;
}
@Override
public int compareTo(Euro e) {
return Double.compare(this.amount, e.getAmount());
}
@Override
public String toString(){
return return String.valueOf(amount);
}
}
The same toString method should be added in Dollar class as well