In: Computer Science
Using java, commenting should be proper, along with testing for valid and invalid inputs:
Develop a simple tool for analyzing a segment of text that determines the number of words in that segment and the average word length. The application should have a single window with a scrolling text box (JTextArea) and an area to display the statistics. The statistics area should be a panel with a titled border, containing labelled fields that display the number of words in the text and the average word length, as well as any other statistics you would like to add. The statistics area should also contain a button that, when pressed, computes the statistics for the current text content.
Hint: Use a Scanner object to parse the text content. The .next() operation will get the next word.
Design and implement an application that works as a stopwatch. Include a display that shows the time as it increments. The time value should be seconds only, to one decimal place. Include buttons that allow the user to start and stop the time, and to reset the display to zero. Arrange the components to present a nice user interface. The buttons should include a mnemonic shortcut. Only a single action listener should be used for the buttons.
TextStatistics.java
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TextStatistics extends JFrame implements ActionListener {
private static final long serialVersionUID =
1L;
// declare controls
private JTextArea textArea;
private JPanel panel;
private JLabel label1,label2,label3;
private JTextField words,average;
private JButton calculate,clear;
public TextStatistics() throws HeadlessException
{
setTitle("Text Statistics Tool");
// set title of frame
setSize(420, 500); // set size of
the frame
setLayout(null); // set layout as
null
setDefaultCloseOperation(EXIT_ON_CLOSE);
// instantiate all declared
controls
textArea=new JTextArea();
label1=new JLabel("Enter text
here:");
label2=new JLabel("Number of
words:");
label3=new JLabel("Avg length of
words:");
words=new JTextField();
average=new JTextField();
calculate=new
JButton("Calculate");
clear=new JButton("Clear");
// setting bounds to the
controls
label1.setBounds(150,35,200,30);
label2.setBounds(30,40,150,30);
label3.setBounds(30,80,150,30);
words.setBounds(150,40,100,30);
average.setBounds(150,80,100,30);
calculate.setBounds(30,120,100,30);
clear.setBounds(150,120,100,30);
textArea.setBounds(50,65,300,120);
// add text area and label into
frame
add(textArea);
add(label1);
// create panel
panel=new JPanel();
panel.setLayout(null);
// add controls into the
panel
panel.add(label2);
panel.add(label3);
panel.add(words);
panel.add(average);
panel.add(calculate);
panel.add(clear);
// set border as titled border to
panel
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK),
"Statistics"));
panel.setToolTipText("Statistic");
panel.setBounds(50,200,300,180);
add(panel); // add panel into the
frame
// adding action listener to the
buttons
calculate.addActionListener(this);
clear.addActionListener(this);
setVisible(true); // making frame
visible
}
@Override
public void actionPerformed(ActionEvent e) {
// if clear button is clicked
if(e.getSource()==clear) {
// clear text
area and both text fields
textArea.setText("");
words.setText("");
average.setText("");
}else
// if calculate
button is clicked
if(e.getSource()==calculate) {
String text=textArea.getText(); // get text from
text area
ArrayList<String> list=new
ArrayList<>(); // create 1 array list
// add all words to array list
for(String word:text.split(" "))
list.add(word);
words.setText(list.size()+""); // display words
count
int averageCount=0;
// calculate average count letters in all
words
for(String word:list)
averageCount+=word.length();
average.setText(averageCount/list.size()+""); //
display average letters count
}
}
public static void main(String[] args) {
new TextStatistics(); // anonymous
object of TextStatistics class
}
}
Output
Note--> At the time of entering text into text area don't use enter button.