0

Heres the method where I try to add everything. What im trying to do is add up a series of coins, which are named penny,dime...etc. And i gave all of them a value within the enum. But how do I access each of the coins within the array, then add up their values?

public double totalValue(Coin[] coins)
{
    double sum = 0;

    //computes and returns the monetary value of all the coins in the jar
    for(int i = 0; i < coins.length; i++) 
    {
        double coinValue = coins[i].CoinName.getCoinValue();
        sum = sum + coins[i];
        System.out.println(sum);
    }
    return sum; //change this!
}

and here is where the values for the enum are defined.

public enum CoinName
{
     PENNY(.01), NICKEL(.05), DIME(.10), QUARTER(.25), FIFTY_CENT(.50), DOLLAR(1.0);

private double value;
private double coinValue;

private CoinName(double value)
{
    this.coinValue = value;
}

public double getCoinValue() 
{

    return coinValue;
}

}

///// I have just added my coin class.

import java.util.Random;
public class Coin
{

public static long SEED = System.currentTimeMillis();
public static Random RANDOM = new Random(SEED);

//private instance variables denomination, year,  and sideUp: year is an int,        denomination is of type  CoinName and  sideUp is of type  CoinSide
private CoinName denomination;
private CoinSide sideUp;
private int year;

public Coin(CoinName denomination,int year) 
{
    this.denomination = denomination;
    this.year = year;

    int side = Coin.RANDOM.nextInt(2);
    if (side == 0)
    {
        sideUp = CoinSide.HEADS;
    }
    else 
        sideUp = CoinSide.TAILS;
}

//Accessors for denomination, year and sideUp

public CoinName setDenomination() 
{   
    int i = 0;
    i = Coin.RANDOM.nextInt(6);
    if (i == 0)
    {
    denomination = CoinName.PENNY;
    }
    if (i == 1)
    {
    denomination = CoinName.NICKEL;
    }
    if (i == 2)
    {
    denomination = CoinName.DIME;
    }
    if (i == 3)
    {
    denomination = CoinName.QUARTER;
    }
    if (i == 4)
    {
    denomination = CoinName.FIFTY_CENT;
    }
    if (i == 5)
    {
    denomination = CoinName.DOLLAR;
    }

    return denomination;
}

public CoinName getDenomination()
{
    return denomination;
}

public void setSideUp(CoinSide sideUp)
{
    sideUp = sideUp;
}

public CoinSide getSideUp()
{
    return sideUp;
}

public void setYear(int year)
{
    year = RANDOM.nextInt(2013-1873) + 1873;
}

public int getYear()
{
    return year;
}

//the standard toString method that prints out a coin in the format “PENNY/1990/HEADS”
public String toString()
{
    return denomination + "/" + year + "/" + sideUp;
}

//the standard equals method that checks if two Coin objects are equal – they are equal if the denominations are identical
public boolean equals(Object obj)
{
    if (obj instanceof Coin){
        Coin d = (Coin)obj;
        if (this.getDenomination()==d.getDenomination())
            return true;
        else
            return false;
    }
    return false;
}
public void toss()
{
    //flip the coin
    //Use the object RANDOM to generate random numbers
    int side = Coin.RANDOM.nextInt(2);
    if (side == 0)
    {
        sideUp = CoinSide.HEADS;
    }
    else 
        sideUp = CoinSide.TAILS;


}

}

7
  • 1
    What is the problem/error? Expected/Example input/output? Also what is a Coin? Commented Sep 19, 2013 at 21:37
  • 1
    What's wrong with your current code? What happens when you try to run it? Commented Sep 19, 2013 at 21:37
  • 1
    What does your class Coin look like? sum = sum + coins[i]; isn't going to work, because sum is an int and coins[i] is a Coin object. You can't add a Coin object to an int. Commented Sep 19, 2013 at 21:37
  • I would recommend keeping track of cents instead of dollars. That will let you use ints, and help avoid floating point errors. Commented Sep 19, 2013 at 21:54
  • The problem is that when I run it, gives gives me the java.lang.Error: Unresolved compilation problems: Cannot make a static reference to the non-static method totalValue(Coin[]) from the type CoinJar. This happens in the driver. I talked to my professor today, and I had everything static, he told me to change it so it wasnt, so I did. So I cant just change things back to static. Commented Sep 19, 2013 at 23:30

2 Answers 2

1

A Coin has a denomination, which holds the value of the coin. With how you have defined things, to sum the values of an array of coins you have to first get the denomination and then extract the value from it:

for(int i = 0; i < coins.length; i++) 
{
    CoinName denomination = coins[i].getDenomination();
    sum = sum + denomination.getCoinValue();
    System.out.println(sum);
}

Note that for this to work the array of coins must be full, with no null values, and each coin must have a denomination.

Sign up to request clarification or add additional context in comments.

6 Comments

Even better would be a for each: for ( Coin coin : coins ) sum += coin.getCoinValue();. OP also needs to decide Coin vs. CoinName.
When i try both ways, it says that getCoinValue() is undefined for type Coin.
What is "Coin"? You have only showed us a class called "CoinName".
Ive posted my Coin class. It pretty much just defines what a coin is.
Yes it does, thank you very much! @Joni The only thing now is that when I try to run the program, it tells me I cannot make a static reference to totalValue, which is not static, how would a fix this?
|
0

Assuming that your Coin class has a CoinName property which of type CoinName then you need to change the line

sum = sum + coins[i];

to use the coinValue you get the line before. So change to

sum = sum + coinValue;

or typically this would be

sum += coinValue;

You also have in your Enum two doubles which, one of which is not needed, remove the value

1 Comment

I had that in there before when i was testing something else, i forgot to remove it, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.