Question

I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

I need help writing this code for java class.

Starter file: Project3.java and input file: dictionary.txt

Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following: at index [1], the number words of length 1. At index [2] then number of words of length 2 . . . at histogram[n] the number of words of length n. Since there will be no words of length zero, there will be a zero in histogram[0].

The histogram is just a plain array of int that is initialized to length zero. Recall that Java allows us to define a new array with a length of zero. It happens every time you run a Java program with no commnd line arguments. Doing so initializes the args array to have length zero. Every time you read a word from the dictionary you cannot just execute an increment statment such as

 ++histogram[word.length()]; 

This is the correct statement to execute, but you must first make sure the histogram is long enough to have a cell at that index. If your current word has length of 7, you must first make sure that your histogram array has a .length of at least 8 (not 7) because the [7] cell of the array is actually the eighth cell.

If you do need to upsize your histogram then only upsize it to be just big enough for that particular word. At the end of the program it is possible that you could have gaps in your histogram if (depending on the input file) there were no words of length twenty six. In this case there would still be a zero at the [26] cell in the array that was put there when the Java compiler initialized all the cells to zero.

Here is the starting code.

/* Project3.java  Dynamic histogram */

import java.io.*;
import java.util.*;

public class Project3
{
        static final int INITIAL_CAPACITY = 10;
        public static void main (String[] args) throws Exception
        {
                // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE
                if (args.length < 1 )
                {
                        System.out.println("\nusage: C:\\> java Project3 \n\n"); // i.e. C:\> java Project3 dictionary.txt
                        System.exit(0);
                }
                int[] histogram = new int[0]; // histogram[i] == # of words of length n

                /* array of String to store the words from the dictionary. 
                        We use BufferedReader (not Scanner). With each word read in, examine it's length and update word length frequency histogram accordingly.
                */

                String[] wordList = new String[INITIAL_CAPACITY];
                int wordCount = 0;
                BufferedReader infile = new BufferedReader( new FileReader(args[0]) );
                while ( infile.ready() )
                {
                        String word = infile.readLine();
                        // # # # # # DO NOT WRITE/MODIFY ANYTHING ABOVE THIS LINE # # # # #

                        // test to see if list is full. If needed do an up size (just like Lab#3)

                        // now you may safely append word onto list and incr count

                        // look at the word length and see if the histogram length is at least
                        // word length + 1. If not, you must upsize histogram to be exactly word length + 1
                        
                        // now you can increment the counter in the histogram for this word's length
                
                        //  # # # # # DO NOT WRITE/MODIFY ANYTHING BELOW THIS LINE  # # # # #
                } //END WHILE INFILE READY
                infile.close();

                wordList = trimArr( wordList, wordCount );
                System.out.println( "After final trim: wordList length: " + wordList.length + " wordCount: " + wordCount );

                // PRINT WORD LENGTH FREQ HISTOGRAM
                for ( int i = 0; i < histogram.length ; i++ )
                        System.out.format("words of length %2d  %d\n", i,histogram[i] );

        } // END main

        // YOU MUST CORRECTLY COPY THE STRING REFS FROM THE OLD ARR TO THE NEW ARR
        static String[] upSizeArr( String[] fullArr )
        {       
                return null; // just to make it complie you change as needed
        }
        static String[] trimArr( String[] oldArr, int count )
        {
                return null; // just to make it complie you change as needed
        }

        // YOU MUST CORRECTLY COPY THE COUNTS FROM OLD HISTO TO NEW HISTO
        static int[] upSizeHisto( int[] oldArr, int newLength )
        {
                return null; // just to make it complie you change as needed
        }
} // END CLASS PROJECT#3

If there is anything else you might need to write this, let me know and I will be sure to post it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


/* Project3.java Dynamic histogram */


import java.io.*;
import java.util.*;

public class Project3
{
static final int INITIAL_CAPACITY = 10;

public static void main(String[] args) throws Exception
{
if (args.length < 1 )
{
System.out.println("\nusage: C:\\> java Project3 \n\n"); // i.e. C:\> java Project3 dictionary.txt
System.exit(0);
}

int[] histogram = new int[0];

String[] wordList = new String[INITIAL_CAPACITY];
int wordCount = 0;
BufferedReader infile = new BufferedReader(new FileReader(args[0]));
while (infile.ready())
{
String word = infile.readLine();

// test to see if list is full. If needed do an up size
if (wordCount == wordList.length)
wordList = upSizeArr(wordList);
//now you may safely append word onto list and incr count
String nWord = infile.readLine();
wordList[wordCount++] = nWord;

// look at the word length and see if the histogram length is at least
// word length + 1. If not, you must upsize histogram to be exactly word length + 1
int wordLength = word.length();
if (word.length() > histogram.length)
histogram = upSizeHisto(histogram, wordLength);

// now you can increment the counter in the histogram for this word's length
histogram[word.length()]++;

}
infile.close();

wordList = trimArr(wordList, wordCount);
System.out.println("After trim, wordList length: " + wordList.length);

for (int i = 0; i < histogram.length; i++)
System.out.println("words of length " + i + ": " + histogram[i]);

}


private static String[] upSizeArr(String[] fullArr)
{
int l = (fullArr.length)*2;
String[] array = new String[l];
for (int i = 0; i < fullArr.length; i++)
{
array[i] = fullArr[i];
}

return array;
}

private static String[] trimArr(String[] oldArr, int count) {
String[] array = new String[count];
for (int i = 0; i < array.length; i++)
{
array[i] = oldArr[i];
}

return array;
}

private static int[] upSizeHisto(int[] oldArr, int newLength) {
int array[] = new int[newLength];
for (int i = 0; i < oldArr.length; i++)
{
array[i] = oldArr[i];
}

return array;
}
}

Add a comment
Know the answer?
Add Answer to:
I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints...

    Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • I have to make a java code that takes jumbled words from an input file called...

    I have to make a java code that takes jumbled words from an input file called jumbles.txt and sorts them in alpabetical order 1. Expect the user must to put a filename on the command line like this: C:\>java Lab4 jumbles.txt 2. Use that args[0] value as the name of the input file and open it with a BufferedReader 3. Load every line/word of that input file (one word per line) into an ArrayList of String 4. Sort that ArrayList...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • In Java Programming chapter 6 Make a class that represents a file. This class will have...

    In Java Programming chapter 6 Make a class that represents a file. This class will have the ability to calculate the number of lines in that file and the ability to search through the file. UML diagram: -filename:String +FileStats(String fname) +getNumMatchingWords(String wordtoFind) : int +getNumLines() : int The getNumMatchingWords method will take a bit of text and determine how many lines contain that text. Make the comparison not care about case. View required output Test Case 1 Files in the...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT