Write a Java class that implements the concept of Coins, assuming the following
ID: 3695627 • Letter: W
Question
Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include the following methods: - A method to return the amount of money in dollars notation (e.g., $0.41 if you have one coin of each) - Write a test program (client class) to test all methods in the class.
Explanation / Answer
public class Coins
{
private int quarters; // private fields number of coins
private int dimes;
private int nickels;
private int pennies;
public Coins() // default constructor
{
quarters=1; // setting all values to 1
dimes=1;
nickels=1;
pennies=1;
}
public Coins(int quarters,int dimes,int nickels,int pennies) // constructor with perameters
{
this.quarters=quarters; // setting values with this key word
this.dimes=dimes;
this.nickels=nickels;
this.pennies=pennies;
}
public void quartersSetter(int quarters) //getters and setters methods
{
this.quarters=quarters;
}
public int quartersGetters()
{
return quarters;
}
public void dimesSetter(int dimes)
{
this.dimes=dimes;
}
public int dimesGetter()
{
return dimes;
}
public void nickelsSetter(int nickels)
{
this.nickels=nickels;
}
public int nickelsGetter()
{
return nickels;
}
public void penniesSetter(int pennies)
{
this.pennies=pennies;
}
public int penniesGetter()
{
return pennies;
}
public float money() // method moey to find total amount
{
return (quarters*0.41)+(dimes*0.41)+(nickels*0.41)+(pennies*0.41);
}
public String toString() // overriding toString method
{
return "quarters "+quarters+"dimes "+dimes+"nickels "+nickels+"pennies "+pennies;
}
public boolean equals(Object o) // overriding equals method we need to override hascode method but no need here
{
if(o==this)
{
return true;
}
if(!(o instanceof Coins))
{
return false;
}
Coins c=(Coins) o;
return (Integer.compare(quarters,c.quarters)==0 && Integer.compare(dimes,c.dimes)==0) && (Integer.compare(nickels,c.nickels)==0 && Integer.compare(pennies,c.pennies)==0);
}
}
Client Class
public class Client
{
public static void main(String args[])
{
Coins obj1=new Coins(); // creating object with no args
Coins obj2=new Coins(3,1,2,5); // creating obj with orgs
Coins obj3=new Coins(3,1,2,5); // creating object with same value with perameters
obj1.quartersSetter(5); //setting and getting values for obj1
obj1.dimesSetter(2);
System.out.println(obj1.quarterGetter());
obj2.toString(); // calling toString mehtod
System.out.println("$"obj2.money()+"is total money"); // calling money mehtod
if(obj2.equals(obj3)) // checking equals method
System.out.println("testing equal ok");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.