Question

Write a method that has the following header: public static void printShuffled(String filename) The method reads...

Write a method that has the following header:

public static void printShuffled(String filename)

The method reads a text file, filename, sentence by sentence into an array list, shuffles the sentences, and then prints out the shuffled contents. Assume sentences end with one of these characters: ".", ":", "!" and "?". Your method should create an array list for storing the sentences. The array list should be created with approximately the correct number of sentences, instead of being gradually expanded as the file is read in.

Write a test program that reads the attached story.txt file and prints its contents using your method.

Hints:

 To read sentences instead of lines or words, use the method useDelimiter("[.:!?]")of the Scanner class.

 To determine the approximate number of sentences, divide the file length, representing the size of the file, by an assumed average size of a sentence (let’s say 50 characters).

story.txt

A farmer worked in a vineyard and became rich. He wanted his sons to be just like him. On his deathbed, the farmer told his sons that there was a great treasure buried in the vineyard. After the farmer died, the sons went to the vineyard and dug up the soil. They could not find a buried treasure! At harvest time, the vineyard produced the best grapes ever. Now, the sons understood the meaning of the treasure.

Sample Output

Now, the sons understood the meaning of the treasure. On his deathbed, the farmer told his sons that there was a great treasure buried in the vineyard. He wanted his sons to be just like him. They could not find a buried treasure. At harvest time, the vineyard produced the best grapes ever. After the farmer died, the sons went to the vineyard and dug up the soil. The farmer worked in a vineyard and became rich.

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

Java Program:

/* Java Program that reads the contents of a file and shuffles the sentences */

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

class ShuffleSentences
{
   //Main method
   public static void main(String args[]) throws FileNotFoundException
   {
       //Calling method along with file name
       printShuffled("story.txt");
   }
  
   //Method definition
   public static void printShuffled(String filename)throws FileNotFoundException
   {
       //File object
       File file = new File(filename);
      
       //Scanner class object
       Scanner fileReader = new Scanner(file);
      
       //Using delimiters
       fileReader.useDelimiter("[.:!?]");
      
       //For initial capacity
       long approxNumberOfSentences;
       long avgSentenceSize = 50;
      
       //Calculating approximate number of sentences
       approxNumberOfSentences = file.length() / avgSentenceSize;
      
       //Creating an array list
       ArrayList<String> sentences = new ArrayList<String>((int)approxNumberOfSentences);
      
       //Reading sentences from file
       while(fileReader.hasNext())
       {
           //Storing in array list
           sentences.add(fileReader.next());
       }
      
       //Shuffling array list
       Collections.shuffle(sentences);  
          
       System.out.println("\n Contents of array list after shuffling: \n\n");
      
       //Printing contents of array list after shuffling
       for(int i=0; i<sentences.size(); i++)
           System.out.print(sentences.get(i) + ". ");
          
       System.out.println("\n\n");
   }
}

______________________________________________________________________________________________

Sample Run:

C:Windows system32icmd.exe D:\Java> javac ShuffleSentences.java D:\Java>java ShuffleSentences Contents of array list after sh

Add a comment
Know the answer?
Add Answer to:
Write a method that has the following header: public static void printShuffled(String filename) The method reads...
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
  • Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( );...

    Write a complete C++ program that defines the following class: Class Salesperson { public: Salesperson( ); // constructor ~Salesperson( ); // destructor Salesperson(double q1, double q2, double q3, double q4); // constructor void getsales( ); // Function to get 4 sales figures from user void setsales( int quarter, double sales); // Function to set 1 of 4 quarterly sales // figure. This function will be called // from function getsales ( ) every time a // user enters a sales...

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