Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java that computes the number of words in a text entered in a

ID: 3758466 • Letter: W

Question

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 button that, when pressed, computes the minimum, maximum, and average word count
a button that, when pressed, computes the number of numeric tokens
a button that, when pressed, computes word frequencies
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.

Explanation / Answer

Answer :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
  
class WordCounterApplet extends JApplet
implements ActionListener {
  
JTextField textInput; // For the user's input text.

JLabel wordCountLabel; // For displaying the number of words.

  
  
public void init() {
  
setBackground(Color.darkGray);
getContentPane().setBackground(Color.darkGray);
  
textInput = new JTextField();
textInput.setBackground(Color.white);
  
  
JPanel south = new JPanel();
south.setBackground(Color.darkGray);
south.setLayout( new GridLayout(4,1,2,2) );
  
/* Create the button, set the applet to listen for
clicks on the button, and add it to the panel. */
  
JButton countButton = new JButton("Process the Text");
countButton.addActionListener(this);
south.add(countButton);
  
wordCountLabel = new JLabel(" Number of words:");
wordCountLabel.setBackground(Color.white);
wordCountLabel.setForeground(Color.blue);
wordCountLabel.setOpaque(true);
south.add(wordCountLabel);
  


getContentPane().setLayout( new BorderLayout(2,2) );

  
JScrollPane scroller = new JScrollPane( textInput );
getContentPane().add(scroller, BorderLayout.CENTER);
getContentPane().add(south, BorderLayout.SOUTH);
  
} // end init();


public Insets getInsets() {
  
return new Insets(2,2,2,2);
}

  
public void actionPerformed(ActionEvent evt) {
// Respond when the user clicks on the button by getting
// the text from the text input area, counting the number
// of chars, words, and lines that it contains, and
// setting the labels to display the data.

String text;

int wordCt ;

text = textInput.getText();

/* Compute the wordCt by counting the number of characters
in the text that lie at the beginning of a word. The
beginning of a word is a letter such that the preceding
character is not a letter. This is complicated by two
things: If the letter is the first character in the
text, then it is the beginning of a word. If the letter
is preceded by an apostrophe, and the apostrophe is
preceded by a letter, than its not the first character
in a word.
*/

wordCt = 0;
for (int i = 0; i < charCt; i++) {
boolean startOfWord; // Is character i the start of a word?
if ( Character.isLetter(text.charAt(i)) == false )
startOfWord = false; // No. It's not a letter.
else if (i == 0)
startOfWord = true; // Yes. It's a letter at start of text.
else if ( Character.isLetter(text.charAt(i-1)) )
startOfWord = false; // No. It's a letter preceded by a letter.
else if ( text.charAt(i-1) == ''' && i > 1
&& Character.isLetter(text.charAt(i-2)) )
startOfWord = false; // No. It's a continuation of a word
// after an apostrophe.
else
startOfWord = true; // Yes. It's a letter preceded by
// a non-letter.
if (startOfWord)
wordCt++;
}




wordCountLabel.setText(" Number of Words: " + wordCt);

  
} // end actionPerformed()
  

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote