Question
1) Create a Class called Coin- a. Declare Class Instance Data. b. Constructor Method i. Set value to a random integer between 0 and 1
1) Create a Class called Coin-
a. Declare Class Instance Data.
b. Constructor Method i. Set value to a random integer between 0 and 1 ii. If value = 1, set sideUp equal to Heads, else set sideUp equal to Tails
c. flipCoin Method i. Set value to a random integer between 0 and 1 ii. If value = 1, set sideUp equal to Heads, else set sideUp equal to Tails
d. getFace method i. return the value for sideUp
e. toString method.
import java.util.Random;
public class Coin { private int value; private String sideUp; private Random rnd;
public Coin(){ rnd = new Random(); flipCoin(); } public void flipCoin(){ value = rnd.nextInt(2); if (value == 1) { sideUp = "Heads" ; } else { sideUp = "Tails" ; } } public String getFace(){ return sideUp; } public String toString(){ return "Value: " + value + ",sideUp: " + sideUp; } }
I've already completed part 1, I just need help with creating a flip the coin game using javafx in the main code.
2) main code a. At minimum create class level int variable to hold number of wins i. You decide with other nodes need to be defined as class level. b. Create an object from GridPane class (See last page for layout example) c. Create an object from the Text class and initialize with your title of game and INCLUDE YOUR NAME d. Create an obiect from Coin class. e. Create an obiect from Circle Class f. Create an object from the Text class and initial with the word "Heads" g. Create an object from the Group class and add the Circle object and the Text object to it i. NOTE: this is simplified from Homework 2. We don't need two groups. Just use the setText method to change the word to Heads or Tails h. Create an obiect from the Text class and initialize the text with instructions the user to enter H or I i. Create an object from the TextField class to hold user guess j. Create an object from the Button class and label it k. Associate the Button object with the event handler code you will write. 1. Create an object from the Text class to hold the label to display if the won or lost m. Create an object from the Text class to hold the label to display the number of wins. You should initialize the text to say zero wins Add everything to the GridPane object. Don't worry about centering everything. We are not n. grading on "prettiness." o. Use the String returned from the getFace) method to update the words on the Circle object. p. Add the Group to the Scene q. Add the Scene to the primaryStage r. Display the primaryStage 3) Your Event Handler Code for the Button object a. Call method to flip the coin b. Use the String returned from the getFace) method to update the words on the Circle object. c. Input the user guess of H or T d. Use decision code to i. If they entered T or H 1. If guess matches the getFace) string then Increment win counter Update the label to tell them they won a. b. 2. Else 3. "end if" 1. Tell them invalid input a. Update label to tell them they lost ii. Else iii. "end if" 2) main code a. At minimum create class level int variable to hold number of wins i. You decide with other nodes need to be defined as class level. b. Create an object from GridPane class (See last page for layout example) c. Create an object from the Text class and initialize with your title of game and INCLUDE YOUR NAME d. Create an obiect from Coin class. e. Create an obiect from Circle Class f. Create an object from the Text class and initial with the word "Heads" g. Create an object from the Group class and add the Circle object and the Text object to it i. NOTE: this is simplified from Homework 2. We don't need two groups. Just use the setText method to change the word to Heads or Tails h. Create an obiect from the Text class and initialize the text with instructions the user to enter H or I i. Create an object from the TextField class to hold user guess j. Create an object from the Button class and label it k. Associate the Button object with the event handler code you will write. 1. Create an object from the Text class to hold the label to display if the won or lost m. Create an object from the Text class to hold the label to display the number of wins. You should initialize the text to say zero wins Add everything to the GridPane object. Don't worry about centering everything. We are not n. grading on "prettiness." o. Use the String returned from the getFace) method to update the words on the Circle object. p. Add the Group to the Scene q. Add the Scene to the primaryStage r. Display the primaryStage 3) Your Event Handler Code for the Button object a. Call method to flip the coin b. Use the String returned from the getFace) method to update the words on the Circle object. c. Input the user guess of H or T d. Use decision code to i. If they entered T or H 1. If guess matches the getFace) string then Increment win counter Update the label to tell them they won a. b. 2. Else 3. "end if" 1. Tell them invalid input a. Update label to tell them they lost ii. Else iii. "end ifStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started