Write a GUI based program that analyzes a word. THe user will type the word in a
ID: 3554376 • Letter: W
Question
Write a GUI based program that analyzes a word. THe user will type the word in a text field. Provide buttons for the following:
For this, you should design and code a separate (non-GUI) class encapsulating a word and its analysis, then instantiate an object of that class inside your GUI class and call the various methods as needed.
*Extra Notes
Be sure that you follow the projects suggestion and create a separate class for the word analysis. An instance of this class and its methods should then be called by your GUI interface.
Your solution must include a comment block at the top of the code that includes
Your solution must demonstrate appropriate documentation. You should strive to select identifier names that make your code as self-documenting as possible.
Explanation / Answer
gui.java
package chegg;
import java.awt.GridLayout;
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.JPanel;
import javax.swing.JTextField;
public class gui {
public static void main(String[] args) {
JFrame frame = new JFrame("Word Processor");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout();
layout.setRows(1);
JPanel panel = new JPanel();
panel.setLayout(layout);
final JTextField input = new JTextField();
panel.add(input);
JButton vowels = new JButton("Get no. of vowels");
panel.add(vowels);
JButton consonants = new JButton("Get no. of Consonants");
panel.add(consonants);
JButton length = new JButton("Get length");
panel.add(length);
final JLabel ans = new JLabel("0");
panel.add(ans);
vowels.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
ans.setText(String.valueOf(ProcessWord.getVowels(input
.getText())));
}
});
consonants.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
ans.setText(String.valueOf(ProcessWord.getConsonants(input
.getText())));
}
});
length.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
ans.setText(String.valueOf(ProcessWord.getLength(input
.getText())));
}
});
frame.add(panel);
frame.pack();
frame.show();
}
}
ProcessWord.java
package chegg;
public class ProcessWord {
static int getLength(String word) {
return word.length();
}
static int getConsonants(String word) {
return word.length() - getVowels(word);
}
static int getVowels(String word) {
word.toLowerCase();
int ans = 0;
int counter;
for (counter = 0; counter < word.length(); counter++) {
switch (word.charAt(counter)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
ans++;
break;
default:
break;
}
}
return ans;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.