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

Not in functions You are required to open an input file (shown below) then read

ID: 3712691 • Letter: N

Question

Not in functions

You are required to open an input file (shown below) then read one character at

a time until all characters in the file are all read. While reading the content

of the file, keep a count of the following:

1)

Count of each digit read. The counts should be kept in an int array of

size 10.

2)

Count of each alphabetic character read irrespective if it is an upper

case or lower case. The counts should be kept in an int array of size 26.

3)

Count of whitespace characters. A whitespace includes the blank (space) ‘

’, new line character , tab character , form feed , Vertical tab

and carriage return .

4)

Count of all other characters not alphabetic, not a digit and not a

whitespace.

5)

Total count of all characters read.

After reading and processing the file, and get each count correctly, you will

display each letter of alphabet and its count, each digit and its count, count

of whitespaces and all other counts.

Your solution must use

FUNCTIONAL DECOMPOSITION.

Explanation / Answer

Java Code:

import java.io.*;

import java.util.*;

class test

{

public static void main(String []args)throws IOException{

Scanner sc = new Scanner(System.in);

System.out.println("Enter the name of the file: ");

String fname = sc.next();

FileReader fr = new FileReader(fname);

int numFreq[]=new int[10]; //stores the frequency of numbers

int charFreq[]=new int[26];//stores the frequency of characters

String whiteSpace[]={"Blank Space","New line","Tab","form feed","Vertical Tab","Carriafge return "};

int whiteSpaceFreq[]=new int[6];

int othersFreq=0; //stores frequency of non-digit,letter or whitespace characters

int totalChars=0;//stores total characters

int i;

while ((i=fr.read()) != -1){

char inp=(char)i;

totalChars++;

//if inp is a digit record its count

if(inp>=48 && inp<=57){

numFreq[inp-'0']++;

}

//if inp is a upper case character increase its count

else if(inp>=65 && inp<=90){

charFreq[inp-'A']++;

}

//if inp is a lower case character increase its count

else if(inp>=97 && inp<=122){

charFreq[inp-'a']++;

}

//if inp is a whitespace character increse its frequency

else if(inp==' ')

whiteSpaceFreq[0]++;

else if(inp==' ')

whiteSpaceFreq[1]++;

else if(inp==' ')

whiteSpaceFreq[2]++;

else if(inp==' ')

whiteSpaceFreq[3]++;

else if(inp==' ')

whiteSpaceFreq[5]++;

else

othersFreq++;

}

//printing results

System.out.println("Digits: ");

for( i=0;i<10;i++){

System.out.println(i+" " +numFreq[i]);

}

System.out.println("Characters: ");

for( i=0;i<26;i++){

System.out.println((char)(97+i) +" " +charFreq[i]);

}

System.out.println("Whitespace Characters: ");

for( i=0;i<6;i++){

System.out.println(whiteSpace[i] +" " +whiteSpaceFreq[i]);

}

System.out.println("other characters: "+othersFreq);

System.out.println("Total characters: "+totalChars);

}

  

}

Sample input output:

radas-macOS:Desktop radas$ java test
Enter the name of the file:
wrongWords.txt
Digits:
0 0
1 2
2 2
3 3
4 0
5 3
6 0
7 0
8 0
9 0
Characters:
a 1
b 0
c 0
d 3
e 0
f 3
g 2
h 1
i 1
j 2
k 1
l 0
m 0
n 0
o 0
p 0
q 1
r 0
s 1
t 0
u 2
v 0
w 3
x 0
y 0
z 0
Whitespace Characters:
Blank Space 0
New line 2
Tab 2
form feed 0
Vertical Tab 0
Carriafge return 0
other characters: 0
Total characters: 35

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