SlideShare a Scribd company logo
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",
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;
//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){
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;
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
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",
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);
}
}
}

More Related Content

DOCX
III B.TECH CSE_flutter Lab manual (1).docx
PDF
Using database in android
PDF
To-Do App With Flutter: Step By Step Guide
PDF
PPTX
SenchaCon 2016: Keynote Presentation - Art Landro, Gautam Agrawal, Mark Brocato
PDF
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
PPTX
GUI Programming with Java
PPTX
Introducing Wire for Drupal, the Livewire concepts adapted to work with Drupal
III B.TECH CSE_flutter Lab manual (1).docx
Using database in android
To-Do App With Flutter: Step By Step Guide
SenchaCon 2016: Keynote Presentation - Art Landro, Gautam Agrawal, Mark Brocato
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
GUI Programming with Java
Introducing Wire for Drupal, the Livewire concepts adapted to work with Drupal

Similar to So I need to create a compound word game, and I dont quite understa.pdf (20)

PDF
Manual tecnic sergi_subirats
PPTX
PDF
I hope the below code is helpful to you, please give me rewards.im.pdf
PDF
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
PDF
Code to copy Person.java .pdf
PDF
final project for C#
PDF
Java ProgrammingImplement an auction application with the followin.pdf
PDF
Introduction to ECMAScript 2015
PDF
JavaScript ES6
PDF
Convert the following program so that it uses JList instead of JComb.pdf
PPTX
Dev Day 2024: Jonathan Frere - Playwright: Das Beste aus dem Dramatiker herau...
DOCX
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
PPTX
10 awt event model
PDF
Why am I getting an out of memory error and no window of my .pdf
PPTX
Gui programming a review - mixed content
DOCX
gUIsInterfacebuild.xml Builds, tests, and runs the pro.docx
PDF
Program-a. library is importedimport java.awt.; import j.pdf
PDF
PasswordCheckerGUI.javaimport javafx.application.Application; im.pdf
PPTX
Object Oriented Programming Session 4 part 2 Slides .pptx
PPTX
GUI programming
Manual tecnic sergi_subirats
I hope the below code is helpful to you, please give me rewards.im.pdf
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
Code to copy Person.java .pdf
final project for C#
Java ProgrammingImplement an auction application with the followin.pdf
Introduction to ECMAScript 2015
JavaScript ES6
Convert the following program so that it uses JList instead of JComb.pdf
Dev Day 2024: Jonathan Frere - Playwright: Das Beste aus dem Dramatiker herau...
C#_hw9_S17.docModify AnimatedBall on Chapter 13. Instead of dr.docx
10 awt event model
Why am I getting an out of memory error and no window of my .pdf
Gui programming a review - mixed content
gUIsInterfacebuild.xml Builds, tests, and runs the pro.docx
Program-a. library is importedimport java.awt.; import j.pdf
PasswordCheckerGUI.javaimport javafx.application.Application; im.pdf
Object Oriented Programming Session 4 part 2 Slides .pptx
GUI programming
Ad

More from leolight2 (20)

PDF
Si el gobierno se hubiera apoderado de los activos de Global Trading.pdf
PDF
Si bien puede diferir en otros pa�ses, en los Estados Unidos la dist.pdf
PDF
SHOW STEPS IN EXCEL PLEASE.Data DayDateWeekdayDaily Deman.pdf
PDF
Si bien la mayor�a de los visitantes de Washington, DC, visitan los .pdf
PDF
Si bien es importante que los vendedores internacionales aprecien .pdf
PDF
Show the Relational Algebra formula for each. This one may be a phot.pdf
PDF
should be at least two paragraphs long, or more, depending upon the .pdf
PDF
Should Governments Report Like BusinessesHistorically, states a.pdf
PDF
Seventy-three percent of adults in a certain country believe that li.pdf
PDF
Sheffield Corporation, a private corporation, was organized on Febru.pdf
PDF
SHOW ANSWER ON GRAPH Many demographers predict that the United State.pdf
PDF
Ser capaz de amplificar fragmentos de ADN espec�ficos es fundamental.pdf
PDF
Solve all of them If the marginal propensity to save is 0.25 , then.pdf
PDF
Solution Register a service endpoint in the Dataverse instance that.pdf
PDF
Solutions for The Toliza Museum of Art, did not cover all the answer.pdf
PDF
Soles es una empresa de calzado que ha abierto recientemente su tien.pdf
PDF
So I have 3 enums named land_type, entity, tile as shown enum lan.pdf
PDF
So for C-ferns the CP is the commonwild type (green) and the cp is .pdf
PDF
SLL Corporation�s balance sheet is shown below. The current rate on .pdf
PDF
So here is the code from the previous assignment that we need to ext.pdf
Si el gobierno se hubiera apoderado de los activos de Global Trading.pdf
Si bien puede diferir en otros pa�ses, en los Estados Unidos la dist.pdf
SHOW STEPS IN EXCEL PLEASE.Data DayDateWeekdayDaily Deman.pdf
Si bien la mayor�a de los visitantes de Washington, DC, visitan los .pdf
Si bien es importante que los vendedores internacionales aprecien .pdf
Show the Relational Algebra formula for each. This one may be a phot.pdf
should be at least two paragraphs long, or more, depending upon the .pdf
Should Governments Report Like BusinessesHistorically, states a.pdf
Seventy-three percent of adults in a certain country believe that li.pdf
Sheffield Corporation, a private corporation, was organized on Febru.pdf
SHOW ANSWER ON GRAPH Many demographers predict that the United State.pdf
Ser capaz de amplificar fragmentos de ADN espec�ficos es fundamental.pdf
Solve all of them If the marginal propensity to save is 0.25 , then.pdf
Solution Register a service endpoint in the Dataverse instance that.pdf
Solutions for The Toliza Museum of Art, did not cover all the answer.pdf
Soles es una empresa de calzado que ha abierto recientemente su tien.pdf
So I have 3 enums named land_type, entity, tile as shown enum lan.pdf
So for C-ferns the CP is the commonwild type (green) and the cp is .pdf
SLL Corporation�s balance sheet is shown below. The current rate on .pdf
So here is the code from the previous assignment that we need to ext.pdf
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Computing-Curriculum for Schools in Ghana
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
GDM (1) (1).pptx small presentation for students
O7-L3 Supply Chain Operations - ICLT Program
Complications of Minimal Access Surgery at WLH
Final Presentation General Medicine 03-08-2024.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Computing-Curriculum for Schools in Ghana
VCE English Exam - Section C Student Revision Booklet
Orientation - ARALprogram of Deped to the Parents.pptx
Yogi Goddess Pres Conference Studio Updates
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Supply Chain Operations Speaking Notes -ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers

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); } } }