(ArrayIndexOutOfBoundsException) Modifythe program from the code bel.docx
1. (ArrayIndexOutOfBoundsException) Modifythe program from
the code below to prompt the user for four numbers between 0
and 53 inclusive, then display those cards. If the user enters a
number that is not in the correct range, the program catches the
IndexOutOfBoundsException and displays the message Out of
Bounds. The cards are on the given link:
https://drive.google.com/file/d/0BwnXHLM8uUJidG5GUU13bF
VSaW8/view?usp=sharing
package display4cards;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Display4Cards extends JFrame implements
ActionListener {
Random rand = new Random();
JButton jb1;
JButton jb2;
JButton jb3;
JButton jb4;
public Display4Cards()
{
2. setTitle("Display four cards");
//Establishing Grid Layout
setLayout(new GridLayout(1,4,9,9));
display();
}
public void display() {
JLabel I[];
I = new JLabel[3];
ImageIcon img = new
ImageIcon("C:UsersPabloEscobarDocumentsNetBe
ansProjectsDisplay4Cardscardb1fv.png");
jb1 = new JButton(img);
jb1.addActionListener(this);
jb2 = new JButton(img);
jb2.addActionListener(this);
jb3 = new JButton(img);
jb3.addActionListener(this);
jb4 = new JButton(img);
jb4.addActionListener(this);
add(jb1);
add(jb2);
add(jb3);
add(jb4);
setVisible(true);
setSize(400,200);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
File file = new
File("C:UsersPabloEscobarDocumentsNetBeansProj
ectsDisplay4Cardscard");
File names[] = file.listFiles();
int index = rand.nextInt(names.length);
System.out.println(index);
3. System.out.println(names[index]);
if (e.getSource() == jb1) {
ImageIcon img2 = new
ImageIcon(names[index].getAbsolutePath());
jb1.setIcon(img2);
}
else if(e.getSource() == jb2)
{
ImageIcon img2 = new
ImageIcon(names[index].getAbsolutePath());
jb2.setIcon(img2);
}
else if(e.getSource() == jb3)
{
ImageIcon img2 = new
ImageIcon(names[index].getAbsolutePath());
jb3.setIcon(img2);
}
else if(e.getSource() == jb4)
{
ImageIcon img2 = new
ImageIcon(names[index].getAbsolutePath());
jb4.setIcon(img2);
}
repaint();
}
public static void main(String[] args) {
new Display4Cards();
}
}
4. Solution
Changed to be done: I have observed that currently the variable
named names[] does not contain sorted list of card images
paths. To have sorted list we have to change the names of card
as following. Change the card name 1 to 01, card name 2 to 02,
card name 3 to 03, card name 4 to 04 and so on till card 9 to 09.
As we rename the card names from 1-9 to 01-09, then we will
get the names[] array have sorted names. Then we can use the
following modified code .
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.util.Arrays;
import java.util.Scanner;
5. public class Display4Cards extends JFrame implements
ActionListener {
Random rand = new Random();
JButton jb1;
JButton jb2;
JButton jb3;
JButton jb4;
int indexValue[]=new int[4];
int val = 0;
public Display4Cards() throws IndexOutOfBoundsException
{
Scanner s=new Scanner(System.in);
System.out.println("Enter 4 numbers between 0-53");
for(int j=0; j<4; j++)
{//for reading array
val = s.nextInt();
if(val < 1 || val > 52 )
{
throw new IndexOutOfBoundsException( "Out of
Bounds Error");
}
6. indexValue[j] = val;
}
setTitle("Display four cards");
//Establishing Grid Layout
setLayout(new GridLayout(1,4,9,9));
display();
}
public void display() {
JLabel I[];
I = new JLabel[3];
ImageIcon img = new
ImageIcon("C:UsersPabloEscobarDocumentsNetBe
ansProjectsDisplay4Cardscardb1fv.png");
jb1 = new JButton(img);
jb1.addActionListener(this);
jb2 = new JButton(img);
jb2.addActionListener(this);
jb3 = new JButton(img);
jb3.addActionListener(this);
jb4 = new JButton(img);
jb4.addActionListener(this);
add(jb1);
add(jb2);
7. add(jb3);
add(jb4);
setVisible(true);
setSize(400,200);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
File file = new
File("C:UsersPabloEscobarDocumentsNetBeansProj
ectsDisplay4Cardscard");
File names[] = file.listFiles();
Arrays.sort(names);
int index = rand.nextInt(names.length);
System.out.println(index);
System.out.println(names[index]);
if (e.getSource() == jb1) {
ImageIcon img2 = new
ImageIcon(names[indexValue[0]-1].getAbsolutePath());
jb1.setIcon(img2);
}
else if(e.getSource() == jb2)
{
ImageIcon img2 = new ImageIcon(names[indexValue[1] -