Results 1 to 4 of 4
  1. #1
    fresh83 is offline Member
    Join Date
    Dec 2009
    Posts
    55
    Rep Power
    0

    Default 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 09:21 AM. Reason: changed the [quote] tags to [code] tags ...

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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!

  3. #3
    fresh83 is offline Member
    Join Date
    Dec 2009
    Posts
    55
    Rep Power
    0

    Default 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.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default 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

  1. Pick random array index and change the element value
    By Space Ranger in forum New To Java
    Replies: 8
    Last Post: 03-29-2011, 11:18 PM
  2. Replies: 5
    Last Post: 12-21-2009, 02:17 PM
  3. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  4. (Location based Service) application for Pocket PC
    By talk_to_vivekmishra in forum CLDC and MIDP
    Replies: 0
    Last Post: 08-14-2007, 09:23 AM
  5. Creating random sentences
    By bluekswing in forum New To Java
    Replies: 4
    Last Post: 06-27-2007, 06:45 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
  •