Results 1 to 3 of 3
  1. #1
    icanbeafrog is offline Member
    Join Date
    Jan 2015
    Rep Power

    Default Creating a method that resembles ArrayList .add()

    Hello, 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:

    Java Code:
    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.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Creating a method that resembles ArrayList .add()

    Using a loop would be fine. You just need to determine to what you are adding the coins.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Rep Power

    Default Re: Creating a method that resembles ArrayList .add()

    Are you perhaps creating your own linked list structure then? Or an array which you grow when you need it to?

    For the future: it usually helps to post a more generalized example of your code rather than only the one snippet that you want to zoom in on. That gives a proper context.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  2. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  3. Project that resembles DropBox
    By SerbianSergeant in forum Networking
    Replies: 2
    Last Post: 09-17-2011, 12:42 AM
  4. Creating an ArrayList from an ArrayList
    By Klahking in forum New To Java
    Replies: 17
    Last Post: 09-09-2010, 03:34 PM
  5. Creating ArrayList
    By dewitrydan in forum New To Java
    Replies: 9
    Last Post: 08-08-2010, 04:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts