Results 1 to 4 of 4
- 10-28-2011, 07:43 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 55
- Rep Power
- 0
Creating a random pocket of Change
Alright, I created a superclass called coin and then made subclasses for all the other kinds of American coin (dime , quarter, nickel, penny)
now in my pocket class I want to some how generate a random pocket of change.So that means different mixtures of change and different amounts of coins, but i havent the slightest where to start beside i know im going to be needing java.util.Random; :) can anyone give me a hint?
Super Class:
Java Code:public class coin{ int value; String name; public coin(){ value=0; name="Canadian Penny"; } public String toString() { return String.format("This coin is a %s\n Its value is:%s", name, value); } }
Subclass:
Java Code:public class dime extends coin{ dime(){ value=10; name="Dime"; } public String toString() { return String.format("This coin is a %s\n Its value is:%s",name,value); } }
Pocket class that uses the previous classes:
Java Code:import java.util.Random; class pocket{ public static void main(String[] args){ coin leftPocket[]= new coin[5]; leftPocket[0]= new dime(); leftPocket[1]= new nickel(); leftPocket[2]= new quarter(); leftPocket[3]= new coin(); leftPocket[4]= new penny(); for(coin x:leftPocket){ System.out.println(x.toString()); } } }Last edited by JosAH; 10-28-2011 at 08:21 AM. Reason: changed the [quote] tags to [code] tags ...
- 10-28-2011, 03:48 PM #2
Re: Creating a random pocket of Change
How would you do this by hand? Pretend your Math.random() is some dice, and you have some rolls of coins. What would you do to create a random collection of coins from the rolls?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-28-2011, 06:13 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 55
- Rep Power
- 0
Re: Creating a random pocket of Change
lol I guess I would roll once to see how many pieces of change are in the pocket, then set each side of the die equal to a different coin and roll once for each coin to determine its value.
thank you for forcing me to think , but caring enough to guide me . I will try this to the best of my ability's and then post to see if its a efficent way of doing it.
- 10-28-2011, 06:20 PM #4
Re: Creating a random pocket of Change
Don't worry too much about efficiency.
But pretend you have a really dumb friend, who doesn't really know anything about money or counting. Give him some dice and some rolls of coins. Write out directions that your friend could follow (better make them simple, remember how dumb he is) to get a random pocket of change. When you have that written out, you'll have an algorithm that should be pretty easy to translate into code.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Pick random array index and change the element value
By Space Ranger in forum New To JavaReplies: 8Last Post: 03-29-2011, 10:18 PM -
hard question .. help me .. about creating random numbers
By soldier in forum New To JavaReplies: 5Last Post: 12-21-2009, 01:17 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
(Location based Service) application for Pocket PC
By talk_to_vivekmishra in forum CLDC and MIDPReplies: 0Last Post: 08-14-2007, 08:23 AM -
Creating random sentences
By bluekswing in forum New To JavaReplies: 4Last Post: 06-27-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks