14 Replies - 873 Views - Last Post: 20 July 2012 - 04:30 PM Rate Topic: -----

#1 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

First time writing a class

Posted 20 July 2012 - 03:22 PM

I'm having trouble writing my first class. I keep on getting the errors below.


C:\Users\David>javac Coin.java
Coin.java:14: error: missing method body, or declare abstract
public int flip( int heads, int tails );
^
Coin.java:23: error: missing method body, or declare abstract
public int getNumHeads( );
^
Coin.java:25: error: return outside method
return heads;
^
Coin.java:28: error: missing method body, or declare abstract
public int getNumTails( );
^
Coin.java:30: error: return outside method
return tails;
^
Coin.java:33: error: missing method body, or declare abstract
public int reset( int heads, int tails );
^
6 errors


The majority of them are "missing method body", which I am unsure about. Any tips or advice on how to fix that problem and the others?

import java.util.*;

public class Coin
{
	int heads = 0;
	int tails = 0;
	Random r = new Random(17);
	
	public Coin ( )
	{
		int coin;
	}
	
	public int flip( );
	{
		int side = r.nextInt(2);
		if( side == 0)
			heads++;
		else
			tails++;
	}
	
	public int getNumHeads( );
	{
		return heads;
	}
	
	public int getNumTails( );
	{
		return tails;
	}
	
	public int reset( int heads, int tails );
	{
		heads = 0;
		tails = 0;
	}
}
		


Is This A Good Question/Topic? 0
  • +

Replies To: First time writing a class

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: First time writing a class

Posted 20 July 2012 - 03:26 PM

Remove the semicolons from the end of your method signatures:

public int flip( );
                  ^ get rid of this and the rest

Was This Post Helpful? 1
  • +
  • -

#3 jared.deckard  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 46
  • Joined: 11-July 12

Re: First time writing a class

Posted 20 July 2012 - 03:29 PM

You have semicolons after each ( )
This is incorrect syntax.
Was This Post Helpful? 0
  • +
  • -

#4 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 03:30 PM

Thank you. That fix helped the majority of the errors. But now I get C:\Users\David>javac Coin.java
Coin.java:21: error: missing return statement
}
^
Coin.java:37: error: missing return statement
}
^
2 errors

I tried getting rid of the {} in those methods but that just caused even more errors. What is wrong with this part?

OHHHH wait. I think I know what I have to do. I have to add return tails or head statements, correct or no?

View PostGregBrannon, on 20 July 2012 - 03:26 PM, said:

Remove the semicolons from the end of your method signatures:

public int flip( );
                  ^ get rid of this and the rest

This post has been edited by Beastleewr11298: 20 July 2012 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: First time writing a class

Posted 20 July 2012 - 03:33 PM

Then you removed more than I suggested. It looks fine here after just removing the 4 semicolons. You'll have to backtrack and fix it yourself or post your updated code to see what else you "fixed."
Was This Post Helpful? 0
  • +
  • -

#6 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 03:38 PM

I get the C:\Users\David>javac Coin.java
Coin.java:21: error: missing return statement
}
^
Coin.java:37: error: missing return statement
}
^
2 errors

with this code
import java.util.*;

public class Coin
{
	int heads = 0;
	int tails = 0;
	Random r = new Random(17);
	
	public Coin ( )
	{
		int coin;
	}
	
	public int flip( )
	{
		int side = r.nextInt(2);
		if( side == 0)
			heads++;
		else
			tails++;
	}
	
	public int getNumHeads( )
	{
		return heads;
	}
	
	public int getNumTails( )
	{
		return tails;
	}
	
	public int reset( int heads, int tails )
	{
		heads = 0;
		tails = 0;
	}
}
		


that is with just the 4 semicolons removed.

View PostGregBrannon, on 20 July 2012 - 03:33 PM, said:

Then you removed more than I suggested. It looks fine here after just removing the 4 semicolons. You'll have to backtrack and fix it yourself or post your updated code to see what else you "fixed."

Was This Post Helpful? 0
  • +
  • -

#7 N1G3L2  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 28
  • Joined: 25-October 11

Re: First time writing a class

Posted 20 July 2012 - 03:46 PM

flip method needs a return statement

