i need help on my project
1. Write a program that will print out statistics for eight coin tosses. The user will input either an “h” for heads or a “t” for tails for the eight tosses. The program will then print out the total number and percentages of heads and tails. Use the increment operator to increment the number of tosses as each toss is input. For example, a possible sample dialog might be:

For each coin toss enter either ‘h’ for heads or ‘t’ for tails.

First toss: h
Second toss: t
Third toss: t
Fourth toss: h
Fifth toss: t
Sixth toss: h
Seventh toss: t
Eighth toss: t

Number of heads: 3
Number of tails: 5
Percent heads: 37.5
Percent tails: 62.5

Since we do not know how to use if statements, loops or the conditional or ternary operator, none of those structures can be used. This problem can be solved using only the material in Chapters 1 - 2.

2.Use JOptionPane for I/O (Input/Output). Test your program with several test cases and PrintScreen the results. Print the tosses and the results screens for the first test case. Just print the results for the other cases.

Recommended Answers

All 8 Replies

What help do you need?

whats with everyone expecting help for homework from "us" when they don't even have any code to start

1. Write a program that will print out statistics for eight coin tosses. The user will input either an “h” for heads or a “t” for tails for the eight tosses. The program will then print out the total number and percentages of heads and tails. Use the increment operator to increment the number of tosses as each toss is input. For example, a possible sample dialog might be:

For each coin toss enter either ‘h’ for heads or ‘t’ for tails.

First toss: h
Second toss: t
Third toss: t
Fourth toss: h
Fifth toss: t
Sixth toss: h
Seventh toss: t
Eighth toss: t

Number of heads: 3
Number of tails: 5
Percent heads: 37.5
Percent tails: 62.5

Since we do not know how to use if statements, loops or the conditional or ternary operator, none of those structures can be used. This problem can be solved using only the material in Chapters 1 - 2.

2.Use JOptionPane for I/O (Input/Output). Test your program with several test cases and PrintScreen the results. Print the tosses and the results screens for the first test case. Just print the results for the other cases.


ooppss... sorry about that i have been started it but i can't get the right equation on how to get the number of toss the count and the percentage... so can u please guide me how to do the formula or on how to solve it?

import javax.swing.*;
public class CoinToss
{
	public static void main (String [] arg)
	{
	    
	    
    double NumberOfHead, NumberOfTail;
     	
	
	String HT= ""; 

	HT= JOptionPane.showInputDialog("First toss:");
		
	HT	= JOptionPane.showInputDialog("Second toss:" );
		
	HT	= JOptionPane.showInputDialog("Third toss: " );
		
	HT	= JOptionPane.showInputDialog("Fourth toss: ");
		
	HT	= JOptionPane.showInputDialog("Fifth toss: ");
		
	HT	= JOptionPane.showInputDialog("Sixth toss: ");
		
	HT	= JOptionPane.showInputDialog("Seventh toss: ");
		
	HT	= JOptionPane.showInputDialog("Eighth toss: " );
		
				

	   System.out.println("First toss: " + HT); 		
	   System.out.println("Second toss: "+ HT);
	   System.out.println("Third toss:  " + HT);
	   System.out.println("Fourth toss: "+ HT);
	   System.out.println("Fifth toss: "+ HT);
	   System.out.println("Sixth toss: "+ HT);
	   System.out.println("Seventh toss: "+ HT);
	   System.out.println("Eighth toss: " + HT);
	}
}

What help do you need?

hi i need help on how to get the count and percentage

If its a head increment NumberOfHead, if its a tail increment NumberOfTail. Those give you the count, do the usual arithmetic to get the %.
PS: You use the same String (HT) to hold each of the eight answers, so each one overwrites the previous one - that's not what you want to happen.

Here is own code in which i have put the method to calculate number of head and tail and their respective percentage without using if-else operator. Please note this would run if user input small letter. If you want to put it for capital letter then you can change it to lowercase using toLower method.

import javax.swing.*;
public class CoinToss
{
	public static void main (String [] arg)
	{


		int NumberOfHead=0, NumberOfTail=0;
		double PercentageOfHead=0, PercentageOfTail=0;

		String HT= "";
	
		HT= JOptionPane.showInputDialog("First toss:");
		System.out.println("First toss: " + HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Second toss:" );
		System.out.println("Second toss: "+ HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Third toss: " );
		System.out.println("Third toss: " + HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Fourth toss: ");
		System.out.println("Fourth toss: "+ HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Fifth toss: ");
		System.out.println("Fifth toss: "+ HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Sixth toss: ");
		System.out.println("Sixth toss: "+ HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Seventh toss: ");
		System.out.println("Seventh toss: "+ HT);
		NumberOfTail+=HT.charAt(0)/((int)('t'));
		HT = JOptionPane.showInputDialog("Eighth toss: " );
		System.out.println("Eighth toss: " + HT);
	
		//calculation
			NumberOfTail+=HT.charAt(0)/((int)('t'));
		NumberOfHead=8-NumberOfTail;
		 PercentageOfHead=(double)NumberOfHead/8*100;
		PercentageOfTail=100- PercentageOfHead;

		//Output
		 System.out.println("Number of head : "+NumberOfHead);
		System.out.println("Number of Tail : "+NumberOfTail);
		System.out.println("Percentage of head : "+PercentageOfHead);
		System.out.println("Percentage of Tail : "+PercentageOfTail);
	}
}

@navedalam

Please please do NOT do this! We are trying to help Sheena to become a Java programmer by giving pointers in the right direction. That way she will learn to think about Java, use the API reference, and develop the skills she will need to program on her own.
All you have done is to give her a way to cheat her way to a "C" grade. You teach NOTHING. Your code uses non-standard Java conventions and doesn't even meet the spec in the first post.

@Sheena - I strongly recommend that you ignore navedalam's post - it won't help you learn. "No pain, no gain!" J.

@navedalam

Please please do NOT do this! We are trying to help Sheena to become a Java programmer by giving pointers in the right direction. That way she will learn to think about Java, use the API reference, and develop the skills she will need to program on her own.
All you have done is to give her a way to cheat her way to a "C" grade. You teach NOTHING. Your code uses non-standard Java conventions and doesn't even meet the spec in the first post.

@Sheena - I strongly recommend that you ignore navedalam's post - it won't help you learn. "No pain, no gain!" J.

@james thank u for you concern don't worry i'm not going to copy that coz i want to learn
@navedalam thank u also i appreciated it

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.