Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hello guys need help withe the following, doesn't work for some reasons

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hello guys need help withe the following, doesn't work for some reasons

    import java.lang.*;
    import java.io.*;
    public class UseCoin
    {
    public static void main(String[] agrs) throws IOException
    {
    Coin dime;
    //Coin.HEADS = false; // ERROR
    dime = new Coin(10);
    System.out.println(dime);
    //dime.face = false; //ERROR
    Coin[] pocket;
    pocket = new Coin[10];

    int sum = 0;
    for(int i = 0; (i< pocket.length) && (sum < 100); i++)
    {
    int cvalue = (int)(Math.random()*25) + 1;
    pocket[i] = new Coin(cvalue);
    sum +=cvalue;
    }
    System.out.println("In the pocket: " + sum);
    for(int i = 0; i< pocket.length; i++)
    {
    System.out.println(pocket[i]);

    try {
    i = System.in.read();
    }
    catch ( IOException iox ) {
    System.out.println( "there was an error: " + iox );
    }

    System.out.println("pocket["+i+"].compareTo(["+i+"]) = "+pocket[i].compareTo(pocket[i]));
    }
    }
    }

    //---------------------------------------------------------------------------------------------------------------------------

    public class Coin
    {
    public static final int HEADS = 0;
    public static final int TAILS = 1;

    private int value = 1;
    private int face;

    public Coin()
    {
    value = 1;
    flip();
    }

    public Coin(int v)
    {
    value = v;
    flip();
    }

    public void flip()
    {
    face = (int)(Math.random()*2);
    }

    public int getValue()
    {
    return value;
    }

    public String getFace()
    {
    if (face == HEADS)
    return "HEADS";
    else
    return "TAILS";
    }

    public int compareTo( Coin coin )
    {
    if ( value == coin.getValue() )
    {
    return 0;
    }
    else if ( value < coin.getValue() )
    {
    return -1;
    }
    else
    {
    return 1;
    }
    } // end of compareTo()

    public String toString()
    {
    return "Value: " + value + ", Face: " + getFace();
    }
    }


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Hello guys need help withe the following, doesn't work for some reasons

    doesn't work for some reasons
    Can you be more specific?

Similar Threads

  1. Hello guys
    By geekboy in forum Member Introductions
    Replies: 1
    Last Post: July 27th, 2011, 02:51 AM
  2. Hi guys
    By nextngema in forum Java SE APIs
    Replies: 2
    Last Post: July 17th, 2010, 04:34 PM
  3. hey guys....
    By prince joseph in forum Member Introductions
    Replies: 2
    Last Post: March 27th, 2010, 11:15 PM