Design a Java program named class Coin that has two private class members String
ID: 3794681 • Letter: D
Question
Design a Java program named class Coin that has two private class members String coinName and int coinValue and toString method for displaying its parameters. Also, design a class Purse that has just one private member ArrayList coins which stores several objects of class Coin. This class has to have a functionality of adding coins to the purse and displaying the purse contents (toString method). Make each of those classes implement the Comparable interface. Design a test program that creates several instances of class Coin and compares them (by the coin values). This program should also create two instances of class Purse and compare them by the total value of all stored coins.
Explanation / Answer
Coin.java
public class Coin implements Comparable {
//Declaring variables
private String coinName;
private double coinValue;
//parameterized constructor
public Coin(String coinName, double coinValue) {
super();
this.coinName = coinName;
this.coinValue = coinValue;
}
//Setters and getters
public String getCoinName() {
return coinName;
}
public void setCoinName(String coinName) {
this.coinName = coinName;
}
public double getCoinValue() {
return coinValue;
}
public void setCoinValue(double coinValue) {
this.coinValue = coinValue;
}
//This method is used to compare the two Coin Objects
@Override
public int compareTo(Object o) {
int val=0;
Coin c=(Coin)o;
String ocoinname=c.getCoinName();
double ocoinval=c.getCoinValue();
String coinname=this.getCoinName();
double coinval=this.getCoinValue();
if(coinname.equals(ocoinname) && coinval==ocoinval)
val=1;
else
val=-1;
return val;
}
//toString() method is used to display the contents of the Coin class object
@Override
public String toString() {
return "coinName=" + coinName + ", coinValue=" + coinValue;
}
}
___________________
Purse.java
import java.util.ArrayList;
public class Purse implements Comparable {
//Declaring Instance variable
private ArrayList coins;
//parameterized constructor
public Purse(ArrayList coins) {
super();
this.coins = coins;
}
//This method is used to add the contents of the Purse Class object
public double addCoins()
{
double sum=0;
for(Object c:coins)
{
Coin c1=(Coin)c;
sum+=c1.getCoinValue();
}
return sum;
}
//This method is used to compare two Purce class obejcts
@Override
public int compareTo(Object o) {
int val=0;
Purse p=(Purse)o;
double ototval=p.addCoins();
double totval=this.addCoins();
if(ototval==totval)
val=1;
else
val=-1;
return val;
}
//toString() method is used to display the contents inside the Purce class obejct
@Override
public String toString() {
for(Object o:coins)
{
Coin c=(Coin)o;
System.out.println(c.getCoinName()+" "+c.getCoinValue());
}
return " ";
}
}
_____________________
Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
//Creating the Coin class objects
Coin c1=new Coin("Five Dollors",5);
Coin c2=new Coin("Ten Dollors",10);
Coin c3=new Coin("Ten Dollors",10);
Coin c4=new Coin("One Dollors",1);
Coin c5=new Coin("Five Dollors",5);
Coin c6=new Coin("Ten Dollors",10);
Coin c7=new Coin("Ten Dollors",10);
Coin c8=new Coin("One Dollors",1);
System.out.println(" Comparing the Coin#1 and Coin#4 class objects");
//Comparing the coin class objects
int val=c1.compareTo(c4);
if(val==1)
{
System.out.println(c1.toString()+" is equal to "+c2.toString());
}
else
{
System.out.println(c1.toString()+" is not equal to "+c2.toString());
}
//Comparing the coin class objects
System.out.println(" Comparing the Coin#2 and Coin#3 class objects");
int val1=c2.compareTo(c3);
if(val1==1)
{
System.out.println(c2.toString()+" is equal to "+c3.toString());
}
else
{
System.out.println(c2.toString()+" is not equal to "+c3.toString());
}
//Creating an ArrayList which holds Coin class objects
ArrayList arl1=new ArrayList<Coin>();
//Adding Coin class objects
arl1.add(c1);
arl1.add(c2);
arl1.add(c3);
arl1.add(c4);
//Creating an ArrayList which holds Coin class objects
ArrayList arl2=new ArrayList<Coin>();
//Adding Coin class objects
arl2.add(c5);
arl2.add(c6);
arl2.add(c7);
arl2.add(c8);
//creating the purse class objects by passing the ArrayList as argument
Purse p1=new Purse(arl1);
Purse p2=new Purse(arl2);
//Comparing the two purse class objects
System.out.println(" Comparing the two purse class objects");
int val2=p1.compareTo(p2);
if(val2==1)
{
System.out.println("Total value in both the purses are equal");
}
else
{
System.out.println("Total value in both the purses are not equal");
}
}
}
________________________
Output:
Comparing the Coin#1 and Coin#4 class objects
coinName=Five Dollors, coinValue=5.0 is not equal to coinName=Ten Dollors, coinValue=10.0
Comparing the Coin#2 and Coin#3 class objects
coinName=Ten Dollors, coinValue=10.0 is equal to coinName=Ten Dollors, coinValue=10.0
Comparing the two purse class objects
Total value in both the purses are equal
_________________Thank You