
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
public class memory extends JFrame implements ActionListener {
private JButton[] cards;
private ImageIcon[] icons;
private int[] iconIDs;
private JButton firstButton;
private ImageIcon firstIcon;
private int numMatches;
public memory() {
setTitle("Memory Matching Game");
setSize(800, 600);
setLayout(new BorderLayout());
JPanel boardPanel = new JPanel(new GridLayout(4, 4));
add(boardPanel, BorderLayout.CENTER);
icons = new ImageIcon[8];
for (int i = 1; i <= 8; i++) {
icons[i-1] = new ImageIcon("image" + i + ".png");
}
iconIDs = new int[16];
for (int i = 0; i < 8; i++) {
iconIDs[2*i] = i;
iconIDs[2*i+1] = i;
}
Random rand = new Random();
for (int i = 0; i < 16; i++) {
int index = rand.nextInt(16);
int temp = iconIDs[i];
iconIDs[i] = iconIDs[index];
iconIDs[index] = temp;
}
cards = new JButton[16];
for (int i = 0; i < 16; i++) {
cards[i] = new JButton();
cards[i].setPreferredSize(new Dimension(150, 150));
cards[i].addActionListener(this);
boardPanel.add(cards[i]);
}
resetGame();
}
private void resetGame() {
numMatches = 0;
firstButton = null;
firstIcon = null;
for (int i = 0; i < 16; i++) {
cards[i].setEnabled(true);
cards[i].setIcon(null);
}
for (int i = 0; i < 16; i++) {
int id = iconIDs[i];
cards[i].putClientProperty("id", id);
}
}
private void handleCardClick(JButton button) {
int id = (int) button.getClientProperty("id");
ImageIcon icon = icons[id];
if (firstIcon == null) {
// first card clicked
firstIcon = icon;
firstButton = button;
button.setIcon(icon);
button.setEnabled(false);
} else {
// second card clicked
if (icon == firstIcon) {
// matching cards
numMatches++;
button.setIcon(icon);
button.setEnabled(false);
firstButton = null;
firstIcon = null;
} else {
// non-matching cards
firstButton.setEnabled(true);
firstButton.setIcon(null);
firstButton = null;
firstIcon = null;
button.setIcon(null);
button.setEnabled(false);
}
}
if (numMatches == 8) {
// game over
int choice = JOptionPane.showConfirmDialog(this, "Congratulations, you won!\nDo you want to play again?", "Game Over", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
resetGame();
} else {
System.exit(0);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
handleCardClick(button);
}
public static void main(String[] args) {
memory game = new memory();
game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
please tell me why my code wont execute but it has no errrors and how I might fix the issue.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps with 1 images

Knowledge Booster
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
- Im trying ro read a csv file and store it into a 2d array but im getting an error when I run my java code. my csv file contains 69 lines of data Below is my code: import java.util.Scanner; import java.util.Arrays; import java.util.Random; import java.io.File; import java.io.FileNotFoundException; import java.io.FilenameFilter; public class CompLab2 { public static String [][] getEarthquakeDatabase (String Filename) { //will read the csv file and convert it to a string 2-d array String [][] Fileinfo = new String [69][22]; int counter = 0; File file = new File(Filename); try { Scanner scnr = new Scanner(file); scnr.nextLine(); //skips the label in the first row of the file while (scnr.hasNextLine()) { // this while loop will count the number of values in the usgs file counter += 1; // increases by one each time a line is read scnr.nextLine(); } while…arrow_forwardWhat are the Javadoc comments for each class? I am strugglingarrow_forwardConsider the GameOfLife class public class GameOfLife { private BooleanProperty[][] cells; public GameOfLife() { cells = new BooleanProperty[10][10]; for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { cells[x][y] = new SimpleBooleanProperty(); public void ensureAlive(int x, int y) { cells[x][y].set(true); public void ensureDead(int x, int y) { cells[x][y]•set(false); public boolean isAlive(int x, int y) { return cells[x][y]•get(); } a. Identify one code smell in this program. b. Identify and explain the most pertinent (problematic) design smell in this program.arrow_forward
- how to do this in java that functions with the main class in the first image and can be compared with the container class in the second imagearrow_forwardNeed help with menu loop pleasearrow_forwardin this android app package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.ListView;public class PlayerActivity2 extends AppCompatActivity {ListView simpleList;String SerialNo[] = {"1", "2", "3", "4", "5", "6","7","8","9","10"};int flags[] = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5, R.drawable.image6, R.drawable.image7, R.drawable.image8, R.drawable.image9, R.drawable.image10};String Names[] = {"mmm", "nnn", "aaa.", "bbb", "ccc", "ddd","eee jk"," ijk","Virgil jk","gil jklk"};String Score[] = {"1", "2","3", "5", "4", "3","5","5","5","5"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity2);simpleList = (ListView)findViewById(R.id.simpleListView);//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_listview, R.id.textView,…arrow_forward
- Java language Use arrays in creating your class. Stack Interface (LIFO) void push(Object) Object pop() String toString() boolean isEmpty() boolean equals(Object) getIndexOf get remove private static void arrayListTests() { System.out.println("ArrayList Tests"); // todo: make more tests here ArrayList a = new ArrayList(); System.out.println("Check empty array isEmpty:" + a.isEmpty()); a.insert('B', 0); a.insert('a', 0); a.insert('t', 1); System.out.println("Check non-empty array isEmpty:" + a.isEmpty()); System.out.println(a.toString()); while (a.isEmpty() == false) { System.out.println(a.remove(0)); } // Fill over initial capacity and check that it grows for (int i = 0; i < 110; i++) { a.append(new Integer(i)); } System.out.println("Size of array after 110 adds: "+ a.size()); System.out.println("Value of last element: "+ a.get(a.size()-1)); System.out.println("Insert past end of list"); a.insert('z', 200); System.out.println("Insert negative index"); a.insert('z', -3);…arrow_forwardimport java.util.ArrayList;import java.util.Scanner;public class CIS231A4JLeh {public static void main(String[] args) {Scanner scnr = new Scanner(System.in);ArrayList<Integer> integers = new ArrayList<>();String input = scnr.nextLine();while (input.isEmpty()) {String[] parts = input.split("\\s+");for (String part : parts) {try {integers.add(Integer.parseInt(part));} catch (NumberFormatException ignored) {}}input = scnr.nextLine();}System.out.println("Jakob");System.out.println("Number of integers input: " + integers.size());System.out.println("All values input, in ascending order:");printIntegers(integers);System.out.println("Lowest value input: " + integers.get(0));System.out.println("Highest value input: " + integers.get(integers.size() - 1));System.out.printf("Average of all values input: %.2f%n", calculateAverage(integers));System.out.println("Mode of the data set and its frequency: " + calculateMode(integers));}private static void printIntegers(ArrayList<Integer>…arrow_forwardJavaTimer.java: import java.util.Arrays;import java.util.Random; public class JavaTimer { // Please expand method main() to meet the requirements.// You have the following sorting methods available:// insertionSort(int[] a);// selectionSort(int[] a);// mergeSort(int[] a);// quickSort(int[] a);// The array will be in sorted order after the routines are called!// Be sure to re-randomize the array after each sort.public static void main(String[] args) {// Create and initialize arraysint[] a = {1, 3, 5}, b, c, d;// Check the time to sort array along startTime = System.nanoTime();quickSort(a);long endTime = System.nanoTime();long duration = (endTime - startTime) / 1000l;// Output resultsSystem.out.println("Working on an array of length " + a.length + ".");System.out.println("Quick sort: " + duration + "us.");}// Thanks to https://www.javatpoint.com/insertion-sort-in-javapublic static void insertionSort(int array[]) {int n = array.length;for (int j = 1; j < n; j++) {int key = array[j];int…arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education