Please I need help with writing a program for my computer science course,One of
ID: 3757667 • Letter: P
Question
Please I need help with writing a program for my computer science course,One of our assignment is to create a program using applets. Below I have posted the assignment from the instructor. Thanks for your help
Write a program in Java that computes the number of words in a text entered in a text field. Use the following graphical objects:
a text field for the input text (one line of text)
labels to output the word count
a button that, when pressed, computes the word count
a button that, when pressed, clears the text field
a panel, that includes all graphical objects listed above
Make your application run in a window (JFrame) and use a proper layout, backgrounds and colors to make all objects clearly visible.
Extra credit (maximum 2 points) will be given for additional text statistics (for example, maximum, minimum and average word size, number of numeric tokens, word frequencies etc.).
Documentation: Run your program and make sure it compiles without errors and works as described above. Then add your name in the beginning of the code as a comment. Add also comments to explain the classes and methods used. Note that comments and the way you format your program will be graded too.
Explanation / Answer
Please find the code below.
There are two classes; 1. MainClass.java, 2. CountWords.java
save them on 2 different files with the name same as class name and run as Java applet
1. MainClass.java
package chegg_applet;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainClass extends Applet {
private TextField input;
private Label output;
public void init () {
// Construct the TextFields
this.input = new TextField(40);
this.output = new Label("Enter Text and press count word for word counts.");
Button b = new Button("Count Words");
Button clear = new Button("Clear");
// add the button to the layout
this.add(input);
this.add(b);
this.add(clear);
this.add(output);
// specify that action events sent by the
// button or the input TextField should be handled
// by the same CountWords object
CountWords ca = new CountWords(input, output);
b.addActionListener(ca);
this.input.addActionListener(ca);
clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
input.setText("");
output.setText("Enter Text and press count word for word counts.");
}
});
// notice that ActionEvents produced by output are ignored.
}
}
2. CountWords.java
package chegg_applet;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CountWords implements ActionListener {
private TextField in;
private Label out;
public CountWords(TextField in, Label out) {
this.in = in;
this.out = out;
}
public void actionPerformed(ActionEvent ae) {
String s = in.getText();
String[] tokens = s.split(" ");
out.setText("Number of words in string is: " + tokens.length);
}
}