Results 1 to 7 of 7
  1. #1
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Question Help with a program

    Hello,

    i am trying to write a coin program:

    Write a class called Coin which will be used by a program called CountFlips.
    1. The class, is an object created from this program. It is composed of data values and methods. It should contain a main method and should output the number of flips done (an integer that will be read from the user), the number of heads and tails that occur.

    2. The CountFlip class will flip a coin multiple times and counts the number of ‘heads’ and ‘tails’ that result. This class should have a method called flip() of type void; a method called isHead() of type Boolean and a toString() method that will return the current face of the coin as a string.



    So i created 2 classes, one called Coin.java & the other CountFlips.java, Thats all what i got so far, i know its Very noobish... and missing alot of stuff, i hope someone could help me with it, and provide some kind of explanation.


    PHP Code:
    package test;
    
    public class Coin {
    	
    	private final int heads = 0;
    	private final int tails = 1;
    	private int facetype;
    	private int flips;
    	
    	
    	public Coin()
    	{
    		facetype = 0; //Initial coin face (Heads=0)
    	}
    	 
    	public int flip()
    	{
    		facetype = (int) (Math.random() + 1); //Facetype = 1 OR 2, and i guess this is wrong...
    		
    		return facetype;
    		
    	}
    	
    	public boolean isHeads()  //Just threw this in here for the boolean part
    	{
    	return true;
    	} 
    	
    	public void setFaceType(int type) //Setter
    	{
    		facetype = type;
    	}
    	
    	public int getFaceType() //Getter
    	{
    		return facetype;
    	}
    	
    	public String toString()  // <--- I require help with how to write a toString method, i still dont find it very clear.
    	{
    		
    	}
    	
    	
    
    }
    
    
    //-----------------------------------------------------------------------------------------------------------------------
    //(2nd class)
    
    package test;
    
    import java.util.Scanner;
    
    public class CountFlips {
    	
    	
    	public static void main (String[] args)
    	{
    		int count = 0;
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.println("How many times would you like to flip the coin?");
    		int flips = scan.nextInt();
    		
    		Coin coin1 = new Coin();
    		
    		for(int i=0; i<flips; i++)
    		{coin1.flip();}
    		
    		System.out.println("Number of flips: " + flips);
    		System.out.println("-# of Heads: " + coin1.getFaceType());
    		System.out.println("-# of Tails: " + coin1.getFaceType());
    		
    		
    		
    		
    	}
    
    }
    Thanks in advance,
    Regards,
    toiletpaper
    Last edited by toiletpaper; 05-01-2014 at 07:09 PM.

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

    Default Re: Help with a program

    What exactly is your question? What exactly are you stuck on or confused about?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  3. #3
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Help with a program

    I have no idea how to write the toString() method, i have several mistakes in the coin.java too, i was hoping if someone could help me identify whats wrong and fix them. Furthermore, the code isn't complete. I've spent some time editing the code, to no avail...

    It would be awesome if you could provide some similar exercise (a bit simpler maybe, or quite similar, it would probably help me understand the whole Class/Object/Method concept much more, i've tried searching online, but couldn't find anything similar).
    Last edited by toiletpaper; 05-01-2014 at 07:13 PM.

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

    Default Re: Help with a program

    You aren't going to find an example exactly like this. That's part of the fun of programming: every project is different.

    That being said, I'm sure you can find examples that are similar to the *individual parts* of your program. For example, I'm sure you can find examples that use a toString() method. And I'm sure you can google individual compiler errors to fix them as well.

    Programming is all about breaking a big problem down into a bunch of smaller problems, then solving the smaller problems one at a time.

    Which small problem are you stuck on?
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  5. #5
    toiletpaper is offline Member
    Join Date
    May 2014
    Posts
    43
    Rep Power
    0

    Default Re: Help with a program

    I mainly dont understand the toString() i've found some stuff online but i still dont rly get it...

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

    Default Re: Help with a program

    What don't you get about it?

    Consider this bit of code:

    Java Code:
    Coin c = new Coin();
    c.flip();
    System.out.println(c);
    What should that print out? Java doesn't automagically know how to represent an Object as a String, so you have to tell it. That is what the toString() method is for. Try returning different values from the toString() method and test that. You should be testing individual pieces of your program, not trying to write the whole thing in one go.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

  7. #7
    Silvertale is offline Member
    Join Date
    May 2014
    Posts
    11
    Rep Power
    0

    Default Re: Help with a program

    The toString should have something like this:

    if(heads == true){//you can actually leave off the ==true here
    return "you got heads!";
    }else{
    return "you got tails!";
    }

    you might also want to change the head/tails thing to just a boolean value instead of two seperate variables.

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. Replies: 4
    Last Post: 11-21-2012, 02:19 PM
  3. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  4. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  5. Replies: 0
    Last Post: 04-04-2008, 03:40 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
  •