2

trying to convert a entered number into quarters, nickels, dimes, and pennies. Having a few problems:

    public class Coins {
public static void main(String[] args){

private int quarters, dimes, nickels, pennies;

public void CoinsToChange(TotalCoins){
    quarters = (int)(TotalCoins/25);
    TotalCoins %= 25;
    dimes = (int)(TotalCoins/10);
    TotalCoins %= 10;
    nickels = (int)(TotalCoins/5);
    TotalCoins %= 5;
    pennies = (int)(TotalCoins/1);
    TotalCoins %= 1;

    System.out.println("Quarters = " + quarters + "\nDimes = " + dimes + "\nNickels = " + nickels + "\nPennies = " + pennies);
    }
}
    }

and this is my tester class: (im having problems calling the CoinsTotChange method.)

     import java.util.Scanner;

    public class CoinsTester {
    Coins money = new Coins();
    money.CoinsToChange(changeMoney);
    Scanner kybd = new Scanner(System.in);
    int changeMoney = kybd.nextInt();

}

epascarello
  • 204,599
  • 20
  • 195
  • 236
SplattColor
  • 115
  • 2
  • 13

4 Answers4

3

Try changing the order of operations. You typically cannot use an object/variable before it has been declared/instanced.

Coins money = new Coins();
Scanner kybd = new Scanner(System.in);
int changeMoney = kybd.nextInt();
money.CoinsToChange(changeMoney);
Tank
  • 1,006
  • 7
  • 17
1

There are lots of syntax issues but your overall computations are correct. Try something like this:

public class Coins {
    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        int totalPennies = kybd.nextInt();
        printMoneyAsChange(totalPennies);
    }

    public static void printMoneyAsChange(int pennies) {
        int quarters = pennies / 25;
        pennies %= 25;

        int dimes = pennies / 10;
        pennies %= 10;

        int nickels = pennies / 5;
        pennies %= 5;

        System.out.println(String.format("Quarters = %d\nDimes = %d\nNickels = %d\nPennies = %d", quarters, dimes, nickels, pennies));
    }
}
Erik Gillespie
  • 3,929
  • 2
  • 31
  • 48
1

These are the problems:

  • In your Coins class, you have attempted to wrap everything inside the main method. You need to declare your method and variables separately, and then call them in the main method.
  • Instead of making a main method in your Coins class, it should be in your tester class.
  • When you declare a method with parameters, you need to specify the type. So instead of public void CoinsToChange(TotalCoins) it should be public void CoinsToChange(int TotalCoins).
  • Finally, in your tester class, you were calling money.CoinsToChange(changeMoney) before you declared and assigned changeMoney in int changeMoney = kybd.nextInt(). You need to put that line after it instead of before it.

Making as few changes as possible, I got your code to work like so:

public class Coins {

    private int quarters, dimes, nickels, pennies;

    public void CoinsToChange(int TotalCoins) {
        quarters = (int) (TotalCoins / 25);
        TotalCoins %= 25;
        dimes = (int) (TotalCoins / 10);
        TotalCoins %= 10;
        nickels = (int) (TotalCoins / 5);
        TotalCoins %= 5;
        pennies = (int) (TotalCoins / 1);
        TotalCoins %= 1;

        System.out.println("Quarters = " + quarters + "\nDimes = " + dimes
                + "\nNickels = " + nickels + "\nPennies = " + pennies);
    }
}

Tester class:

 import java.util.Scanner;

    public class CoinsTester {

        public static void main(String[] args) {
            Coins money = new Coins();
            Scanner kybd = new Scanner(System.in);
            int changeMoney = kybd.nextInt();
            money.CoinsToChange(changeMoney); // This line needs to be at the bottom
        }
    }

NOTE: Some of the other answers also give you good suggestions having to do with better variable names and use of String formatting. It would be good to take their advice. I specifically wanted to address the compiler errors.

James Dunn
  • 8,064
  • 13
  • 53
  • 87
0

As far as I see there is whole bunch of problems with provided code.

In your first code sample you introduce function declaration and private fields within

public static void main(String[] args){ }

, so your class will not compile and hence you'll be unable to use it from some other class

Then, you are using variable before declaring it as @Tank suggested in his answer.

It's also considered to be a good practice to stick with generally accepted naming conventions and formatting.

user1455836
  • 752
  • 6
  • 18