Question

Lab Description Sort all words by comparing the length of each word. The word with the smallest length would come first. If yimport static java.lang.System.* public class Word implements Comparable<Word> private String word; public Word String s) pubimport java.io.File; import java.io. IOException; import java.util.Scanner; import java.util.Arrays; import static java.lang.

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

WordRunner.java

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
import static java.lang.System.*;

public class WordRunner
{
   public static void main( String args[] ) throws IOException
   {
       Scanner file = new Scanner(new File("src//words.dat"));

       int size = file.nextInt();
       Word words[]=new Word[size];
       int counter=0;
       file.nextLine();
       for (int i=0; i<size; i++)
       {
           words[counter]=new Word(file.nextLine());
           counter++;
       }
       file.close();
      
       Arrays.sort(words);
      
       for (Word word:words)
       {
           System.out.println(word);
       }

   }
}

Word.java

import static java.lang.System.*;

public class Word implements Comparable<Word>
{
   private String word;

   public Word()
   {
       setWord("");
   }
   public Word( String s )
   {
       setWord(s);
   }

   public void setWord(String s)
   {
       word=s;
   }
  
   public String getWord()
   {
       return word;
   }
  
   public int compareTo( Word rhs )
   {  
       if (word.length()-rhs.getWord().length()==0)
       {
           return word.compareTo(rhs.getWord());
       }
       else
       {
           return word.length()-rhs.getWord().length();
       }
   }

   public String toString()
   {
       return word;
   }
}


words.dat

10
freddy
at
elephant
whoooooodat
alice
tommy
bobby
it
at
about


Problems@ Javadoc DeclarationConsole 3 terminated> WordRunnerava Application] CProgram FilesJavaljrel1.8.0 201\binjavaw.exe (

Add a comment
Know the answer?
Add Answer to:
Lab Description Sort all words by comparing the length of each word. The word with the...
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
  • Lab Description: Sample Data Read in one word at a time from the file and output...

    Lab Description: Sample Data Read in one word at a time from the file and output the word as a square. # of data sets in the file . 6 HELLO CAT DOGHOUSE ONE IT Files Needed:: FancyHordrwo.java FancywordiwoRunner.java fancyword2. dat Sample Output: HELLO E L algorithm help OLLER CAT A. TAC 3 more statements //like the one I gave you DOGHOUSE ESUOHGOD ENO import java.util.Scanner; import static java.lang.System.* public class FancyWordTwo private String[1 mat; public FancyWordTwo (String s) //size...

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

  • Using the diagram below and the starter code, write Document Processor that will read a file...

    Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • Lab Description : Write a program that will sum all of a numbers digits. You must...

    Lab Description : Write a program that will sum all of a numbers digits. You must use % for this lab to access the right most digit of the number. You will use /to chop off the right most digit. Sample Data 234 10000 Files Needed: DigitAdder.java JavaLoopLabRunner.java 9005 84645 8547 123456789 55556468 8525455 8514548 1212121212 Sample Output 14 27 24 45 34 35 15 12 import static java.lang.System.* public class DigitAdder public static int go( int num ) return...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • LAB: Zip code and population (generic types)

    13.5 LAB: Zip code and population (generic types)Define a class StatePair with two generic types (Type1 and Type2), a constructor, mutators, accessors, and a printInfo() method. Three ArrayLists have been pre-filled with StatePair data in main():ArrayList<StatePair> zipCodeState: Contains ZIP code/state abbreviation pairsArrayList<StatePair> abbrevState: Contains state abbreviation/state name pairsArrayList<StatePair> statePopulation: Contains state name/population pairsComplete main() to use an input ZIP code to retrieve the correct state abbreviation from the ArrayList zipCodeState. Then use the state abbreviation to retrieve the state name from the ArrayList abbrevState. Lastly,...

  • This last lab teaches you to think and solve problems in the functional programming framework of...

    This last lab teaches you to think and solve problems in the functional programming framework of the Java 8 computation streams. Therefore in this lab, you are absolutely forbidden to use any conditional statements (either if or switch), loops (either for, while or do-while) or even recursion. All computation must be implemented using only computation streams and their operations! In this lab, we also check out the Java NIO framework for better file operations than those offered in the old...

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

  • Lab Description: Compare two Strings to see if each of the two Strings has the same...

    Lab Description: Compare two Strings to see if each of the two Strings has the same first letter. Useful methods:: charAt) Sample Data: hello howdy one twO three two TCEA UIL State Champions ABC DEF Files Needed: StringFirstLetterCheck.java StringFirstLetterRunner.java Sample Output: hello has the same first letter as howdy one does not have the same first letter as two three has the same first letter as two TCEA does not have the same first letter as UIL State does not...

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