Objectives To gain experience with string objects To gain ex

Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. Documentation: Explain the purpose of the program as detail as possible - 8%. Develop a solution for the problem and mention algorithms to be used -12% List data structures to be used in solution. - 5%. Give a description of how to use the program and expected input/output - 5%. Explain the purpose of each class you develop in the program. - 5%. Programming: Program execution according to the requirements given 60% Naming of program as required 5% Description of program: You arc to write a program name wordcount.java that prompt the user for a user input file name, reads the input words and do the following with those words: Count the amount of words in the file. A word can end with any of the following: space (single or multiple), an EOLN character or a punctuation mark (which will be part of the word). Count the amount of lines in the file. Count the amount of alphanumeric characters in the file. Count the number of sentences in the file. Count the amount of vowels in the file - only a, e, i,  o,  u (lower and upper ease) are vowels. Count the amount of punctuations in the file. You must output the above information both on the screen as well as an output file name \"output.txt\". Here are some requirements for the program: The program must work even if the input file is empty. If this is the ease print a message saying that \"the input file is empty\" and then terminate the program. Implementation: Use as many generic algorithm as possible sothat the size of the program can be reduced.

Solution

import java.io.*;

public class wordCount {

   public static void main(String args[]) {

       File f = new File(\"e:\\\\cs1.txt\");

       if(f.exists()){
           FileReader fr = new FileReader(f);
           BufferedReader br = new BufferedReader(fr);
           String line = \"\", str = \"\";
           int a = 0;
           int b = 0;
           while ((line = br.readLine()) != null) {
               str += line + \" \";
               b++;
           }
           System.out.println(\"Total \" + b + \" lines in a file\");

           System.out.println(str);

           StringTokenizer st = new StringTokenizer(str);
           while (st.hasMoreTokens()) {
               String s = st.nextToken();
               a++;
           }
           System.out.println(\"Total \" + a + \" words are in the file\");

           System.out.println(\"Total punctuations are in the file\");


           String s = str.next();
           countPunctuation(s);

           for(int i = 0; i < str.length(); i ++)
           {
               ch = str.charAt(i);

               if(ch == \'a\' || ch == \'A\' || ch == \'e\' || ch == \'E\' || ch == \'i\' ||
               ch == \'I\' || ch == \'o\' || ch == \'O\' || ch == \'u\' || ch == \'U\')
                   vowels ++;
               else if(Character.isDigit(ch))
                   digits ++;
               else if(Character.isWhitespace(ch))
                   blanks ++;
           }

           System.out.println(\"Vowels : \" + vowels);
           System.out.println(\"Digits : \" + digits);
           System.out.println(\"Blanks : \" + blanks);

       }else{
           System.out.println(\"File not found!\");
       }

       }

   public static int countPunctuation(String s)
   {
       int periodCount = 0;
       int commaCount = 0;
       int semicolonCount = 0;
       int colonCount = 0;
       int exclamationCount = 0;
       int questionCount = 0;
       int total = 0;


       for(int i = 0; i < s.length(); i++)
       {
           if(s.charAt(i) = \".\")
           {
               periodCount++;
               total++;
           }
           if(s.charAt(i) = \",\")
           {
               commaCount++;
               total++;
           }
           if(s.charAt(i) = \";\")
           {
               semicolonCount++;
               total++;
           }
           if(s.charAt(i) = \":\")
           {
               colonCount++;
               total++;
           }
           if(s.charAt(i) = \"!\")
           {
               exclamationCount++;
               total++;1
           }
           if(s.charAt(i) = \"?\")
           {
               questionCount++;
               total++;
           }

       }

       System.out.println(\"There are \" + periodCount + \" periods in this String.\");
       System.out.println(\"There are \" + commaCount + \" commas in this String.\");
       System.out.println(\"There are \" + semicolonCount + \" semicolons in this String.\");
       System.out.println(\"There are \" + colonCount + \" colons in this String.\");
       System.out.println(\"There are \" + exclamationCount + \" exclamation marks in this String.\");
       System.out.println(\"There are \" + questionCount + \" quesiton marks in this String.\");


       return total;
   }

}

 Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. D
 Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. D
 Objectives: To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files - opening for input and output. D

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site