public int flip( )
	    {
	        int side = r.nextInt(2);
	        if( side == 0)
	           return heads++;
	        else
	            return tails++;
	    }



and for reset method you could do something like:

public void reset( int heads, int tails )
{
    heads = 0;
    tails = 0;
}
	    



or

public int reset( int num )
{
    return num = 0;
}



and just call that for heads and tails...
Was This Post Helpful? 1
  • +
  • -

#8 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 03:48 PM

Thank you very much. It compiled fine. Hopefully it works. Thanks again very much.

View PostN1G3L2, on 20 July 2012 - 03:42 PM, said:

flip method needs a return statement

public int flip( )
	    {
	        int side = r.nextInt(2);
	        if( side == 0)
	           return heads++;
	        else
	            return tails++;
	    }


This post has been edited by Beastleewr11298: 20 July 2012 - 03:49 PM

Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: First time writing a class

Posted 20 July 2012 - 03:50 PM

Sorry. I had another issue masking the return errors. Didn't know that would happen.
Was This Post Helpful? 0
  • +
  • -

#10 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 03:54 PM

Now that my Coin.java has compiled, here is the code currently.
import java.util.*;

public class Coin
{
	int heads = 0;
	int tails = 0;
	Random r = new Random(17);
	
	public Coin ( )
	{
		int coin;
	}
	
	public int flip( )
	{
		int side = r.nextInt(2);
		if( side == 0)
			return heads++;
		else
			return tails++;
	}
	
	public int getNumHeads( )
	{
		return heads;
	}
	
	public int getNumTails( )
	{
		return tails;
	}
	
	public void reset( int heads, int tails )
	{
		heads = 0;
		tails = 0;
	}
}

but now I get this error when I run my program to use the Coin class I just fixed. The program I am running, CoinTester.java is 100% correct because my professor created it, not me and he said to not alter it. Any help from here?



C:\Users\David>javac CoinTester.java
CoinTester.java:21: error: method reset in class Coin cannot be applied to given
types;
coin1.reset(); // sets numHeads and numTails back to zero;
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
CoinTester.java:32: error: method reset in class Coin cannot be applied to given
types;
coin2.reset(); // sets numHeads and numTails back to zero;
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
CoinTester.java:43: error: method reset in class Coin cannot be applied to given
types;
coin1.reset(); // sets numHeads and numTails back to zero;
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
3 errors
Was This Post Helpful? 0
  • +
  • -

#11 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 04:04 PM

That's fine, any advice on the current situation?

View PostGregBrannon, on 20 July 2012 - 03:50 PM, said:

Sorry. I had another issue masking the return errors. Didn't know that would happen.

Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: First time writing a class

Posted 20 July 2012 - 04:11 PM

It would be helpful to see the CoinTester class, but what I get from the errors you've posted is Coin.tester() is not supposed to have any arguments. The signature should be:

public void reset()

Which makes sense, because the method sets heads and tails to 0 and needs no arguments.
Was This Post Helpful? 0
  • +
  • -

#13 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 04:20 PM

yeah that fix helped it out completely. I got this as my output now

Flipping Coin1 20 times.
0 1 2 3 0 1 4 5 2 6 3 7 8 4 9 5 10 11 6 12
heads=7, tails=13

Flipping Coin2 10 times.
0 1 2 3 0 1 4 5 2 6
heads=3, tails=7

Flipping Coin1 again 35 times.
0 1 0 2 1 2 3 3 4 5 4 6 5 7 6 8 7 8 9 10 11 12 9 10 11 12 13 14 15 16 13 14 15 1
6 17
heads=18, tails=17

But where would I go to change the numbers that get printed out to print out T for true and F for false? I'm sure it is somewhere in my Coin.java but not sure where I would go to to add that option.


