Results 1 to 10 of 10

Thread: array list help

  1. #1
    dorno83 is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default array list help

    I'm having problem learning some simple java programming. I bought a book and this is one of the questions in the book. I was wondering if anyone could help me work through this.

    "Implement a class Purse. A purse contains a collection of coins. For simplicity, we will only store the coin names in an ArrayList<String>.
    Supply a method

    void addCoin(String coinName)

    Add a method toString to the Purse class that returns a string showing the coins in the purse in the format

    Purse[Quarter, Dime, Nickel, Dime]


    Thanks

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Link...

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    Take it one step at a time.
    Write the basic structure for a class.
    Add the two methods. Leave them empty for now, except for a System.out.println() to show it was called.
    Add a main to create an instance of the class and to call its two methods.
    Compile and test the program.
    Come back if any problems.
    Then we'll work on adding code to each of the methods.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    dorno83 is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default heres what i got so far

    OK so I went a little further and it semi works. I'm just not using my array list yet.

    import java.util.ArrayList;
    import java.util.Scanner;
    /**
    * Write a description of class PokerHand here.
    *
    * @author Matthew Doran
    * @version 06 NOV 08
    */
    public class PurseCoins
    {
    // instance variables - replace the example below with your own
    private String coin;

    /**
    * Constructor for objects of class PurseCoins.
    */
    public PurseCoins(String coinAdded)
    {
    coinAdded = coin;
    }

    public void addCoin(String coinName)
    {
    ArrayList<String> coin = new ArrayList<String>();
    coin.add("Quarter");
    coin.add("Dime");
    coin.add("Nickel");
    coin.add("Penny");

    }
    public static void main(String[] args)
    {
    Scanner in = new Scanner(System.in);
    System.out.print ("Enter coin to add: ");
    String coinAdded = in.nextLine();
    String addCoin = new String();

    if (coinAdded.equalsIgnoreCase(coinAdded))
    {
    addCoin = coinAdded;
    }



    System.out.println("Purse[ " + addCoin);

    // System.out.println(coin.get(0)
    // + coin.get(1)
    // + coin.get(2));
    }
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, is this code working? Do you have any questions on this?

    Please use code tags when you posting again.

  7. #7
    dorno83 is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default continuation

    The code works but its not right.
    Its supposed to show all the coins added to the purse not just one.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You mean this.

    Java Code:
    System.out.println(coin.get(0)
    So, loop through the list from first to last element.

  9. #9
    dorno83 is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default

    how do I do that?

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Do something like this,

    Java Code:
            for(int i = 0; i < coin.size(); i++) {
                System.out.println(coin.get(i));
            }
    But actually you cannot do that in main method, because VM cannot see that your list. Basically it scope is not valid in main method. You should define it in global level.

Similar Threads

  1. adding list to an array element
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 09-25-2008, 04:23 AM
  2. Array List problem
    By kurenai in forum New To Java
    Replies: 2
    Last Post: 06-25-2008, 07:30 AM
  3. How to convert List to Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  4. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 08:10 AM
  5. using an Array list
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 09:08 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
  •