Concept explainers
Pick Four cards
Program Plan:
- Import the required packages.
- Create a class Myclass
- Declare the necessary variables
- Using start initialize the required
- Create a new array list
- Loop that iterates to all the 52 values into the list.
- Create and initialize a random shuffle method.
- Create “hbox” and add cards randomly.
- Align the position of the cards.
- Create a new button.
- Add an action even to the button.
- Shuffle randomly to place four cards.
- Initialize the new pane.
- Add the created items into the pane.
- Create a scene based on the defined positions
- Set title for the preview
- Display the cards after refresh button is pressed.
- Define the main method.
- Initialize the call.
The below program is used to pick four cards randomly by pressing refresh button:
Explanation of Solution
Program:
//import the required headers
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.util.ArrayList;
//define the class Myclass
public class Myclass extends Application
{
@Override
//start method gets overidden in the application class
public void start(Stage primeview) {
// create a new list
ArrayList<Integer> mylist = new ArrayList<>();
// iterate for all cards
for (int iter = 1; iter <= 52; iter++)
{
// add the values to the list
mylist.add(iter);
}
// shuffle method call
java.util.Collections.shuffle(mylist);
// new Hbox declaration
HBox my_hBox = new HBox(5);
// set the alignment to be center
my_hBox.setAlignment(Pos.CENTER);
// add card 1 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(0) + ".png"));
// add card 2 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(1) + ".png"));
// add card 3 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(2) + ".png"));
// add card 4 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(3) + ".png"));
// new button gets created
Button btRefresh = new Button("Refresh");
// action event for the button gets created
btRefresh.setOnAction(e -> {
// shuffling the card
java.util.Collections.shuffle(mylist);
// clear the contents of the hbox
my_hBox.getChildren().clear();
// add card 1 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(0) + ".png"));
// add card 2 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(1) + ".png"));
// add card 3 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(2) + ".png"));
// add card 4 to the hbox
my_hBox.getChildren().add(new ImageView("exer1/image/card/" + mylist.get(3) + ".png"));
});
// new border pane is created
BorderPane new_pane = new BorderPane();
// align the position
new_pane.setCenter(my_hBox);
// align the button position
new_pane.setBottom(btRefresh);
// add the contents to the pane
BorderPane.setAlignment(btRefresh, Pos.TOP_CENTER);
// new scene gets created
Scene n_scene = new Scene(new_pane, 250, 150);
// title of the stage is set
primeview.setTitle("Pick four cards");
// the stage gets placed in the scene
primeview.setScene(n_scene);
// stage gets displayed
primeview.show();
}
// main method
public static void main(String[] args)
{
// initilaize calls
launch(args);
}
}
The below output will be displayed initially:
Screenshot of initial stage
When the refresh button is clicked the cards gets shuffled and it will be displayed as shown below:
Screenshot after rotation
Want to see more full solutions like this?
Chapter 15 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
- 8.19 (Removing a Particular Word From a Given Line of Text) Write a program that inputs a line of text and a given word. The program should use string library functions strcmp and strcpy to remove all occurrences of the given word from the input line of text. The program should also count the number of words in the given line of text before and after removing the given word using the strtok function. Solve it Without using pointersarrow_forward12.10 LAB: All permutations of names java Write a program that lists all ways people can line up for a photo (all permutations of a list of Strings). The program will read a list of one word names (until -1), and use a recursive method to create and output all possible orderings of those names, one ordering per line.arrow_forward(Enhancing Class Date) Modify the Date class of Figs. 17.13–17.14 to perform error checking on the initializer values for data members month, day and year. Also, provide a member functionnextDay to increment the day by one. Write a program that tests function nextDay in a loop thatprints the date during each iteration to illustrate that nextDay works correctly. Be sure to test thefollowing cases:a) Incrementing into the next month.b) Incrementing into the next year.arrow_forward
- 10 - In an examination, 500 students appeared. Out of these students, 38 % got A+ grade, 45 % got B+ and the remaining just passed. Assuming that no student failed, find the number of students who got A+, B+ and the number of students who just passed.(Python code)arrow_forward7.10 LAB: Acronyms An acronym is a word formed from the initial letters of words in a set phrase. Write a program whose input is a phrase and whose output is an acronym of the input. If a word begins with a lower case letter, don't include that letter in the acronym. Assume there will be at least one upper case letter in the input. Ex: If the input is: Institute of Electrical and Electronics Engineers the output is: IEEEarrow_forward1.19 Give two examples of semantic errors in a program, using the language of your choice.arrow_forward
- 6.38 LAB: Convert to binary - methods (JAVA) Write a program in java that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string. Ex: If the input is: 6 the output is: 110 The program must define and call the following two methods. Define a method named intToReverseBinary() that takes an int as a parameter and returns a string of 1's and 0's representing the integer in binary (in reverse). Define a method named stringReverse() that takes a string as a parameter and returns a string representing the input string in reverse. public static String intToReverseBinary(int integerValue)public static String stringReverse(String inputString) Code starts here: import java.util.Scanner; public…arrow_forward2. Identify and correct the errors in each of the following statements: a) if ( c !< 17 ); window.alert( "c is not less than 17" ); b) if ( c =< 17 ) window.alert( "c is equal to or less than 17" ); c) while ( c <= 5 ) { product *= c; ++c; d) if ( gender == 1 ) document.writeln( "Woman" ); else; document.writeln( "Man" ); e)var a = new Array( 20); for ( varj = 0; j = 30, ++i ){ a[ j ] = 0; }arrow_forward4.19 C++ Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Done", "done", or "d" for the line of text. Ex: If the input is: Hello there Hey done then the output is: ereht olleH yeHarrow_forward
- 5.17 LAB: Output words in a sentence that are at least 4 characters long The starter code reads in user input (assumed to be one or more sentences), and removes the punctuation. Iterate through the list of words and output the words that are more than 3 characters long. Ex: If the input is: Hello, how are you today? the output is: Hello today #provided code import stringuserInput = input() # this creates a mapping which maps each punctuation character to a null string (None)mapping = str.maketrans('', '', string.punctuation) # this removes puncutation from the stringuserInput = userInput.translate(mapping) print('User input without punctuation:', userInput) # split the string into wordswords = userInput.split()print('User input contains the following words: ')print(words)arrow_forward11.4.1: Leap year - functions. A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If the year is a century year (1700, 1800, etc.), the year must be evenly divisible by 400 Some example leap years are 1600, 1712, and 2016. Write a program that takes in a year and determines whether that year is a leap year. If the input is 1712, the output is: 1712 is a leap year. If the input is 1913, the output is: 1913 is not a leap year. Your program must define and call a function: Function OutputLeapYear(integer inputYear) returns nothing The function should output whether the input year is a leap year or not. Need program written in the coral languagearrow_forward3.6.5: Printing array elements separated by commas. Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education