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

Revise main to return a count of how many words in the gutenberg.txt file are pa

ID: 644543 • Letter: R

Question

Revise main to return a count of how many words in the gutenberg.txt file are palindromes longer than 6 characters. Use strategies, of course, but do it two ways: one using the AND composite and one using the OR composite.

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

public class MainClass {

   public static void main(String[] args) {
       try {
           printWhen("gutenberg.txt",new StartWithT());
       } catch (IOException e) {
           System.err.println("OOPSie: problems reading the file.");
       }

   }

   public static void printWhen(String filename, CheckStrategy which) throws IOException
   {  
       BufferedReader infile = new BufferedReader(new FileReader(filename)); // a decorated FileReader!
       String buffer = null;
       while((buffer = infile.readLine()) != null) {                           // read in a line of text
           StringTokenizer words = new StringTokenizer(buffer);               // chop it up into words
           while( words.hasMoreTokens() ) {                                   // test each word
               String word = words.nextToken();
               if (which.check(word)) {
                   System.out.println(word);
               }
           }
       }
       infile.close();
   }
}

Explanation / Answer

//The java program that checks palindromes of more than 6 characters
//in a text filw, gutenberg.txt
import java.io.*;
import java.util.StringTokenizer;
public class MainClass
{

   public static void main(String[] args)
   {
      
       //set lenght of the word is 6
       int wordLenght=6;

       try
       {
           //call the method printWhen with two arguments a text file, and class CheckStrategy with word lenght
           int count=printWhen("gutenberg.txt",new CheckStrategy(wordLenght));
           //Then print the count of words which are more than 6 character of palindromes
       System.out.println("6-Letter palindrom words: "+count);
       }
       catch (IOException e)
       {
           System.err.println("OOPSie: problems reading the file.");
       }

   }

   public static int printWhen(String filename, CheckStrategy which) throws IOException
   {
      
       int counter=0;
      
       BufferedReader infile = new BufferedReader(new FileReader(filename));
       // a decorated FileReader!
       String buffer = null;
       while((buffer = infile.readLine()) != null)
       {                           // read in a line of text
           StringTokenizer words = new StringTokenizer(buffer);// chop it up into words
          
           while( words.hasMoreTokens())
           {                                   // test each word
               String word = words.nextToken();
               if (which.check(word))
               {
                   //increment the counter value if the check return true
                   //for words which are more than 6 character of palindrome words
                   counter++;
                   //print the palindrome words
                   System.out.println(word);
               }
           }
       }
      
       infile.close();
       return counter;
   }
}

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


//CheckStrategy.java
//The java class that sets the lenght of the word
//in a constructor . Then the method check that checks if
//the given input word is of size more than 6 and it is
//palindrome.
public class CheckStrategy
{

   //lenght instance variable
   private int length;

  
   //constructor to set the lenght of the word
   public CheckStrategy(int length)
   {
       this.length=length;
   }

   //The metod check that takes the word as input
   //and returens true of the given input word is
   //of size more than 6 characters and it is of palidrome
   //word
   public boolean check(String word)
   {
       boolean isPalindrome=false;
       if(word.length()<length)      
           return false;
       else
       {
          
           String reverse=new String();
           //reverse the string
           for ( int i = length; i >= 0; i-- )
                 reverse = reverse + word.charAt(i);
          
           //check for palindrome
           if(reverse.equalsIgnoreCase(word))
               isPalindrome=true;
          
       }
       return isPalindrome;
   }
}


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

Note :gutenberg.txt file is not available. It is taken a sample text contains paplindromes

of more than 6 character words.

Input text file : gutenberg.txt

anilina
Nauruan
acca
adda

Sample output:

anilina
Nauruan
6-Letter palindrom words: 2

Hope this helps you.

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