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

Create a two class application - a driver and a principal class - that reads in

ID: 3627199 • Letter: C

Question

Create a two class application - a driver and a principal class - that reads in text interactively from the keyboard, and then reports on two aspects of the entered text. First it should print a table of word sizes in the entered text, for sizes from 1 up to 12 and larger; and second, it should report the number of sentences in the text. To be clear on how this should work, here is the text from a sample run:

> run WordProfileDriver
Enter lines of text, type two returns in a row to end

[DrJava Input Box] The time has come, the walrus said - to speak of many things
[DrJava Input Box] At least that's how the ultra-ultra-famous first line goes, from the
[DrJava Input Box]the Lewis Carroll poem! If you don't know it, give it a try.

words of size 1---2
words of size 2---6
words of size 3---9
words of size 4---10
words of size 5---5
words of size 6---3
words of size 7---1
words of size 8---0
words of size 9---0
words of size 10---0
words of size 11---0
words of size >= 12---1
sentences: 2


As you can see, the driver class is called WordProfileDriver (you must use this name). The second class, where all the action happens, must be called WordProfile. Some further thoughts:


Read input using the style of program Backwards from section 7.2 of the iJava textbook.
This is how the Backwards program look like:
import java.util.*;

public class Backwards{
public static void main(String[] args){
String[] lines = new String[50];
Scanner scan = new Scanner(System.in);
int pos = 0;
String t = " ";
while(t.length() > 0){
t = scan.nextLine();
lines[pos] = t;
pos++;
}
for(int j = pos - 1; j >= 0; j--){
lines[j] = lines[j].toUpperCase();
System.out.println(lines[j]);
}
}
}

You must make essential use of arrays in your word size accounting. Notice that words that are of size 12 or larger are lumped together.

You will need to use a StringTokenizer for this problem. In addition to "space", these characters should be considered delimiters: ,.?!:; This means that dash - the symbol "-", and single quote - the symbol " ' ", should be considered characters in a word (and thus "can't" is a single word of length 5, a lone dash is actually a word of size 1, and "head-long" is a single word of length 9).

To count sentences, just count the number of ? (question mark), !(exclamation mark), .(period). Just counting these three sentence terminators is fairly easy, although strictly speaking it's not completely correct, since a sentence might end in ??, for example.

Lines should be read in in the driver, but should be processed using StringTokenizer in WordProfile. Also, the essential array, where where you keep track of word size counts, MUST be in the WordProfile class, and not in the driver.

Explanation / Answer

please rate - thanks

import java.util.*;

public class WordProfileDriver{
public static void main(String[] args){
String sent="",in;
Scanner scan = new Scanner(System.in);
System.out.println("Enter lines of text, type two returns in a row to end: ");

in=scan.nextLine();
while(in.length()>0)
      {sent=sent+in;
        in=scan.nextLine();
        }
WordProfile sentence=new WordProfile();
sentence.count(sent);
for(int i=1;i<=12;i++)
    System.out.println("words of size "+i+"---"+sentence.getCount(i));
System.out.println("sentences: "+sentence.getSentCount());   
}
}

------------------------------------------

import java.util.*;
public class WordProfile
{private int sentences;
private int[] counts=new int[12];
public WordProfile()
{sentences=0;
for(int i=0;i<12;i++)
     counts[i]=0;
   
}
public void count(String s)
{String delims = " ,.?!:;",w="";
int l;
StringTokenizer tokens = new StringTokenizer(s, delims);
while(tokens.hasMoreTokens())
      {w=tokens.nextToken();
        if(w.length()>12)
             l=12;
        else
             l=w.length();
        counts[l-1]++;
        }
delims = ".?!";
StringTokenizer tokens2 = new StringTokenizer(s, delims);
while(tokens2.hasMoreTokens())
      {sentences++;
        w=tokens2.nextToken();
      }
   
   
   
    }
public int getCount(int i)
{return counts[i-1];
}
public int getSentCount()
{return sentences;
}
}

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