Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
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

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition), 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

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition), 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
4.7: Time Machine Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time (in hours, minutes, and a Boolean indicating AM or PM) and a future time (in hours, minutes, and a Boolean indicating AM or PM) and calculate the difference in minutes between the start and future time. A time is specified in your program with three variables: int hours, minutes; bool isAM; for example, to represent 11:50 PM, you would store: hours = 11,minutes = 50,isAM = false This means that you need six variables to store a start and future time. Write a program that allows the user to enter a start time and a future time. Include a function named computeDifference that takes the six variables as parameters that represent the start time and future time. Your function should return, as an int, the time difference in minutes. for example,…
4.7: Time Machine Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time (in hours, minutes, and a Boolean indicating AM or PM) and a future time (in hours, minutes, and a Boolean indicating AM or PM) and calculate the difference in minutes between the start and future time.A time is specified in your program with three variables:int hours, minutes;bool isAM;for example, to represent 11:50 PM, you would store:hours = 11,minutes = 50,isAM = false   This means that you need six variables to store a start and future time. Write a program that allows the user to enter a start time and a future time. Include a function named computeDifference that takes the six variables as parameters that represent the start time and future time. Your function should return, as an int, the time difference in minutes. for example,…
4.7: Time Machine Your time machine is capable of going forward in time up to 24 hours. The machine is configured to jump ahead in minutes. To enter the proper number of minutes into your machine, you would like a program that can take a start time (in hours, minutes, and a Boolean indicating AM or PM) and a future time (in hours, minutes, and a Boolean indicating AM or PM) and calculate the difference in minutes between the start and future time. A time is specified in your program with three variables: int hours, minutes; bool isAM; // You can also use a char, i.e. A or P Write a program that allows the user to enter a start time and a future time. Include a function named computeDifference that takes the six variables as parameters that represent the start time and future time. Your function should return, as an int, the time difference in minutes. for example, given a start time of 11:59 AM and a future time of 12:01 PM, your program should compute 2 minutes as the time difference.…

Chapter 15 Solutions

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education