Question

In: Computer Science

Problem: Implement a class named StringParser, along with specified methods, that can read words from a...

Problem: Implement a class named StringParser, along with specified methods, that can read words from a text file, parse those words (such as finding palindromes), and write those words to a new file More specifically: 1. Write the StringParser class so that it has the following methods: a) public static void main(String[] args) -- The main driver of the program. Prompts the user for an input file, an output file, and a choice for what kinds of words to output. b) public static void rawOutput(File in, File out)-- Parses an input file, writing each word from the input on a separate line in the output file. c) public static void palindromeOutput(File in,File out) -- Parses an input file, writing only the palindromes to the output file, in alphabetical order, one line at a time, without repetition. Palindromes are words that read the same forward and backward, ignoring digits and punctuation. d) public static void hundredDollarWordOutput(File in, File out) -- Parses an input file, writing only the $100 words to the output file, in alphabetical order, one line at a time, in uppercase, without repetition. $100 words are found by assigning $1 to A, $2 to B, and so on. For example ELEPHANTS is a $100 word because: E + L + E + P + H + A + N + T + S is 5 + 12 + 5 + 16 + 8 + 1 + 14 + 20 + 19 = 100 e) public static boolean isPalindrome(String word) -- Determines if a word is a palindrome (reads the same forward and backward). The method is case-sensitive, so it will say that dad and DAD and d-a-d are palindromes, but Dad and d-ad are not palindromes. f) public static String cleanup(String word)-- Takes a string and removes all non-letters, returning an all uppercase version. For example, this input: "A man, a plan, a canal. Panama." will produce this output: "AMANAPLANACANALPANAMA" g) public static int wordValue(String word)-- Returns the monetary value of a word, found by assigning the value $1 to A, $2 B, and so on, up to $26 for Z. The method will ignore differences in case, so both A and a are each worth $1. It will also ignore any non-letters in the input. 2. Create your own input test file, and use it with your code to generate three output files. 3. Upload all five files (Java source code, input file, three output files).

Solutions

Expert Solution

Complete Program:

File: StringParser.java




Input file: indata.txt

Sample Run 1:

Sample Run 2:

Output file: rawWords.txt

Sample Run 3:

Output file: palindromeWords.txt

Sample Run 4:

Output file: hundredDollarWords.txt

CODE TO COPY:

File: StringParser.java

// StringParser class implementation
import java.io.*;
import java.util.*;
public class StringParser
{
   // start main method
   public static void main(String args[])
   {
       Scanner keyboard = new Scanner(System.in);
       String inputFileName;
       String outputFileName;
       File in = null;
       File out = null;
       int choice;

       System.out.print("Enter input filename: ");
       inputFileName = keyboard.next();
       in = new File(inputFileName);

       if (in.exists())
       {
           System.out.println("Found. What do you want to output?");
           System.out.println("1. Raw word list");
           System.out.println("2. Palindromes");
           System.out.println("3. $100 words");
           System.out.print("Choose: ");
           choice = keyboard.nextInt();
          
           if(choice >= 1 && choice <= 3)
           {
               System.out.print("Enter output filename: ");
               outputFileName = keyboard.next();
               out = new File(outputFileName);
           }

           switch (choice)
           {
           case 1:
               rawOutput(in, out);
               break;

           case 2:
               palindromeOutput(in, out);
               break;

           case 3:
               hundredDollarWordOutput(in, out);
               break;

           default:
               System.out.println("Invalid choice!");
           }
       }
       else
       {
           System.out.println("File does not exist. Goodbye.");
       }
      
       keyboard.close();
   } // end of main method

   // rawOutput method implementation
   public static void rawOutput(File in, File out)
   {
       try
       {
           Scanner infile = new Scanner(in);
           PrintWriter outfile = new PrintWriter(out);

           while (infile.hasNext())
           {
               String word = infile.next();
               outfile.println(word);
           }

           infile.close();
           outfile.close();
          
           System.out.println("Finished printing raw word list.");
       }
       catch (FileNotFoundException e)
       {
           System.out.println("File does not exist. Goodbye.");
       }
   } // end of rawOutput method

   // palindromeOutput method implementation
   public static void palindromeOutput(File in, File out)
   {
       try
       {          
           Scanner infile = new Scanner(in);
           PrintWriter outfile = new PrintWriter(out);
           ArrayList<String> list = new ArrayList<String>();
          
           while (infile.hasNext())
           {
               String word = infile.next();
               word = cleanup(word);
               if (isPalindrome(word) && !list.contains(word))
               {
                   list.add(word);
               }
           }
          
           Collections.sort(list);          
           for(String word : list)
           {
               outfile.println(word);
           }

           infile.close();
           outfile.close();
          
           System.out.println("Finished printing palindromes.");
       }
       catch (FileNotFoundException e)
       {
           System.out.println("File does not exist. Goodbye.");
       }
   } // end of palindromeOutput method

   // hundredDollarWordOutput method implementation
   public static void hundredDollarWordOutput(File in, File out)
   {
       try
       {
           Scanner infile = new Scanner(in);
           PrintWriter outfile = new PrintWriter(out);
           ArrayList<String> list = new ArrayList<String>();
          
           while (infile.hasNext())
           {
               String word = infile.next();
               word = cleanup(word);
               if (wordValue(word) == 100 && !list.contains(word))
               {
                   list.add(word);
               }
           }
          
           Collections.sort(list);          
           for(String word : list)
           {
               outfile.println(word);
           }

           infile.close();
           outfile.close();
          
           System.out.println("Finished printing $100 words.");
       }
       catch (FileNotFoundException e)
       {
           System.out.println("File does not exist. Goodbye.");
       }
   } // end of hundredDollarWordOutput method

   // isPalindrome method implementation
   public static boolean isPalindrome(String word)
   {      
       int i = 0;
       int j = word.length() - 1;
       while (i < j)
       {
           if (word.charAt(i) != word.charAt(j))
           {
               return false;
           }

           i++;
           j--;
       }

       return true;
   } // end of isPalindrome method

   // cleanup method implementation
   public static String cleanup(String word)
   {
       String result = "";

       for (char ch : word.toCharArray())
       {
           if (Character.isLetter(ch))
           {
               result += ch;
           }
       }
      
       result = result.toUpperCase();

       return result;
   } // end of cleanup method

   // cleanup method implementation
   public static int wordValue(String word)
   {
       int value = 0;
      
       for(int i = 0; i < word.length(); i++)
       {
           value += ((int)word.charAt(i) - 64);
       }
      
       return value;
   } // end of cleanup method
} // end of StringParser class


Related Solutions

In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose:...
Description: Create a class in Python 3 named Animal that has specified attributes and methods. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Animal that has the following attributes and methods and is saved in the file Animal.py. Attributes __animal_type is a hidden attribute used to indicate the animal’s type. For example: gecko, walrus, tiger, etc. __name is a...
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
Implement a VotingMachine class that can be used for a simple election. Include the methods to...
Implement a VotingMachine class that can be used for a simple election. Include the methods to voteForDemocrat and voteForRepublican. Add a method to clear out all votes. Additionally, add a method to print the results. Write the driver code to simulate the voting. in java
Modify the HighArray class to implement the method specified below, which reads integer data from a...
Modify the HighArray class to implement the method specified below, which reads integer data from a file to fill the array. The first integer read from the file should indicate the number of integers contained in the file which can be read into the array. /** * Read integers into array from named file. First element in * file is a count of number of integers contained in the file. * * @param fileName   name of file containing integer data...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the end of...
in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT