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: 9780134670942
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, 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
3.27 LAB: Login name Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first six letters of the first name, followed by the first letter of the last name, an underscore (_), and then the last digit of the number (use the % operator). If the first name has less than six letters, then use all letters of the first name. Ex: If the input is: Michael Jordan 1991 the output is: Your login name: MichaeJ_1 Ex: If the input is: Nicole Smith 2024 the output is: Your login name: NicoleS_4
11.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 (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 #include #include #include main.cpp: using namespace std; // TODO: Write method to create and output all permutations of the list of names. void AllPermutations(const vector &permList, const vector &nameList) { } int main(int argc, char* argv[]) { vector nameList; vector permList; string name; // TODO: Read in a list of names; stop when -1 is read. Then call recursive method. return 0; }
11.10 LAB: All permutations of names C++   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   Please help with //TODO   #include <vector>#include <string>#include <iostream> using namespace std; // TODO: Write method to create and output all permutations of the list of names.void AllPermutations(const vector<string> &permList, const vector<string> &nameList) { } int main(int argc, char* argv[]) {vector<string> nameList;vector<string> permList;string name; // TODO: Read in a list of names; stop when -1 is read. Then call…

Chapter 15 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th 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