Also, if it helps here is the CoinTester program
/*
	CoinTester.java - tests the Coin class by
	constructing variables and calling it's methods
*/
public class CoinTester
{
  public static void main( String args[] )
  {
    Coin coin1 = new Coin();
    Coin coin2 = new Coin();

	// FLIP COIN1, PRINT RESULTS

    System.out.println("\nFlipping Coin1 20 times.");
    for (int i=0 ; i<20 ; ++i)
        System.out.print( coin1.flip() + " " ); // Equal chance of head or tail
    System.out.println();

    System.out.println("heads=" + coin1.getNumHeads() +
    		 ", tails=" + coin1.getNumTails() );
    coin1.reset(); // sets numHeads and numTails back to zero;

	// FLIP COIN2, PRINT RESULTS

    System.out.println("\nFlipping Coin2 10 times.");
    for (int i=0 ; i<10 ; ++i)
        System.out.print( coin2.flip() + " " ); // Equal chance of head or tail
    System.out.println();

    System.out.println("heads=" + coin2.getNumHeads() +
    		", tails=" + coin2.getNumTails() );
    coin2.reset(); // sets numHeads and numTails back to zero;

	// FLIP COIN2 AGAIN, PRINT RESULTS

    System.out.println("\nFlipping Coin1 again 35 times.");
    for (int i=0 ; i<35 ; ++i)
       System.out.print( coin1.flip() + " " ); // Equal chance of head or tail
    System.out.println();

    System.out.println("heads=" + coin1.getNumHeads() +
    		", tails=" + coin1.getNumTails() );
    coin1.reset(); // sets numHeads and numTails back to zero;
  }// END main
}//EOF


View PostGregBrannon, on 20 July 2012 - 04:11 PM, said:

It would be helpful to see the CoinTester class, but what I get from the errors you've posted is Coin.tester() is not supposed to have any arguments. The signature should be:

public void reset()

Which makes sense, because the method sets heads and tails to 0 and needs no arguments.

This post has been edited by Beastleewr11298: 20 July 2012 - 04:28 PM

Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: First time writing a class

Posted 20 July 2012 - 04:27 PM

You can look at your Coins class and see that nowhere does it have a print statement, so that's not where the statement printing the number of heads and tails is coming from.

However, the number of heads and tails may be coming from the methods getNumHeads() and getNumTails(). You'll have to check the instructor's code to verify.

I'm not sure what you want to change.
Was This Post Helpful? 0
  • +
  • -

#15 Beastleewr11298  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 13-June 12

Re: First time writing a class

Posted 20 July 2012 - 04:30 PM

Yes, I just posted the instructors code if you could find the offending code that is causing it to print the random number instead of a T or F, it is in my last post. I altered my code slightly and now it prints the T or F but it also prints the random numbers still. Here is my code as of now.
import java.util.*;

public class Coin
{
	int heads = 0;
	int tails = 0;
	Random r = new Random(17);
	
	public Coin ( )
	{
		int coin;
	}
	
	public int flip( )
	{
		int side = r.nextInt(2);
		if( side == 0)
		{
			System.out.print("T" + " ");
			return heads++;
		}
		else
		{
			System.out.print("F" + " ");
			return tails++;
		}
	}
	
	public int getNumHeads( )
	{
		return heads;
	}
	
	public int getNumTails( )
	{
		return tails;
	}
	
	public void reset( )
	{
		heads = 0;
		tails = 0;
	}
}
		


And this is the output I now get.

C:\Users\David>java CoinTester

Flipping Coin1 20 times.
F 0 F 1 F 2 F 3 T 0 T 1 F 4 F 5 T 2 F 6 T 3 F 7 F 8 T 4 F 9 T 5 F 10 F 11 T 6 F
12
heads=7, tails=13

Flipping Coin2 10 times.
F 0 F 1 F 2 F 3 T 0 T 1 F 4 F 5 T 2 F 6
heads=3, tails=7

Flipping Coin1 again 35 times.
T 0 T 1 F 0 T 2 F 1 F 2 F 3 T 3 T 4 T 5 F 4 T 6 F 5 T 7 F 6 T 8 F 7 F 8 T 9 T 10
T 11 T 12 F 9 F 10 F 11 F 12 F 13 F 14 F 15 F 16 T 13 T 14 T 15 T 16 T 17
heads=18, tails=17


View PostGregBrannon, on 20 July 2012 - 04:27 PM, said:

You can look at your Coins class and see that nowhere does it have a print statement, so that's not where the statement printing the number of heads and tails is coming from.

However, the number of heads and tails may be coming from the methods getNumHeads() and getNumTails(). You'll have to check the instructor's code to verify.

I'm not sure what you want to change.

This post has been edited by Beastleewr11298: 20 July 2012 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1