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

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by verruckt24
0

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

0

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.

0

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();
 }
}
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.