import java.util.*;

public class Coin

{
 private Random randomNumbers = new Random();

// flips a coin many times

 public void flipCoins()
 {
 Scanner input = new Scanner( System.in );

 int heads = 0; 
 int tails = 0; 
 int choice; 

 do
 {
 // display a menu
 System.out.println( "1. Toss Coin" );
 System.out.println( "2. Exit" );
 System.out.print( "Choice: " );
 choice = input.nextInt();
 if ( choice == 1 )
 {
 if ( flip() )
 heads++;
 else
 tails++;

 System.out.printf( "Heads: %d, Tails: %d\n", heads, tails );
 } 

 } while ( choice != 2 );
 } 

 
 public boolean flip()
 {
 return randomNumbers.nextInt( 2 ) == 1;
 }
 }

This program flips a coin on user's prompts and results in true and false for Heads and Tails respectively,and gives the no. of heads and tails in the end.My question is where can I have the main method in this program ? Please help !

Anywhere you want to as long as it is not declared inside another method/constructor/block.

Can a program run without " main " ? Also how can I have 2 public classes in the program to run ? Iam using Textpad .

Question 1.
Some class has to have a main, unless it's an Applet, Midlet, Portlet, Servlet, etc.

Question 2.
By having one class reference the other.

this is the other part of my program.But when I run altogether,it doesn't because I have 2 public class.Can you please tell me exactly where, looking at my program

import java.util.*;

public class Coin

{
private Random randomNumbers = new Random();

// flips a coin many times

public void flipCoins()
{
Scanner input = new Scanner( System.in );

int heads = 0;
int tails = 0;
int choice;

do
{
// display a menu
System.out.println( "1. Toss Coin" );
System.out.println( "2. Exit" );
System.out.print( "Choice: " );
choice = input.nextInt();
if ( choice == 1 )
{
if ( flip() )
heads++;
else
tails++;

System.out.printf( "Heads: %d, Tails: %d\n", heads, tails );
}

} while ( choice != 2 );
}


public boolean flip()
{
return randomNumbers.nextInt( 2 ) == 1;
}
}
private static CoinTest
{
public static void main( String args[] )
 {
 Coin application = new Coin();
 application.flipCoins();
 }
}

Every public class should be defined in a file named identically. So, it's obvious that a file cannot have two public classes. So you need to create two files for the two public classes and then make them to refer each other, being public, this is possible (as long as it falls in the classpath).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.