Ernie S
Ernie S

Reputation: 131

Only allowed one instance field but need more?

I am a student struggling to finish a particular lab assignment by my teacher. I having problems when I try to compile this in Jcreator, I get an error, cannot find symbol. I think this is due to the fact that I haven't created "dollars" and "cents". The teacher said students could only have one instance field so how can I fix this?

Edit: Thanks, I fixed the modulus operator and put the return values in.

I am getting errors in the "int dollars = (int) total / PENNIES_PER_DOLLAR_VALUE;" line and the "int cents = total % PENNIES_PER_DOLLAR_VALUE;".

Thanks

 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 / PENNIES_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 % PENNIES_PER_DOLLAR_VALUE;
            return cents;
    }


 }

Upvotes: 1

Views: 113

Answers (2)

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

Your getDollars() and getCents() methods aren't returning anything when they declare that they return int's.

public int getDollars()
{
    int dollars = (int) total / PENNIES_PER_DOLLAR_VALUE;
    return dollars;
}

public int getCents()
{
    int cents = total % PENNIES_PER_DOLLAR_VALUE;
    return cents;
}

EDIT:

The issue is the naming of your constants.

You define this:

public static final int PENNY_PER_DOLLAR_VALUE = 100;

But you use this:

PENNIES_PER_DOLLAR_VALUE

Upvotes: 1

goto10
goto10

Reputation: 4370

You created a constant named PENNY_PER_DOLLAR_VALUE:

public static final int PENNY_PER_DOLLAR_VALUE = 100;

But then later you refer to PENNIES_PER_DOLLAR_VALUE:

int dollars = (int) total / PENNIES_PER_DOLLAR_VALUE;

and

int cents = total % PENNIES_PER_DOLLAR_VALUE;

That's the symbol it can't find.

Upvotes: 1

Related Questions