Hello, I'm writing a program that acts as a 'pocket' where the user is able to enter a kind of coin, such as, a quarter and the amount of quarters it has. I was assigned to do 3 different class, the Coin Class in which the coins and their values can be instatiated from, a Pocket Class, where I have to write a method that can add the coins of the user (basically the method would act like ArrayList .add() ) and the PocketClass tester. I have already written most of the code, but I am stuck as to how I could write the following method:

public void addCoin(String s, int i)
 {
      // s is type of coin, you are using s to instantiate a Coin and get value
     // i is number of coins, you are using i to keep adding value to the totalValuefor
 }

Would I use a for-loop in order to keep track of the number of coins? I would use ArrayList but the assignment calls for creating a method similar to that of .add()

Thank you in advance.

addCoin is similar to add, even if you use an ArrayList. You can also use an array, and for each .add create a new Array, with one more element, containing the new one.

but just the snippet you posted above is way to little for us to know what way you are going.

Your pocket class will hold a number of coins, so it will have to have some kind of Collection as an instance variable to hold all the instances of Coin. Yes, you could use an array Coin[], or an ArrayList<Coin>, but before chosing think through all the ways you will want to use it.
For example, if you keep adding and removing coins, an LinkedList<Coin> may be better, or, looking at the signature of the addCoin method, it may be better to use something like a Map<Coin, Integer> to hold a count for each kind of Coin. And you don't have to use a Collection from the API - you can create your own (linked list is particularly easy) if you really want.

Edited 1 Year Ago by JamesCherrill

From your requirement, it is unclear how you implement your Coin and Pocket class. What is in your Coin class? I assume that your Coin class constructor is similar to...

public Coin (String type) {
  if (!isValidCoinType(type)) {
    // Throw an error or do something
    // The isValidCoinType() method can be anything
    //   depending on how you verify it. It could
    //   be a whole implementation inside the
    //   constructor instead of a method.
  }
  // OK, assign the coin type to the object
}

If so, it means that one instance per Coin class (no number). Now, how does your Pocket class look like (public/private variable inside the class)?

Edited 1 Year Ago by Taywin

I don't think that's a safe assumption. Even if there's just a Coin class, how about

public Coin(String name, int value) { ...

or maybe

 class Coin ...
    class Dollar extends Coin ... // no parameters needed
    class Nickel extends Coin ...

(which will make impossible most of the errors you can get with the first version)

or, even better, if you think any one Dollar is the same as any other Dollar

enum Coin {DOLLAR(100), NICKEL(5) ....
This article has been dead for over six months. Start a new discussion instead.