Objectives: 1. To gain experience with string objects. 2. To gain experience wit
ID: 3663972 • Letter: O
Question
Objectives:
1. To gain experience with string objects.
2. To gain experience with generic algorithms.
3. To gain experience with files - opening for input and output.
4. To gain experience with the ArrayList data structure from the Java Library.
Documentation:
Explain the purpose of the program as detail as possible - 10%.
Develop a solution for the problem and mention algorithms to be used -10%
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:
For each method, give the pre and post conditions and invariant, if any - 10%
Program execution according to the requirements given 45%
Naming of program as required 5%
Print out of source code 5%
Description of program:
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 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 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".
Here are some requirements for the program:
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.
Implementation:
Use as many generic algorithm as possible so that the size of the program can be reduced.
Explanation / Answer
package chegg;
import java.util.*;
import java.io.*;
public class word{
public static void main(String[] args)throws FileNotFoundException
{String filename,input;
int lcount=0,vcount=0,pcount=0,ccount=0,wcount=0,scount=0;
int i,j;
char ch;
char vowel[]={'A','a','E','e','I','i','O','o','U','u'};
char punct[]={'.','!',',','?',':',';'};
char white[]={' ',' ',' '};
Scanner in=new Scanner(System.in);
System.out.print("Enter input file name: ");
filename=in.next();
try{
PrintStream foutput=new PrintStream(new File("output.txt"));
Scanner finput=new Scanner(new FileReader(filename));
if(!finput.hasNext())
{System.out.println(filename+"File is empty. Aborting Program");
System.exit(1);
}
while(finput.hasNextLine())
{input=finput.nextLine();
wcount++;
for(i=0;i<input.length();i++)
{ch=input.charAt(i);
ccount++;
for(j=0;j<white.length;j++)
{if(ch==white[j])
wcount++;
}
for( j=0;j<punct.length;j++)
if(ch==punct[j])
{pcount++;
if(j<3)
scount++;
}
for( j=0;j<vowel.length;j++)
if(ch==vowel[j])
vcount++;
}
lcount++;
}
System.out.println("words: "+wcount);
System.out.println("lines: "+lcount);
System.out.println("sentences: "+scount);
System.out.println("vowels: "+vcount);
System.out.println("characters: "+ccount);
System.out.println("punctuations: "+pcount);
foutput.println("words: "+wcount);
foutput.println("lines: "+lcount);
foutput.println("sentences: "+scount);
foutput.println("vowels: "+vcount);
foutput.println("characters: "+ccount);
foutput.println("punctuations: "+pcount);
foutput.close();
finput.close();
System.exit(0);
}catch ( FileNotFoundException e)
{System.out.println("C:\java word The file you entered either do not exist or the name is spelled wrong.");
System.exit(2);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.