Introduction to Java Programming and Data Structures  Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134700144
Author: Liang
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 15, Problem 15.1PE
Expert Solution & Answer
Check Mark
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.
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  Comprehensive Version (11th 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  Comprehensive Version (11th 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
1. Level the resources (R) for the following network. Show exactly which activity is being moved at each cycle and how many days it is being moved. Show all cycles required to utilize the free float and the back float. B H 3 3 L 2 0-0-0 A C F G K N P Q T 0 3 2 2 1 2-2-2 7R 8R 4R 6R 4R 2R 5R 4R D 1 2R 2 M 000 4R 2 4R 1 2 3 4 B5 B BE B 5 5 7 D 2003 C NO C MBSCM В H 5 2 F 7 7 8 SH2F80 5 Н Н 6 7 7L3G4+ 6H2G4 J 4 4 14 8 L K 00 36 9 10 11 12 13 14 15 P 2 Z+ N N 4 4 Z t 2334 4 Σ + M M 4 +
2. Perform resource allocation for the following project. Resource limits are 6 labors and 2 helpers. Legend: Activity Dur Resources G H 2 3 2L 1H 2L OH A 1 3L 1H + B D F J K 3 4 6 2 4 4L 2H 3L OH 4L 1H 2L 2H 4L 2H C E 2 2 I 1 2L 1H 3L 1H 5L 1H
Need Java method please. Thank you.

Chapter 15 Solutions

Introduction to Java Programming and Data Structures Comprehensive Version (11th Edition)

Chapter 15.6, Problem 15.6.1CPChapter 15.6, Problem 15.6.2CPChapter 15.6, Problem 15.6.3CPChapter 15.8, Problem 15.8.1CPChapter 15.8, Problem 15.8.2CPChapter 15.9, Problem 15.9.1CPChapter 15.9, Problem 15.9.2CPChapter 15.9, Problem 15.9.3CPChapter 15.9, Problem 15.9.4CPChapter 15.10, Problem 15.10.1CPChapter 15.11, Problem 15.11.1CPChapter 15.11, Problem 15.11.2CPChapter 15.11, Problem 15.11.3CPChapter 15.11, Problem 15.11.4CPChapter 15.12, Problem 15.12.1CPChapter 15.12, Problem 15.12.2CPChapter 15.12, Problem 15.12.3CPChapter 15.12, Problem 15.12.4CPChapter 15.12, Problem 15.12.5CPChapter 15.13, Problem 15.13.1CPChapter 15.13, Problem 15.13.2CPChapter 15.13, Problem 15.13.3CPChapter 15, Problem 15.1PEChapter 15, Problem 15.2PEChapter 15, Problem 15.3PEChapter 15, Problem 15.4PEChapter 15, Problem 15.5PEChapter 15, Problem 15.6PEChapter 15, Problem 15.7PEChapter 15, Problem 15.8PEChapter 15, Problem 15.9PEChapter 15, Problem 15.10PEChapter 15, Problem 15.11PEChapter 15, Problem 15.12PEChapter 15, Problem 15.13PEChapter 15, Problem 15.14PEChapter 15, Problem 15.15PEChapter 15, Problem 15.16PEChapter 15, Problem 15.17PEChapter 15, Problem 15.18PEChapter 15, Problem 15.19PEChapter 15, Problem 15.20PEChapter 15, Problem 15.21PEChapter 15, Problem 15.22PEChapter 15, Problem 15.23PEChapter 15, Problem 15.24PEChapter 15, Problem 15.25PEChapter 15, Problem 15.26PEChapter 15, Problem 15.27PEChapter 15, Problem 15.28PEChapter 15, Problem 15.29PEChapter 15, Problem 15.30PEChapter 15, Problem 15.31PEChapter 15, Problem 15.32PEChapter 15, Problem 15.33PEChapter 15, Problem 15.34PEChapter 15, Problem 15.35PEChapter 15, Problem 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