Write a class encapsulating the concept of coins, assuming that coins have the f
ID: 3566560 • Letter: W
Question
Write a class encapsulating the concept of coins, assuming that coins have the following attributes: a number of quarters, a number of dimes, a number of nickels, and a number of pennies. Include a constructor, the accessors and mutators, and methods toString() and equals. Also code the following methods: one returning the total amount of money in dollar notation with two significant digits after the decimal point, and others returning the money in quarters (for instance, 0.75 if there are three quarters), in dimes, in nickels, and in pennies. Write a client class to test all the methods in your class.
Explanation / Answer
import java.util.Scanner;
public class Calculator {
private static Scanner s;
/**
* @param args
*/
public static void main(String[] args) {
s = new Scanner(System.in);
System.out.println("Enter no of Quarters");
int q=s.nextInt();
System.out.println("Enter no of Dimes");
int d=s.nextInt();
System.out.println("Enter no of Nickels");
int n=s.nextInt();
System.out.println("Enter no of Penies");
int p=s.nextInt();
coin c1 = new coin(q,d,n,p);
double result = c1.toDollors(q,d,n,p);
System.out.println("The amount in Dollors is" + result);
}
}
class coin{
private int nQuarters;
private int nDimes;
private int nNickels;
private int nPennies;
public double fDollors;
//constructor
public coin(int nQuarters,int nDimes ,int nNickels,int nPennies){
this.nQuarters =nQuarters;
this.nDimes =nDimes;
this.nNickels =nNickels;
this.nPennies =nPennies;
fDollors=0;
}
//Accessors
public int getQuarters() { return nQuarters; }
public int getDimes() { return nDimes; }
public int getNickels() { return nNickels; }
public int getPennies() { return nPennies; }
//Mutators
public void setQuarters(int Quarters) { this.nQuarters=Quarters; }
public void setDimes(int Dimes) { this.nDimes=Dimes; }
public void setNickels(int Nickels) { this.nNickels=Nickels; }
public void setPennies(int Pennies){ this.nPennies=Pennies; }
//Method to Covert to Dollors
public double toDollors(int nQuarters ,int nDimes,int nNickels ,int Pennies){
fDollors= (nQuarters*0.25)+(nDimes*0.10)+(nNickels*0.25)+(nPennies*0.01);
return fDollors;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.