bartleby

Concept explainers

Question
Book Icon
Chapter 15, Problem 15.1PE
Program Plan Intro

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.

Expert Solution & Answer
Check Mark
Program Description Answer

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);

}

}

Sample Output

The below output will be displayed initially:

Screenshot of initial stage

MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version, Chapter 15, Problem 15.1PE , additional homework tip  1

When the refresh button is clicked the cards gets shuffled and it will be displayed as shown below:

Screenshot after rotation

MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version, Chapter 15, Problem 15.1PE , additional homework tip  2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

Chapter 15 Solutions

MyLab Programming with Pearson eText -- Access Card -- for Introduction to Java Programming and Data Structures, Comprehensive Version

Ch. 15.6 - Prob. 15.6.1CPCh. 15.6 - What is a functional interface? Why is a...Ch. 15.6 - Prob. 15.6.3CPCh. 15.8 - Prob. 15.8.1CPCh. 15.8 - Prob. 15.8.2CPCh. 15.9 - Prob. 15.9.1CPCh. 15.9 - Prob. 15.9.2CPCh. 15.9 - Prob. 15.9.3CPCh. 15.9 - If the following code is inserted in line 57 in...Ch. 15.10 - Prob. 15.10.1CPCh. 15.11 - Prob. 15.11.1CPCh. 15.11 - Prob. 15.11.2CPCh. 15.11 - Prob. 15.11.3CPCh. 15.11 - Prob. 15.11.4CPCh. 15.12 - How does the program make the ball appear to be...Ch. 15.12 - How does the code in Listing 15.17, BallPane.java,...Ch. 15.12 - What does the program do when the mouse is pressed...Ch. 15.12 - If line 32 in Listing 15.18, BounceBall.java, is...Ch. 15.12 - Prob. 15.12.5CPCh. 15.13 - Prob. 15.13.1CPCh. 15.13 - What would happen if map is replaced by scene in...Ch. 15.13 - Prob. 15.13.3CPCh. 15 - Prob. 15.1PECh. 15 - (Rotate a rectangle) Write a program that rotates...Ch. 15 - (Move the ball) Write a program that moves the...Ch. 15 - (Create a simple calculator) Write a program to...Ch. 15 - (Create an investment-value calculator) Write a...Ch. 15 - (Alternate two messages) Write a program to...Ch. 15 - (Change color using a mouse) Write a program that...Ch. 15 - (Display the mouse position) Write two programs,...Ch. 15 - (Draw lines using the arrow keys) Write a program...Ch. 15 - (Enter and display a string) Write a program that...Ch. 15 - (Move a circle using keys) Write a program that...Ch. 15 - Prob. 15.12PECh. 15 - (Geometry: inside a rectangle?) Write a program...Ch. 15 - Prob. 15.14PECh. 15 - Prob. 15.15PECh. 15 - (Two movable vertices and their distances) Write a...Ch. 15 - (Geometry: find the bounding rectangle) Write a...Ch. 15 - Prob. 15.18PECh. 15 - (Game: eyehand coordination) Write a program that...Ch. 15 - Prob. 15.20PECh. 15 - (Drag points) Draw a circle with three random...Ch. 15 - (Auto resize cylinder) Rewrite Programming...Ch. 15 - Prob. 15.23PECh. 15 - Prob. 15.24PECh. 15 - Prob. 15.25PECh. 15 - Prob. 15.26PECh. 15 - Prob. 15.27PECh. 15 - (Display a running fan) Write a program that...Ch. 15 - (Racing car) Write a program that simulates car...Ch. 15 - Prob. 15.30PECh. 15 - Prob. 15.31PECh. 15 - (Control a clock) Modify Listing 14.21,...Ch. 15 - (Game: bean-machine animation) Write a program...Ch. 15 - Prob. 15.34PECh. 15 - Prob. 15.35PECh. 15 - Prob. 15.36PE
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT