Question

Heads or Tails Create a JavaFX application that simulates a coin being tossed. When the user clic...

Heads or Tails

Create a JavaFX application that simulates a coin being tossed. When the user clicks a button, the application should generate a random number in the range of 0 to 1. If the number is 0, the coin has landed on “heads,” and if the number is 1, the coin has landed on “tails.” Use an ImageView component, and the coin images that you will find in this book’s Student Sample Programs to display the side of the coin when it is tossed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CoinTossingFXML.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane id="AnchorPane" prefHeight="298.0" prefWidth="370.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="cointossing.FXMLDocumentController">
<children>
<Label layoutX="127.0" layoutY="14.0" text="Tossing the Coin">
<font>
<Font name="System Bold" size="15.0" />
</font>
</Label>
<ImageView fx:id="coinView" fitHeight="150.0" fitWidth="150.0" layoutX="110.0" layoutY="54.0" pickOnBounds="true" preserveRatio="true" />
<Button fx:id="tossButton" layoutX="158.0" layoutY="252.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="54.0" text="Toss" />
</children>
</AnchorPane>

FXMLDocumentController.java

import java.io.File;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class FXMLDocumentController implements Initializable {
  
@FXML private ImageView coinView;
@FXML private Button tossButton;
  
private int tossSide = -1;
  
@Override
public void initialize(URL url, ResourceBundle rb) {
  
tossButtonAction();
  
tossButton.setOnAction((ActionEvent event) -> {
tossButtonAction();
});
}
  
public void tossButtonAction()
{
tossSide = generateRandomNumber();
File file;
Image image;
  
// if tossSide is 0, coin has landed on HEADS
switch(tossSide)
{
case 0:
file = new File("src/coin_head.JPG");
image = new Image(file.toURI().toString());
coinView.setImage(image);
break;
  
case 1:
file = new File("src/coin_tail.JPG");
image = new Image(file.toURI().toString());
coinView.setImage(image);
break;
  
default:
tossSide = generateRandomNumber();
}
}
  
public int generateRandomNumber()
{
Random rand = new Random();
return(rand.nextInt(2));
}
}

CoinTossing.java (Main class)

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class CoinTossing extends Application {
  
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("CoinTossingFXML.fxml"));
  
Scene scene = new Scene(root);
  
stage.setScene(scene);
stage.setTitle("Tossing The Coin");
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

************************************************************** SCREENSHOT *************************************************************

Coin_head.jpg

media%2Fda9%2Fda9885b3-c03b-49d0-a14f-70

Coin_tail.javamedia%2F702%2F702387f3-1520-4b67-b49e-fd

Application

Tossing The Coin 1回! Tossing the Coin Toss

Add a comment
Know the answer?
Add Answer to:
Heads or Tails Create a JavaFX application that simulates a coin being tossed. When the user clic...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • in JAVA please and please show output!! Create a JavaFX application that simulates the rolling of...

    in JAVA please and please show output!! Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a button, the application should generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. Use ImageView component to display the dice. Six images are included in the project folder for you to use. For example, the first picture below is the initial window, after clicking the...

  • here is the dice images you need to use Problem 1/5 (20 points) Create a JavaFX...

    here is the dice images you need to use Problem 1/5 (20 points) Create a JavaFX application that simulates the rolling of a pair of dice. When the user clicks a button, the application should generate two random numbers, each in the range of 1 through 6, to represent the value of the dice. Use ImageView component to display the dice. Six images are included in the project folder for you to use. For example, the first picture below is...

  • Write a program that simulates the toss of a coin. Whenever a coin is tossed the...

    Write a program that simulates the toss of a coin. Whenever a coin is tossed the result will be either a head or tail. Prompt the user as shown by entering an ‘H’ for heads or ‘T’ for tails. Use a loop for input validation of an ‘h’ or ‘t’. Make sure that both an upper case and lower case will be accepted. Have the computer generate a random number. Assign a char variable a (‘h’ ^ ’t’) head or...

  • Using c++ Write a function named coinToss that simulates tossing a coin. When you call the...

    Using c++ Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2. 1 represents heads and 2 represents tails. Exercise 1 (40 points) Write a function named coinToss that simulates tossing a coin. When you call the function it should generate and return a random number in the range 1 through 2.1 represents heads and 2 represents tails Use the function...

  • LabView Coin Toss.vi Build a VI that simulates the toss of a coin. On your block...

    LabView Coin Toss.vi Build a VI that simulates the toss of a coin. On your block dia- gram, use Random Number (0-1) to generate a random floating-point number x, in the range from 0 up to (but not including1. When run, ifx 20.5, assign the result of the coin toss to be Heads; otherwise, the result is Tails. Then, use a Select icon as shown below to decide which of two strings should be sent to a front-panel String Indicator...

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display “heads.” If the random number is 2, the function should display “tails.” Demonstrate the function in a program that asks the user how many times the coin should be tossed and then simulates the tossing of the coin that...

  • Using JavaFX create a Roll application that implements the GUI shown When the user clicks the Rol...

    Using JavaFX create a Roll application that implements the GUI shown When the user clicks the Roll button, the program must roll the dice, change the figures randomly, calculate the sum of the dice and show it in a label

  • For this question you will need to complete the methods to create a JavaFX GUI application...

    For this question you will need to complete the methods to create a JavaFX GUI application that implements two String analysis algorithms. Each algorithm is activated when its associated button is pressed. They both take their input from the text typed by the user in a TextField and they both display their output via a Text component at the bottom of the GUI, which is initialized to “Choose a string methods as indicated in the partially completed class shown after...

  • Let X represent the number of heads subtracts the number of tails obtained when a coin...

    Let X represent the number of heads subtracts the number of tails obtained when a coin is tossed 3 times, i.e., X = number of heads − number of tails. (a) Find the probability mass function of X (b) Given that X is at least 0, what is the probability that X is at least 2

  • 12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field ...

    12. Coin Toss Simulator Write a class named Coin. The Coin class should have the following field .A String named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up The Coin class should have the following methods .A no-arg constructor that randomly determines the side of the coin that is facing up (heads" or "tails") and initializes the aideUp field accordingly .A void method named toss that simulates the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT