Results 1 to 10 of 10
Thread: array list help
- 11-06-2008, 11:12 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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
- 11-06-2008, 11:26 PM #2
Link...
Maybe this will help:
List Implementations (The Java™ Tutorials > Collections > Implementations)Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 11-07-2008, 12:16 AM #3
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.
- 11-07-2008, 05:32 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Yes take a single step at a time. Can you show what you have done so far here.
- 11-07-2008, 07:45 AM #5
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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));
}
}
- 11-07-2008, 07:59 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Ok, is this code working? Do you have any questions on this?
Please use code tags when you posting again.
- 11-07-2008, 10:43 AM #7
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
continuation
The code works but its not right.
Its supposed to show all the coins added to the purse not just one.
- 11-07-2008, 10:47 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
You mean this.
Java Code:System.out.println(coin.get(0)
- 11-07-2008, 10:51 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
how do I do that?
- 11-07-2008, 10:56 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
Do something like this,
Java Code:for(int i = 0; i < coin.size(); i++) { System.out.println(coin.get(i)); }
Similar Threads
-
adding list to an array element
By Preethi in forum New To JavaReplies: 5Last Post: 09-25-2008, 05:23 AM -
Array List problem
By kurenai in forum New To JavaReplies: 2Last Post: 06-25-2008, 08:30 AM -
How to convert List to Array
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:37 PM -
Array List Problem
By khamuruddeen in forum New To JavaReplies: 1Last Post: 12-22-2007, 09:10 AM -
using an Array list
By toad in forum New To JavaReplies: 1Last Post: 11-18-2007, 10:08 PM
Bookmarks