In this question you are asked to implement a program that finds subsequence tha
ID: 3889468 • Letter: I
Question
In this question you are asked to implement a program that finds subsequence that contains the highest frequency of vowels given a short paragraph. For simplicity, we will be interested in subsequences of a particular length, n that will be provided as part of the input.
Write a program that takes the name of a plain text file that contains a short paragraph and an integer n. You will then find the indices of the subsequence of paragraph of length n that contain the highest frequency of vowels in it. For example, if the paragraph is
and the “window” size that we’re interested in is n = 15 then you would scan the para- graph (including all whitespace) and find every subsequence with the maximum number of vowels. Your output should include all substrings (by indices) in the paragraph similar to the following.
Java: Name your class vowelsFreq. You must use command line arguments
to read the input; so the command line usage for the example above would be:
1 The class teacher asks students to name an animal that begins with an ggE,, .. 3 | One boy says, ‘'Elephant. 4 Then the teacher asks for an animal that 5 | begins with a ‘'T, ,. 6 | The same boy says, ‘'Two elephants." 7 The teacher sends the boy out of the class 8 for bad behavior 9After that she asks for an animal beginning 10 with 'M. 11 The boy shouts from the other side of the wall: 12 |‘"Maybe an elephant!', at begins tusExplanation / Answer
import java.util.*;
import java.io.*;
public class vowelsFreq
{
public static void main(String args[])
{
String str="";
int n=Integer.parseInt(args[1]);
int len,i,j,max=0,freq;
List<Integer> li=new ArrayList<Integer>();
List<Integer> lj=new ArrayList<Integer>();
int k=0;
try
{
str = new Scanner(new File(args[0])).useDelimiter("\Z").next();
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
len=str.length();
for(i=0;i<len-n;i++)
{
freq=0;
for(j=i;j<i+n;j++)
{
char ch=str.charAt(j);
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
{
freq++;
}
}
if(freq>max)
{
max=freq;
li.clear();
lj.clear();
k=0;
li.add(k,i);
lj.add(k,j);
}
else if(freq==max)
{
k++;
li.add(k,i);
lj.add(k,j);
}
}
System.out.println("n = "+n);
System.out.println("highest frequency: "+max+"/"+n+" = "+((max/n)*100)+"%");
System.out.println("High vowel frequency parts: ");
for(i=0;i<=k;i++)
{
int a,b;
a=li.get(i);
b=lj.get(i);
System.out.println(a+" thru "+b+" : "+str.substring(a,b));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.