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)
- lab assignment 1.13 LAB: Introduction to Cyptography (classes/constructors) Cryptography is the practice of encryption. Information Security uses cryptography techniques to encrypt and decrypt data. A simple encryption method might take plaintext and mix up the letters using some predetermined pattern and then use that pattern to decrypt the data for reading. Ciphers are the algorithms used to put the data into its secret pattern and then systematically decrypt it for reading. This script is going to use a famous simple cipher called the Caesar Cipher. It is a substitution cipher where each letter in the text is 'shifted' in a certain number of places. It uses the alphabet as the primary pattern and then based on the shift number, it would shift all the letters and replace the alphabet with our pattern. For example, if our shift number was 3, then A would be replaced with D, if we performed a right shift. As an example: Text = "THE CAT IS VISIBLE AT MIDNIGHT" Ciphertext = "WKH FDW LV…arrow_forward4.10 LAB: All permutations of names 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, then use a recursive function to create and output all possible orderings of those names separated by a comma, one ordering per line. When the input is: Julia Lucas Mia then the output is (must match the below ordering): Julia, Lucas, Mia Julia, Mia, Lucas Lucas, Julia, Mia Lucas, Mia, Julia Mia, Julia, Lucas Mia, Lucas, Julia def print_all_permutations(permList, nameList): # TODO: Implement method to create and output all permutations of the list of names. if __name__ == "__main__": nameList = input().split(' ') permList = [] print_all_permutations(permList, nameList)arrow_forwardJava scriptarrow_forward
- Please read instructions carefully:arrow_forwardSpring 2020-2021 Question2 (50 pts) Write a program that reads from the user a character ('q' or 'c'). • If the character is 'c', the program reads from the user the ra dius r of a circle and prints its area. If the user enters 'q', the program asks the user to enter the length and width of a quadrilateral. We assume the quadrilateral is either a square or rectangle. You should print if the quadrilateral is square or rectangle. • Otherwise, it prints "Wrong character" PS: Use the following formulas : area of circle=3.14*r? Sample Runl: Enter a character (q or c): q Enter the Length and Width: 80 150 It is a rectangle Sample Run2: Enter a character (q or c): q Enter the Length and Width: 80 80 It is a square Q + Page 3 / 3arrow_forward6.19 LAB: Leap year - functions A common year in the modern Gregorian Calendar consists of 365 days. In reality, 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, therefore, both 1700 and 1800 are not leap years Some example leap years are 1600, 1712, and 2016. Write a program that takes in a year and determines the number of days in February for that year Ex. If the input is 1712 the output is 1712 has 29 days in February. Ex If the input is 1913 the output is 1913 has 28 days in February. Your program must define and call the following function. The function should return the number of days in February for the input year. def days in feb(user_year)arrow_forward
- Q29) Consider you have a list of 20 exam scores. Write a computer program that calculates the average of the top 8 scores. The program prints the message "The average top eight scores is:" and then prints the average number. Exam scores: 73, 91, 37, 81, 63, 66, 50, 90, 75, 43, 88, 80, 79, 69, 26, 82, 89, 99, 71, 59arrow_forwardPythonarrow_forward**CODE IS IN JAVA** Modify the BarChart program from Chapter 6 Exercise 13 to accept the number of points scored by each player in a season. The bar chart displays one asterisk for each 10 points scored by a player. For example, if a player has scored 48 points, then display 4 asterisks. An example of the program is shown below: Enter points earned for the season by Art >> 10 by Bob >> 20 by Cal >> 30 by Dan >> 40 by Eli >> 50 Points for Season (each asterisk represents 10 points) Art * Bob ** Cal *** Dan **** Eli ***** The code to modify: Scanner input = new Scanner(System.in); int artPoints; int bobPoints; int calPoints; int danPoints; int eliPoints; final int AMT_EACH_ASTERISK = 10; System.out.println("Enter points earned for the season"); System.out.print(" by Art >> "); artPoints = input.nextInt(); System.out.print(" by Bob >> "); bobPoints =…arrow_forward
- Java Programming exercisearrow_forwardFast answerarrow_forward6.9 LAB: All permutations of names 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. When the input is: Julia Lucas Mia -1 then the output is (must match the below ordering): Julia Lucas Mia Julia Mia Lucas Lucas Julia Mia Lucas Mia Julia Mia Julia Lucas Mia Lucas Julia import java.util.Scanner;import java.util.ArrayList; public class PhotoLineups { // TODO: Write method to create and output all permutations of the list of names.public static void allPermutations(ArrayList<String> permList, ArrayList<String> nameList) {} public static void main(String[] args) {Scanner scnr = new Scanner(System.in);ArrayList<String> nameList = new ArrayList<String>();ArrayList<String> permList = new ArrayList<String>();String name;//…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