Question

In: Computer Science

Using java, commenting should be proper, along with testing for valid and invalid inputs: Develop a...

Using java, commenting should be proper, along with testing for valid and invalid inputs:

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

Solutions

Expert Solution

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.


Related Solutions

Graphical User Interfaces using java. Please provide proper commenting so that I understant what is going...
Graphical User Interfaces using java. Please provide proper commenting so that I understant what is going on in the program: 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 labeled fields that display...
Using a truth table determine whether the argument form is valid or invalid p ∧ q...
Using a truth table determine whether the argument form is valid or invalid p ∧ q →∼ r p∨∼q ∼q→p ∴∼ r
Please answer this using MATLAB (Not c or c++) and make sure to provide proper commenting:...
Please answer this using MATLAB (Not c or c++) and make sure to provide proper commenting: a. Write a function that writes a series of random Fahrenheit temperatures and their corresponding Celsius temperatures to a tab-delimited le. Use 32 to 212 as your temperature range. From the user, obtain the following: 1) The number of temperatures to randomly generate. 2) The name of the output file. b. Write a function that reads a file produced by part (a). Focusing only...
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5...
Develop a program in C++, using functions, to validate a userID. Valid userID specifications: • 5 - 10 characters long. • must begin with a letter. • must contain at least one upper case letter. • must contain at least one lower case letter. • must contain at least one decimal digit. • must contain at least one of the following special characters: #_$ • must not contain any other characters than those specified above. The main program should loop,...
(USING JAVA) Implement a class Moth that models a moth flying along a straight line. The...
(USING JAVA) Implement a class Moth that models a moth flying along a straight line. The moth has a position which is the distance from a fixed origin. When the moth moves toward a point of light its new position is halfway between its old position and the position of the light source. Supply a constructor public Moth(double initialPosition) and methods public void moveToLight(double lightPosition) public double getPosition()
Develop a Java application using Parboiled library to write aparser for a customer form. Your...
Develop a Java application using Parboiled library to write a parser for a customer form. Your program should include a grammar for the parser and display the parse tree with no errors.The customer form should include the following structure:First name, middle name (optional), last nameStreet address, city, state (or province), countryPhone numberRules• First name, middle name and last name should start with an uppercase letter followed by lower case letters. Middle name is an optional input meaning such an input...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart...
Develop an Algorithm and java program using Design Recipe for the following problems. Draw a flowchart to compute the largest and smallest of 4 numbers : Write a Java Program for the above flowchart, use methods(functions), and nested if-else statements. Take input from the user using the Scanner object. Use separate methods to compute the Largest and Smallest of the numbers. Method 1 Name: findLargest(param 1, param 2, param 3, param 4) Method 2 Name: findSmallest(param 1, param 2, param...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT