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

For all assignments, always use comments to explain your program. No copying all

ID: 3784067 • Letter: F

Question

For all assignments, always use comments to explain your program. No copying allowed. If it is found that students copy from each other (in the opinion of the instructor), all of these programs will get 0. You must use the names given to name your program. Should you use different names, you would lose 5% of what each program is worth. To gain experience with string objects. To gain experience with generic algorithms. To gain experience with files- opening for input and output. 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 inputoutput 5% Explain the purpose of each class you develop in the program. -5%. Program execution according to the requirements given 60% Naming of program as required 5% You are 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 oflines 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 case) 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" The program must work even if the input file is empty. If this is the case print a message saying that "the input file is empty" and then terminate the program. Use as many generic algorithm as possible so that the size of the program can be reduced.

Explanation / Answer

import java.util.*;
import java.io.*;
import jpb.*;
public class wordcount{
public static void main(String[] args)
{
String input_file_name,input;
int line_count=0,vowel_count=0,punctuation_count=0,char_count=0,word_count=0,sentence_count=0;
int i,j;
char ch;
char vowel[]={'A','a','E','e','I','i','O','o','U','u'};
char punctuation[]={'.','!',',','?',':',';'};
char white_space[]={' ',' ',' '};
Scanner inp=new Scanner(System.in);
input_file_name=inp.next();
PrintStream file_output = new PrintStream(new File("output.txt"));
// Store current System.out before assigning a new value
PrintStream console = System.out;

Scanner file_input=new Scanner(new FileReader(input_file_name));
   if(!file_input.hasNext())
   {
       System.out.println("Input file is empty");
       System.exit(1);
   }
   while(file_input.hasNextLine())
   {
       input=file_input.nextLine();
        word_count++;
       for(i=0;i<input.length();i++)
       {
           ch=input.charAt(i);
            char_count++;
           for(j=0;j<white_space.length;j++)
            {
               if(ch==white_space[j])
            word_count++;
            }
            for( j=0;j<punctuation.length;j++)
       if(ch==punctuation[j])
        {
               punctuation_count++;
            if(j<3)
           sentence_count++;
       }
            for( j=0;j<vowel.length;j++)
       if(ch==vowel[j])
       vowel_count++;
        }
line_count++;
}

// Use stored value for output stream
System.setOut(console);
System.out.println("This will be written on the console!");

System.out.println("Amount of words in a file: "+word_count);
System.out.println("Amount of linesin a file: "+line_count);
System.out.println("Number of sentences in a file: "+sentence_count);
System.out.println("Amount of vowels in a file: "+vowel_count);
System.out.println("Amount of characters in a file: "+char_count);
System.out.println("Amount of punctuations in a file: "+punctuation_count);

// Assign file_output to output stream
System.setOut(file_output);
System.out.println("This will be written to the text file");

System.out.println("Amount of words in a file: "+word_count);
System.out.println("Amount of linesin a file: "+line_count);
System.out.println("Number of sentences in a file: "+sentence_count);
System.out.println("Amount of vowels in a file: "+vowel_count);
System.out.println("Amount of characters in a file: "+char_count);
System.out.println("Amount of punctuations in a file: "+punctuation_count);

file_output.close();
file_input.close();
System.exit(0);
}

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