I'm a beginner to java. When I ran the class & tester, there were no errors, but I'm not getting the output I want from the tester. I can't find what I'm doing wrong. Please help
Here's my code:
/**
* A CoinCounter has a specific number of cents. It can provide the number of dollars and the
* number of cents that it contains
*/
public class CoinCounter
{
// constants
public static final int PENNIES_PER_NICKEL = 5;
public static final int PENNIES_PER_DIME = 10;
public static final int PENNIES_PER_QUARTER = 25;
public static final int PENNIES_PER_DOLLAR = 100;
// instance field (one - holds the total number of cents EX: 8,534)
private int totalCents;
/**
* Constructs a CoinCounter object with a specified number of pennies, nickels, dimes and quarters
* #param pennies number of pennies
* #param nickels number of nickels
* #param dimes number of dimes
* #param quarters numbr of quarters
*/
public CoinCounter(int pennies, int nickels, int dimes, int quarters)
/**
* Computes the total value in pennies
*/
{
int totalCents = pennies + nickels * PENNIES_PER_NICKEL + dimes * PENNIES_PER_DIME + quarters * PENNIES_PER_QUARTER;
}
// ACCESSOR methods as described (only two)
/**
* Gets the number of dollars
* #return number of dollars
*/
public int getDollars()
{
int dollars = totalCents / PENNIES_PER_DOLLAR;
return dollars;
}
/**
* Gets the number of cents
* #return number of cents
*/
public int getCents()
{
int cents = totalCents % PENNIES_PER_DOLLAR;
return cents;
}
//MUTATOR METHODS - NONE FOR THIS PROGRAM
Here's my tester:
import javax.swing.JOptionPane;
/**
* A class to test the CoinCounter class
*/
public class CoinCounterTester
{
/**
* Tests methods of the CoinCounter class
* #param args not used
*/
public static void main(String[] args)
{
// Use JOptionPane to read in coins
String input = JOptionPane.showInputDialog("Please enter the number of pennies: ");
int pennies = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please enter the number of nickels: ");
int nickels = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please enter the number of dimes: ");
int dimes = Integer.parseInt(input);
input = JOptionPane.showInputDialog("Please enter the number of quarters: ");
int quarters = Integer.parseInt(input);
// Construct an object
CoinCounter myCoins = new CoinCounter(22, 8, 17, 5);
//Call the TWO ACCESSOR METHODS-print dollars & cents
System.out.println("The total dollars is: " + myCoins.getDollars());
System.out.println("The total cents is: " + myCoins.getCents());
System.exit(0);
//NO MUTATOR METHODS to test
}
}
After I run the tester and enter the amount of each coin, my output is
The total dollars is: 0
The total cents is: 0
instead of
The total dollars is: 3
The total cents is: 57
What am I doing wrong? Thanks!
change
int totalCents = pennies + nickels * PENNIES_PER_NICKEL....
to
this.totalCents = pennies + nickels....
Related
I got the dollars to work but now I can`t figure out how to display the quarters, dimes, nickels, and pennies correctly. Technically they show up but my professor wants $1.35 to show up as 1 dollar 1 quarter and 1 dime. But, mine shows up as 1 dollars 5 Quarters 13 Dimes 27 Nickels 135 Pennies. Here is my code:
import java.util.Scanner;
public class ComputeChange {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an amount in double, for example 11.56: " );
double number = input.nextDouble();
System.out.println("Your amount " + number + " consists of ");
int remainingamount = (int)(number*100);
int Quarters = remainingamount/25;
int Dimes = remainingamount/10;
int Nickels = remainingamount/5;
int Pennies = remainingamount;
if (number == 1) {
System.out.print("1 dollar ");
}
else if (number > 1) {
System.out.print((int)number + " dollars ");
}
if (number == 0) {
System.out.println("");
}
System.out.print(Quarters + " Quarters " + Dimes + " Dimes " + Nickels + " Nickels " + Pennies + " Pennies");
}
}
For each different coin you calculate, you need to remove this from the remaining amount. Such as (untested):
int Dollars = (int)number;
int remainingamount = (int)((number-Dollars)*100);
int Quarters = remainingamount/25;
remainingamount -= Quaters * 25;
int Dimes = remainingamount/10;
remainingamount -= Dimes * 10
int Nickels = remainingamount/5;
remainingamount -= Nickels * 5
int Pennies = remainingamount;
Since you're using Java, have you tried JSR 354?
See JavaMoney.org or the JSR 354 Detail page at JCP.org It offers standard API for the conversion of monetary units and default exchange rate providers by the IMF or European Central Bank.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have been away from Java for awhile and am trying to recall and learn a lot still. I have a current project which is a piggy bank that you add coins to and can get various outputs. I am currently tasked with 5 methods plus a helper method for the constructors. I know what I want to do, but can't think through the code to get it done. I want to use the helper method to get the amounts for the other two constructors, but cannot get my head around it, I can only look through my book so long. Any input is appreciated.
The description for each method is as follows:
P.S. The code I do have may not be correct.
publicChangeJar() Default constructor that sets all the instance variables to zero
publicChangeJar(int quarters, int dimes, int nickels, int pennies) A constructor that initializes the instance variables with the provided values converted to quarters, dimes, nickels, and pennies.
public ChangeJar(final double amount) A constructor that initializes the instance variables with the provided value converted to quarters, dimes, nickels, and pennies. For example, if amount was 1.34 then you would have 5 quarters, 1 nickel, 4 pennies
public ChangeJar(final String amount)A constructor that accepts a string as a parameter with the provided value converted to appropriate number of quarters, dimes, nickels, and pennies. For example, if amount was “1.34” then you would have 5 quarters, 1 nickel, 4 pennies.
public ChangeJar(final ChangeJar other)A constructor that initializesthe instance variables of “this” ChangeJar object with the other object.
public class ChangeJar {
private int pennies;
private int nickels;
private int dimes;
private int quarters;
static boolean globalLock = false;
public ChangeJar(){
this(0,0,0,0);
}
public ChangeJar(int pennies, int nickels, int dimes, int quarters)throws IllegalArgumentException {
if (pennies < 0 || nickels < 0 || dimes < 0 || quarters < 0)
throw new IllegalArgumentException("You cannot have negative coins in the jar");
else this.pennies = this.pennies + pennies;
this.nickels = this.nickels + nickels;
this.dimes = this.dimes + dimes;
this.quarters = this.quarters + quarters;
}
public ChangeJar(double amount){
}
public ChangeJar(final String amount){
}
private double amountHelper(double amount){
amount = pennies*.01 + nickels*.05 + dimes*.10 + quarters*0.25;
return amount;
}
public ChangeJar(final ChangeJar other){
}
}
EDIT: My problem here is how to write the helper method to work in both constructors.
Constructor ChangeJar(double)
For your constructor with an amount, you want to use the maximum number of quarters, then the maximum number of pennies, and so on.
Let's say I have $2.87.
First, I will take 11 quarters. We still have $0.12 left.
Then, I will take 1 dime. We still have $0.02 left.
Then, I will take 0 nickel. We still have $0.02 left.
Then, I will take 2 pennies. We're done.
How to implement that? Let's say the amount is 2.87.
public ChangeJar(double amount) {
// How many quarters?
int quarters = (int) (amount / .25); // The division gives 9.48 and we cast the result to int so we get 9
amount = amount - quarters * .25;
System.out.println(quarters + " quarters. Remains: " + amount);
// How many dimes?
int dimes = (int) (amount / .10);
amount = amount - dimes * .10;
System.out.println(dimes + " dimes. Remains: " + amount);
// How many nickels?
int nickels = (int) (amount / .05);
amount = amount - nickels * .05;
System.out.println(nickels + " nickels. Remains: " + amount);
// How many pennies?
int pennies = (int) (amount / .01);
amount = amount - pennies * .01;
System.out.println(pennies + " pennies. Remains: " + amount);
// Prints:
// 11 quarters. Remains: 0.1200000000000001
// 1 dimes. Remains: 0.0200000000000001
// 0 nickels. Remains: 0.0200000000000001
// 2 pennies. Remains: 1.0061396160665481E-16
// Now we just set this in our properties:
this.quarters = quartes;
this.dimes = dimes;
this.nickels = nickels;
this.pennies = pennies;
}
As you can see, the problem is that the remainders are strange values. The constructor works but it's not really cool. Why? Because Java approximates the doubles.
I would suggest to work with ints. For example, you could change your unit from $ to $/100. Our same example with integer values (the input is not 2.87 but 287):
public ChangeJar(int amount) {
// How many quarters?
int quarters = amount / 25;
amount = amount - quarters * 25;
System.out.println(quarters + " quarters. Remains: " + amount);
// How many dimes?
int dimes = amount / 10;
amount = amount - dimes * 10;
System.out.println(dimes + " dimes. Remains: " + amount);
// How many nickels?
int nickels = amount / 5;
amount = amount - nickels * 5;
System.out.println(nickels + " nickels. Remains: " + amount);
// How many pennies?
int pennies = amount;
amount = amount - pennies;
System.out.println(pennies + " pennies. Remains: " + amount);
// Prints:
// 11 quarters. Remains: 12
// 1 dimes. Remains: 2
// 0 nickels. Remains: 2
// 2 pennies. Remains: 0
// Now we just set this in our properties:
this.quarters = quartes;
this.dimes = dimes;
this.nickels = nickels;
this.pennies = pennies;
}
That's already better!
But there is a lot of copy/paste in my code...
How could we make it better?
We can see that for each coin, I get the number of coins and then I subtract the value from the amount.
int amount = 287;
int[] values = new int[]{25, 20, 5, 1}; // The values of my coins
int[] results = new int[values.length];
for (int i = 0; i < values.length; i++) {
int valueOfCoin = values[i];
int numberOfCoins = amount / valueOfCoin; // Division gives the integer part of the result
results[i] = numberOfCoins;
amount = amount % valueOfCoin; // Modulo gives the remainder part of the result
// Or you could simply write: amount %= valueOfCoin;
}
System.out.println("RESULTS=" + Arrays.toString(results));
// Prints:
// RESULTS=[9, 1, 0, 2]
Constructor ChangeJar(String)
I suppose that the String is an amount so we will just convert the String to a Double and call the other constructor (ChangeJar(double)).
public ChangeJar(String amount) {
this(Double.valueOf(amount)); // Double.valueOf() will try to convert the String => Double
}
Constructor ChangeJar(ChangeJar)
The idea is just to copy the values of the other ChangeJar:
public ChangeJar(ChangeJar other) {
this(other.quarters, other.dimes, other.nickels, other.pennies);
}
Ok so i need to make a program to ask me for an amount of money, then I need it to tell me the least amount of coins to make it. The coins I can use are: dollars, quarters, dimes, nickels, and pennies. For example, When I run the program it's supposed to look like this:
> run Coins
Enter the amount of given money:
[1.73]
Give the seller 8 coins:
1 dollars,
2 quarters,
2 dime,
0 nickels,
3 pennies.
This is What I have so far:
import java.util.Scanner;
class Coins {
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
double money;
System.out.println("Enter the amount of money ");
money = input.nextDouble();
while (money > 0.0 ){
if (money >= 1) {
System.out.println(money/1+"dollars");
money -= 1;
}
else if (money>=.25) {
System.out.println(money/.25+"quarters");
money-=.25;
}
else if (money>=.10) {
System.out.println(money/.10+"Dimes");
money-=.10;
}
else if (money>=.05) {
System.out.println(money/.05+"Nickels");
money-=.05;
}
else if (money>=.01) {
System.out.println(money/.01+"Penny");
money-=.01;
}
}
}
}
The part I need help with is this: If I run the program and enter the amount 1.73, the way I have the code written, it takes the number 1.73, divides it by 1, and prints "1.73 dollars". I need a way to get rid of the decimal part so instead of printing "1.73 dollars", it prints "1 dollar". But I'm not sure how to do this. I tried converting it to an int but it messes up the flow of the other statements. Please help me.
You should use the combination of floor with casting to double, the following code works:
class Coins {
public static void main (String args[]) {
double money = 1.73;
while (money > 0.0 ){
if (money >= 1) {
System.out.println(Math.floor(money/1)+" dollars");
money -= Math.floor(money/1)*(double)1;
}
else if (money>=.25) {
System.out.println(Math.floor(money/.25)+" quarters");
money-=Math.floor(money/.25)*(double).25;
}
else if (money>=.10) {
System.out.println(Math.floor(money/.10)+" Dimes");
money-=Math.floor(money/.10)*(double).10;
}
else if (money>=.05) {
System.out.println(Math.floor(money/.05)+" Nickels");
money-=Math.floor(money/.05)*(double).05;
}
else if (money>=.01) {
System.out.println(Math.round(money/.01)+" Penny");
money-=Math.round(money/.01)*(double).01;
}
}
}
}
Another bug you had:
You should subtract Math.floor(money/XXX)*(double)XXX not (double)XXX
You need get rid of the remainder after the divisions. You can use Math.floor() for this:
class Coins {
public static void main (String args[]) {
double money = 1.73;
int dollars = (int) Math.floor(money/1);
money -= dollars * 1;
int quarters = (int) Math.floor(money/0.25);
money -= quarters * 0.25;
int dimes = (int) Math.floor(money/0.10);
money -= dimes * 0.10;
int nickels = (int) Math.floor(money/0.05);
money -= nickels * 0.05;
int pennies = (int) Math.round(money * 100);
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
}
}
Resulting in:
Dollars: 1
Quarters: 2
Dimes: 2
Nickels: 0
Pennies: 3
am only getting the interest amount an not the amount i suppose to pay a month can you please tell me where am going wrong thanks.
import java.util.Scanner;
/**
*
* #author
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate;
double payment;
//input
System.out.print("Enter Loan Amount:");
double principal = input.nextDouble();
System.out.print("Enter Annual Interest:");
double interest = input.nextDouble();
System.out.print("Total payment type:");
String period = input.next();
System.out.print("Enter Loan Length :");
int length = input.nextInt();
//proces
rate = interest / 100;
if (period.equals("monthly")) {
double n = length * 12;
payment = principal * (rate * Math.pow((1 + rate), n) / Math.pow((1 + rate), n));
System.out.printf("Your Monthly Sum is %.2f",payment);
}
}
Your error is here:
principal * rate * Math.pow((1 + rate), n) / Math.pow((1 + rate), n)
This is the same as having only principal * rate. You are saying x = b * a / a.
Replace to:
payment = principal * Math.pow((1 + rate), n);
n is the number of years, you can not do n = length / 12 to get Monthly. You should do instead:
payment = (principal * Math.pow((1 + rate), n)) / 12;
It should be
payment = principal * Math.pow((1 + rate), n);
As
A=P((1+rate/100)^n)
I am student, trying to finish a homework lab. Right now I am struggling to use the input from JOptionPane to display the results based on the input. There are two files to my lab, the CoinCounterTester.java and the CoinCounter.java. Both compile but it should print the total numbers of dollars and the total number of cents (teachers instructions) "Output should be in the console window, and should demonstrate the use of and escape sequences". I'm not sure if any part of the tester is correct but I think the JOptionPane methods are correct. I also think I'm supposed to parse them to get them into integer form , however I do not know how to print the designated number of dollars and number of cents left over based on user input. Can anyone explain?
Thanks
Edit:
Ok the answer seems like its correct but I'm confused about using the constructor call. What would you put for the parameters for the constructor call, I put
CoinCounter coinCounter = new CoinCounter(int quarters, int dimes, int nickels, int pennies);
but got seven errors
Edit 2:
I have now not included the variable type as well wisely suggested and input
CoinCounter coinCounter = new CoinCounter(quarters, dimes, nickels, pennies);
but I still get 4 errors (error cannot find symbol) :(. Can anyone suggest a correction please?
Edit 3: Added the println statements and moved the constructor call to the bottom but whenever I run the program I cannot get the file to print the number of dollars and the number of cents?!
import javax.swing.JOptionPane;
/**
* A class to test the CoinCounter class
*/
public class CoinCounterTester
{
/**
* Tests methods of the CoinCounter class
* #param args not used
*/
public static void main(String[] args)
{
String quarter = JOptionPane.showInputDialog("Enter the quantity of quarters");
int quarters = Integer.parseInt(quarter);
String dime = JOptionPane.showInputDialog("Enter the quantity of dimes");
int dimes = Integer.parseInt(dime);
String nickel = JOptionPane.showInputDialog("Enter the quantity of nickels");
int nickels = Integer.parseInt(nickel);
String penny = JOptionPane.showInputDialog("Enter the quantity of pennies");
int pennies = Integer.parseInt(penny);
CoinCounter coinCounter = new CoinCounter(quarters, dimes, nickels, pennies);
System.out.println(coinCounter.getDollars());
System.out.println(coinCounter.getCents());
System.exit(0);
}
}
/**
* A CoinCounter has a specific number of cents. It can provide the number of dollars and the
* number of cents that it contains
*/
public class CoinCounter
{
// constants
//*** These are class constants so they need public static
public static final int QUARTER_VALUE = 25;
public static final int DIME_VALUE = 10;
public static final int NICKEL_VALUE = 5;
public static final int PENNY_VALUE = 1;
public static final int PENNY_PER_DOLLAR_VALUE = 100;
// instance field (one - holds the total number of cents EX: 8,534)
private int total;
/**
* Constructs a CoinCounter object with a specified number of pennies,
* nickels, dimes and quarters
* #param quarterAmount the amount of quarters
* #param dimeAmount the amount of dimes
* #param nickelAmount the amount of nickels
* #param pennyAmount the amount of pennies
*/
public CoinCounter(int quarters, int dimes, int nickels, int pennies)
{
total = quarters * QUARTER_VALUE + nickels * NICKEL_VALUE + dimes * DIME_VALUE + pennies;
}
// add remaining methods as described
/**
* getDollars returns the number of dollars in the CoinCounter
* #return the number of dollars
*/
public int getDollars()
{
int dollars = (int) total / PENNY_PER_DOLLAR_VALUE;
return dollars;
}
/**
* getCents returns the number the numbers of cents left over after the dollars are removed
* #return the number of cents left over
*/
public int getCents()
{
int cents = total % PENNY_PER_DOLLAR_VALUE;
return cents;
}
}
What you're looking for is a constructor call. You have all the values. You just need to create a CoinCounter to count them for you. An example of doing that would look like:
CoinCounter coinCounter = new CoinCounter(1, 2, 3, 4);
After you have your CoinCounter, you can call methods on it, like coinCounter.getCents(). You print things out using System.out.println(<whatever you want to print>). Those should be the three things you need to finish up.
Edit: Close! Look carefully at how you called the constructor and how I did it. What you did is like this:
CoinCounter coinCounter = new CoinCounter(int 1, int 2, int 3, int 4);
Compare it to my example above.
You only put the variable type there when you define the constructor, not when you call it.