Question

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

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)
  • a button that, when pressed, computes the word count
  • a button that, when pressed, clears the text field
  • labels for the input and output and for showing the word count

Requirements and Restrictions: Choose proper background, color, and layout and arrange the graphical objects exactly as shown in the example below. Make sure the layout does not change when resizing the window.

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.).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class CountWordsGUI
{
public static void main(String args[])
{
JFrame f1=new JFrame();
f1.setLayout(null);
f1.setBackground(Color.green);
f1.setForeground(Color.red);
JLabel inLbl=new JLabel("Enter a sentence :");
JLabel outLb2=new JLabel();
JTextField txtInput=new JTextField();
JButton btnT1=new JButton("ShowCount");
JButton btnT2=new JButton("Clear");

btnT1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int count=0;
String line=txtInput.getText();
String words[]=line.split(" ");
for(int i=0;i<words.length;i++)
{
count=count+1;
}
outLb2.setText("Number of words in a text : "+count);
}
});

btnT2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
txtInput.setText("");
outLb2.setText("");
}
});
inLbl.setBounds(100,50,100,30);
txtInput.setBounds(210,50,200,30);
btnT1.setBounds(100,120,100,30);
btnT2.setBounds(210,120,100,30);
outLb2.setBounds(100,190,300,30);

f1.add(inLbl);
f1.add(outLb2);
f1.add(txtInput);
f1.add(btnT1);
f1.add(btnT2);


f1.setVisible(true);
f1.setSize(460,350);

f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Add a comment
Know the answer?
Add Answer to:
Write a program in Java that computes the number of words in a text entered in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • IN PYTHON 3: Write a Python program to read the attached text file containing words, count...

    IN PYTHON 3: Write a Python program to read the attached text file containing words, count the occurrence of each unique word and store the words in alphabetical order in a text file listing the word and the total count of that word. You cannot use any advanced features such as dictionaries. The program requires user input to determine the name of the file. (example of the text file that needs to be read = for a brief moment I...

  • ***Please use java code for the question below*** Write a program that calculates the future value...

    ***Please use java code for the question below*** Write a program that calculates the future value of a given investment at a given interest rate for a specified number of years. The formula for this calculation is: value = investmentAmount * (1 + monthly interest rate)years*12 Use text fields for the user to enter the numbers (Investment Amount, Number of Years, and Annual Interest). To get/load data from the textbox, (in this case doubles) use the following structure: (bold are...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Write a calculator program using JavaScript in HTML in the same HTML file. (There will only...

    Write a calculator program using JavaScript in HTML in the same HTML file. (There will only be 1 HTML file containing everything that you use for this program.) *************JavaScript Functions should be written in the HTML <head> .............. </head> tag, **************** (Please be mindful of the formatting of the text of your program. Meaning that do not copy paste everything in a single line. Add clear comments for a better understanding of your program) as follows Assignment: create a form...

  • In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST...

    In Java, please write the program for the following program. Please TEST YOUR PROGRAM. You MUST use GUI, that means all user input must be through the windows. The "first window" is the window on the top left of the following picture. Add 5 more items to be purchased. Search is accessed from second window, top right. Thw third window is the bottom left, and the fourth window is the bottom right. The program MUST CONTAIN EVERYTHING IN THE PICTURES....

  • Make a C program to count the number of occurrences of words from the input. For...

    Make a C program to count the number of occurrences of words from the input. For example, with input "one two one three one two" your program should output: one 3 two 2 three 1 It should work for up to 100 different words. If there are more than 100 unique words in the input, the program should still work, and count the number of appearances of the first 100 unique words. Each word should have the same maximum amount...

  • Write a program using Java Streams that reads an arbitrary text file and creates pairs from...

    Write a program using Java Streams that reads an arbitrary text file and creates pairs from everything it finds in the file that is a number and the word that precedes that number. Example input: I need to buy 5 notebooks and 4 folders in 2 days for school. Output. buy 5 and 4 in 2

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • C++ Data Structures TREES You are to write a C++ program to count the frequency (number...

    C++ Data Structures TREES You are to write a C++ program to count the frequency (number of occurrences) of n-grams in a text file. Definition of n-gram is simple: it is the number of consecutive letters in a given text. For example, for the word bilkent the 2-grams (bigrams) are bi, il, lk, ke, en, nt. You may ignore any capitalizations and assume that the text file contains only English letters 'a'...'z', 'A'…'Z', and the blank space to separate words....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT