Question

Java question: Create a class called ​Composition​. Read in all of the words in a file...

Java question: Create a class called ​Composition​. Read in all of the words in a file called dictionary.txt​. Each word will be separated by whitespace (i.e. a space, a tab or a newline character). You must read in all words and determine if a word can be created by concatenating two other words. For example, the word racecar can be created by concatenating race and car. All such possibilities must be output to a file in this format: racecar: race car
In other words, you output the original word followed by a colon, followed by the first word, followed by a space and finally the second word. Each answer is on 1 line. All of these possibilities are output to a file called​ composed.txt​.All of the original words must be in alphabetical order. In other words,​racecar: race car shows up in the output after ​another:an other​.

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

The source code is given below:

/**
* Composition.java
*/
import java.io.*;
class Composition
{   
public static String w1="";// to store word1 of composed word
public static String w2="";// to store word2 of composed word
/* method to check if the word w is a composed word in word list words[]*/
public static boolean isComposed(String w,String words[])
{ int len=words.length;// length of word list
   for(int i=0;i<len-1;i++)
       {
           for(int j=i+1;j<len;j++)
           {
               if((words[i]+words[j]).compareTo(w)==0) // if composed word is w
               { w1=words[i]; // word1
               w2=words[j]; // word2  
               return true;  
               }
           }
       }
       return false; //in case no composed word is w
}
/* sorts word list according to bubble sort algorithm*/
   public static void sort(String words[])
   {
       int len=words.length;
       for(int i=0;i<len-1;i++)
       {
           for(int j=0;j<len-1-i;j++)
           {
               if(words[j].compareTo(words[j+1])>0)// if words[j] is alphabetically found after words[j+1]
               { /* swapping between words[j] and words[j+1] */
                   String temp=words[j];
                   words[j]=words[j+1];
                   words[j+1]=temp;
               }
           }
       }
   }
/* main() throws IOException because it uses file i/o operation */
   public static void main(String args[]) throws IOException
   {
       FileReader fr=new FileReader("dictionary.txt"); // input file dictionary.txt
       FileWriter fw=new FileWriter("composed.txt"); // output file composed.txt
       BufferedReader br=new BufferedReader(fr); // BufferedReader object
       BufferedWriter bw=new BufferedWriter(fw); // BufferedWriter object
       String word="",w;
       while((w=br.readLine())!=null)// reads linewise from input file
       { word=word+w+"\n"; //concatenates words with \n as separator
       }
       br.close();// closes input file
       String words[]=word.split("\n"); // splits word list at \n to create array of strings
sort(words); // sorts array of strings words
  

for(int i=0;i<words.length;i++)// loops till all the words finished
       { if(isComposed(words[i],words)) // checks a word for composed word or not
       bw.write(words[i]+":"+w1+" "+w2+"\n"); //writes the composed word to output file composed.txt
       }
   bw.close(); //closes output file
   }
}

/***************************/

The source code screen shot is given below:

The input file dictionary.txt contains following data:

some
rattle
ball
lifetime
back
ward
elsewhere
upside
grandmother
times
cannot
baseball
fireworks
passport
together
become
life
time
became
sunflower
crosswalk
pass
snake
port
basketball
sweetmeat
superstructure
moonlight
football
railroad
rattlesnake
anybody
weatherman
throwback
skateboard
meantime
foot
earthquake
everything
herein
can
not
sometimes
also
backward
schoolhouse
butterflies

The output file composed.txt contains the following data:

backward:back ward
cannot:can not
lifetime:life time
passport:pass port
rattlesnake:rattle snake
sometimes:some times

Output file screen shot is given below:

Add a comment
Know the answer?
Add Answer to:
Java question: Create a class called ​Composition​. Read in all of the words in a file...
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
  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Program In Assembly For this part, your MAL program must be in a file named p5b.mal....

    Program In Assembly For this part, your MAL program must be in a file named p5b.mal. It must have at least one function in addition to the main program. For the purposes of Part (b), you may assume the following 1. Any line of text typed by a user has at most 80 characters including the newline character. 2. A whitespace character refers to a space, a tab or the new line character. 3. A word is any sequence of...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Today you are to write a Java program that will prompt for and read 2 words...

    Today you are to write a Java program that will prompt for and read 2 words of equal length entered by the user, and create a new word which contains the last letter of the 1st word, the last letter of the 2nd word, followed by the second to last character of the 1st word, followed by the second to last character of the 2nd word and so on. Be sure to use the same format and wording as in...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • 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...

  • Write a program that loads a file called "sample.txt" in read mode, reads its content, and...

    Write a program that loads a file called "sample.txt" in read mode, reads its content, and closes the file. Use exception handling to catch any errors. 2. Compute the letter and punctuation distribution in the file. That is, output the number of a’s, the number of b’s, etc. and the number of commas, dashes, and periods. Ignore case when computing this, so ‘A’ and ‘a’ are the same letter. Use lists. 3.   Compute the number of words in the file....

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • 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:...

  • 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...

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