Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I\'m working on a Java programming assignment and I\'m a bit stuck. Any and

ID: 3833030 • Letter: H

Question

Hi,

I'm working on a Java programming assignment and I'm a bit stuck. Any and all help would be very appreciated! Here is the question:

(Pick four cards) Write a program that lets the user click the Refresh button to
display four cards from a deck of 52 cards, as shown in Figure 15.24a. (See the
hint in Programming Exercise 14.3 on how to obtain four random cards.)

So far, I'm able to print out four random cards, once, but not able to utilize my button to do anything. I've put a copy of the current state of my code below:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;

public class C15E1 extends Application implements EventHandler<ActionEvent>{

   @Override
   public void start(Stage primaryStage) throws Exception {
       // TODO Auto-generated method stub
      
       Random rand= new Random();
      
       int[] Array = {rand.nextInt(53), rand.nextInt(53), rand.nextInt(53), rand.nextInt(53)};
       GridPane pane = new GridPane();
      
       Button button = new Button();
       button.setText("Reset");
       button.setOnAction(this);
      
       button.setOnAction(e -> RandCards(Array));
       Image img1 = new Image("file:src/images/" + Array[0] + ".png");
       Image img2 = new Image("file:src/images/" + Array[1] + ".png");
       Image img3 = new Image("file:src/images/" + Array[2] + ".png");
       Image img4 = new Image("file:src/images/" + Array[3] + ".png");
       ImageView imgpic = new ImageView();
       ImageView imgpic2 = new ImageView();
       ImageView imgpic3 = new ImageView();
       ImageView imgpic4 = new ImageView();
       imgpic.setImage(img1);
       imgpic2.setImage(img2);
       imgpic3.setImage(img3);
       imgpic4.setImage(img4);
      
       pane.getChildren().addAll(button, imgpic, imgpic2, imgpic3, imgpic4);
       GridPane.setRowIndex(imgpic, 0);
       GridPane.setColumnIndex(imgpic, 0);
       GridPane.setRowIndex(imgpic2, 0);
       GridPane.setColumnIndex(imgpic2, 1);
       GridPane.setRowIndex(imgpic3, 0);
       GridPane.setColumnIndex(imgpic3, 2);
       GridPane.setRowIndex(imgpic4, 0);
       GridPane.setColumnIndex(imgpic4, 3);
      
       GridPane.setRowIndex(button, 2);
       GridPane.setColumnIndex(button, 2);
      

       Scene scene = new Scene(pane);
       primaryStage.setTitle("Chapter 15: Exercise 1");
       primaryStage.setScene(scene);
       primaryStage.show();
      
       System.out.println(Arrays.toString(Array));
      
   }
  
   public void RandCards (int[] array) {
       Random rand = new Random();
       int [] randNums = {rand.nextInt(53), rand.nextInt(53), rand.nextInt(53), rand.nextInt(53)};
       }
  
   public static void main(String[] args) {
       launch(args);
   }
}

I think I'm close, I just can't find a way to get the button to refresh the array of four random variables that I have. I need to find a way to put an event handler prompted bythe button push to randomize four numbers and put it into the main array, which I'm then calling to print the cards randomly.

Thanks for any and all help!

Explanation / Answer

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
public class C15E1 extends Application implements EventHandler<ActionEvent>{
  
   ImageView imgpic ;
   ImageView imgpic2;
   ImageView imgpic3;
   ImageView imgpic4;
   Image img[];
   int SIZE = 52;
   @Override
public void start(Stage primaryStage) throws Exception {
      
Random rand= new Random();
  
int[] Array = {rand.nextInt(SIZE), rand.nextInt(SIZE), rand.nextInt(SIZE), rand.nextInt(SIZE)};
GridPane pane = new GridPane();
img = new Image[SIZE];
       for(int i=0; i<SIZE; i++){//Creating images
           img[i] = new Image("file:src/images/" + (i+1) + ".png");
       }
      
Button button = new Button();
button.setText("Reset");
button.setOnAction(this);
  
button.setOnAction(e -> RandCards());

       imgpic = new ImageView();
       imgpic2 = new ImageView();
       imgpic3 = new ImageView();
       imgpic4 = new ImageView();
imgpic.setImage(img[Array[0]]);
imgpic2.setImage(img[Array[1]]);
imgpic3.setImage(img[Array[2]]);
imgpic4.setImage(img[Array[3]]);
  
pane.getChildren().addAll(button, imgpic, imgpic2, imgpic3, imgpic4);
GridPane.setRowIndex(imgpic, 0);
GridPane.setColumnIndex(imgpic, 0);
GridPane.setRowIndex(imgpic2, 0);
GridPane.setColumnIndex(imgpic2, 1);
GridPane.setRowIndex(imgpic3, 0);
GridPane.setColumnIndex(imgpic3, 2);
GridPane.setRowIndex(imgpic4, 0);
GridPane.setColumnIndex(imgpic4, 3);
  
GridPane.setRowIndex(button, 2);
GridPane.setColumnIndex(button, 2);
  
Scene scene = new Scene(pane);
primaryStage.setTitle("Chapter 15: Exercise 1");
primaryStage.setScene(scene);
primaryStage.show();
  
System.out.println(Arrays.toString(Array));
  
}
public void handle(ActionEvent event){
      
   }
public void RandCards () {
Random rand = new Random();
int [] Array = {rand.nextInt(SIZE), rand.nextInt(SIZE), rand.nextInt(SIZE), rand.nextInt(SIZE)};
System.out.println(Arrays.toString(Array));
       imgpic.setImage(img[Array[0]]);
imgpic2.setImage(img[Array[1]]);
imgpic3.setImage(img[Array[2]]);
imgpic4.setImage(img[Array[3]]);
}
public static void main(String[] args) {
launch(args);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote