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)
- c++ onlyarrow_forwardKey stroke enacted moving of a ball 1. Start (a) Write a Java program that uses the Up, ↑, Down ↓, Right → (and possibly Left) arrows of the keyboard (or keypad) to move a ball. (note the Up arrow is called “Up" with ASCII code 38, Down arrow is called "Down" with ASCII code 40, Right arrow is called "Right" with ASCII code. You can also use the 4 keys, commonly used in ODS game programming. Start Pond Figure 1. Initially Pond End End Start Pond Start Figure 2. After 3 moves up using up arrow End Pond End Figure 3. 3 moves later with right arrows Figure 4. Finally one at the lower right corner. (b) Using Java class JFrame to draw a grid of 5 horizontal lines, 5 vertical lines, a blue pond in the middle occupying 5 squares with a text Pond in the middle, a ball at the lower left corner, and texts called Start and End. This is shown exactly as in Figure 1 here. Change the color of ball from green to another color.arrow_forwardPlease read instructions carefully:arrow_forward
- Spring 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_forwardC CODE 3.25 Lab: Grade Calculator Write a program that calculates your grade in this class based on weights for each category and determines the letter grade. The categories and weights have been established as: Reading Assignments (10%) Lab Assignments (20%) Lab Practicums (45%) Quizzes (5%) Midterm Exam (5%) Final Exam (15%) After you calculate the course grade, use conditionals with the following ranges to determine the letter grade: A (93 - 100) A- (90 - 92.9) B+ (87 - 89.9) B (83 - 86.9) B- (80 - 82.9) C+ (77 - 79.9) C (73 - 76.9) If the grade is below 73%, display the text "You did not pass this semester." The code should interact as follows: GRADE ENTRY Enter your reading total (%): 82 Enter your lab total (%): 73 Enter your lab practicum total (%): 75 Enter your quizzes total (%): 92 Enter your midterm total (%): 86 Enter your final exam total (%): 88 RESULTS Your final course grade is: 78.65% You received a C+ for the semester. NOTE: In the grade entry section, the…arrow_forward
- Pythonarrow_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_forwardJava Programming exercisearrow_forward
- GRADED ACTIVITY #6 (M8.1): REPETITION CONTROL STRUCTURE (WHILE, DO-WHILE) Instruction: A mathematician named Ulam proposed generating a sequence of numbers from any positive integer N greater than 1 using the following procedure: If N is 1, stop. If N is even, replace it with N/2. If N is odd, replace it with 3 * N + 1. Continue with this process until N reaches 1. Here are some examples of the Ulam sequence for the first few integers. 2, 1 3, 10, 5, 16, 8, 4, 2, 1 4, 2, 1 5, 16, 8, 4, 2, 1 6, 3, 10, 5, 16, 8, 4, 2, 1 Write a Java program using while/do-while that accepts as input an integer value N (assume N> 1) and prints out the Ulam sequence that begins with the input value N. Sample Input/Output: Depicted below are sample outputs when the program is executed (the items in bold characters are input from the user, while the items in bold italic are calculated and printed by the program): Input N: 14 Ulam Sequence: Input N: 5 Ulam Sequence: 5, 16, 8, 4, 2, 1 14, 7, 22, 11, 34, 17,…arrow_forward3.8 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve the above, first read the integers into an array. Then output the array in reverse.arrow_forward7. Write a function that evaluates the area of a pentagon. Use "math.pi", for pi, and "math.sqrt" for square root. Write the solution on the space provided below. You do not need to run the code. 311 3.2 (Geometry: area of a pentagon) Write a program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pen- tagon, as shown in the following figure. The formula for computing the area of a pentagon is Area 3√3 2 -s², where s is TT the length of a side. The side can be computed using the formula s = 2r sin 5' where r is the length from the center of a pentagon to a vertex. Here is a sample run: Enter the length from the center to a vertex: 5.5 Enter The area of the pentagon is 108.61arrow_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