So I need to create a compound word game, and I don't quite understand how I can split up the
compound words that are selected by the user on a frame into individual buttons where each
button represents each letter, and the user can select which one starts the second word in a
compound word otherwise gives a prompt indicating their wrong and should try again without
having to make a class for each compound word I have. Below I have provided the array of
words, and the word class needed after that will be the startLetter class, where the compound
word is supposed to split into individual buttons. My whole goal is to fit it into one startLetter
class without needing a class for each of the compound words, so in this case, five different
classes I feel aren't necessary.
The compoundWordProject 4 Class/ Main
package compoundWordsProject4;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
//Creates class compoundWords4, extends JFrame and implemetns an ActionListener
public class compoundWords4 extends JFrame implements ActionListener {
//Creates instances for the Words array
static Word Words1 = new Word("SAND");
static Word Words2 = new Word("SANDWHICH", 5);
static Word Words3 = new Word("BED");
static Word Words4 = new Word("BEDROOM", 4);
static Word Words5 = new Word("CHESSECAKE", 7);
static Word Words6 = new Word("CAKE");
static Word Words7 = new Word("Apple");
static Word Words8 = new Word("FireHouse", 5);
static Word Words9 = new Word("Base");
static Word Words10 = new Word("BaseBall", 5);
static Word Words11 = new Word("Ball");
static Word Words12 = new Word("Bat");
// Creates an array for the word instances
static Word[] Words = { Words1, Words2, Words3, Words4, Words5, Words6, Words7,
Words8, Words9, Words10, Words11, Words12};
//Creates an empty array of buttons
JButton[] buttons;
//Creates a private variable for click
private JLabel click;
//Creates the constructor for compoundWords
public compoundWords4() {
setLayout(new FlowLayout()); //Sets the layout
//Creates a label
click = new JLabel("Click on a Compound Word:");
click.setFont(new Font("Arial", Font.BOLD, 25));
add(click);
//Adds words from the string array into Jbuttons
buttons = new JButton[Words.length];
//Creates all the buttons from array and adds actionListner
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(Words[i].getWord());
buttons[i].addActionListener(this);
add(buttons[i]);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Closes the program when the window
is closed
}
public void actionPerformed(ActionEvent e) {
//Creates action performed when buttons are clicked
if(e.getSource() == buttons[0]) {
JOptionPane.showMessageDialog(null, "This is not a compound Word", "Incorrect",
JOpti.
So I need to create a compound word game, and I dont quite understa.pdf
1. So I need to create a compound word game, and I don't quite understand how I can split up the
compound words that are selected by the user on a frame into individual buttons where each
button represents each letter, and the user can select which one starts the second word in a
compound word otherwise gives a prompt indicating their wrong and should try again without
having to make a class for each compound word I have. Below I have provided the array of
words, and the word class needed after that will be the startLetter class, where the compound
word is supposed to split into individual buttons. My whole goal is to fit it into one startLetter
class without needing a class for each of the compound words, so in this case, five different
classes I feel aren't necessary.
The compoundWordProject 4 Class/ Main
package compoundWordsProject4;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
//Creates class compoundWords4, extends JFrame and implemetns an ActionListener
public class compoundWords4 extends JFrame implements ActionListener {
//Creates instances for the Words array
static Word Words1 = new Word("SAND");
static Word Words2 = new Word("SANDWHICH", 5);
static Word Words3 = new Word("BED");
static Word Words4 = new Word("BEDROOM", 4);
static Word Words5 = new Word("CHESSECAKE", 7);
static Word Words6 = new Word("CAKE");
2. static Word Words7 = new Word("Apple");
static Word Words8 = new Word("FireHouse", 5);
static Word Words9 = new Word("Base");
static Word Words10 = new Word("BaseBall", 5);
static Word Words11 = new Word("Ball");
static Word Words12 = new Word("Bat");
// Creates an array for the word instances
static Word[] Words = { Words1, Words2, Words3, Words4, Words5, Words6, Words7,
Words8, Words9, Words10, Words11, Words12};
//Creates an empty array of buttons
JButton[] buttons;
//Creates a private variable for click
private JLabel click;
//Creates the constructor for compoundWords
public compoundWords4() {
setLayout(new FlowLayout()); //Sets the layout
//Creates a label
click = new JLabel("Click on a Compound Word:");
click.setFont(new Font("Arial", Font.BOLD, 25));
add(click);
//Adds words from the string array into Jbuttons
buttons = new JButton[Words.length];
//Creates all the buttons from array and adds actionListner
for(int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(Words[i].getWord());
buttons[i].addActionListener(this);
add(buttons[i]);
}
3. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Closes the program when the window
is closed
}
public void actionPerformed(ActionEvent e) {
//Creates action performed when buttons are clicked
if(e.getSource() == buttons[0]) {
JOptionPane.showMessageDialog(null, "This is not a compound Word", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
}else if(e.getSource() == buttons[1]) {
//Creates a window for startLetter if SandWhich is clicked
startLetter frame2 = new startLetter();
frame2.setVisible(true);
frame2.setSize(400,400);
frame2.setLocation(400,200);
frame2.setTitle("Starting Letter");
}
public static void main (String args[]){
//Creates a window for difficulty
difficulty frame7 = new difficulty();
frame7.setSize(400,400);
frame7.setTitle("Compound Word Game");
frame7.setVisible(true);
}
}
The Word class
package compoundWordsProject4;
//Creates the public class called "word" creates attributes called "word, index and isCompound"
public class Word{
private String word;
private int index;
private boolean isCompound;
4. //Creates attributes called "difficulty" and "topics"
private int difficulty;
private String[] topics;
//Creates the constructor for the compound words
public Word(String w, int i){
this.word = w;
this.index = i;
isCompound = true;
}
//Create the constructor for "word"
public Word(String w){
this.word = w;
isCompound = false;
}
//Creates a get method for difficulty
public int getDifficulty() {
return difficulty;
}
//Creates a set method for difficulty
public void setDifficulty(int d) {
this.difficulty = d;
}
//Creates a get method for topics
public String[] getTopics() {
return topics;
}
//Creates a set method for topics
public void setTopics(String[] t) {
this.topics = t;
}
//Creates a get method for "word"
public String getWord(){
return word;
}
//Creates a set method for "word"
public void setWord(String w){
5. this.word = w;
}
//Creates a get method for "index"
public int getIndex(){
return index;
}
//Creates a set method for "index"
public void setIndex(int i){
this.index = i;
}
//Creates a boolean method for "isCompound"
public boolean isCompound(){
return isCompound;
}
//Creates a set boolean method for "isCompound"
public void setCompound(boolean isCompound){
this.isCompound = isCompound;
}
}
The startLetter class
package compoundWordsProject4;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class startLetter extends JFrame implements ActionListener{
//Creates private variables
private JButton S;
private JButton A;
6. private JButton N;
private JButton D;
private JButton W;
private JButton H;
private JButton I;
private JButton C;
private JButton H2;
private JLabel click2;
//Creates a constructor for startLetter
public startLetter() {
setLayout(new FlowLayout()); //Sets layout
//Creates a label
click2 = new JLabel("Click on the letter where the second compound word begins: ");
click2.setFont(new Font("Arial", Font.BOLD, 13));
add(click2);
//Creates a button for letter S
S = new JButton("S");
S.addActionListener(this);
add(S);
//Creates a button for letter A
A = new JButton("A");
A.addActionListener(this);
add(A);
//Creates a button for letter N
N = new JButton("N");
N.addActionListener(this);
add(N);
//Creates a button for letter D
D = new JButton("D");
D.addActionListener(this);
add(D);
//Creates a button for letter W
7. W = new JButton("W");
W.addActionListener(this);
add(W);
//Creates a button for letter H
H = new JButton("H");
H.addActionListener(this);
add(H);
//Creates a button for letter I
I = new JButton("I");
I.addActionListener(this);
add(I);
//Creates a button for letter C
C = new JButton("C");
C.addActionListener(this);
add(C);
//Creates a button for the second letter H
H2 = new JButton("H");
H2.addActionListener(this);
add(H2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Stops program when window
closes
}
public void actionPerformed(ActionEvent e) {
//Creates action performed when the letters buttons are clicked
if(e.getSource() == S) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
} else if(e.getSource() == A) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
} else if(e.getSource() == N) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
} else if(e.getSource() == D) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
8. JOptionPane.PLAIN_MESSAGE);
//Congratulates the user and closes the program
} else if(e.getSource() == W) {
JOptionPane.showMessageDialog(null, "That is correct!!", "Correct",
JOptionPane.PLAIN_MESSAGE);
System.exit(0);
} else if(e.getSource() == H) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
} else if(e.getSource() == I) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
} else if(e.getSource() == C) {
JOptionPane.showMessageDialog(null, "It does not start here", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
} else if(e.getSource() == H2) {
JOptionPane.showMessageDialog(null, "This is not a compound Word", "Incorrect",
JOptionPane.PLAIN_MESSAGE);
}
}
}