Question

In: Computer Science

[15] Create a program called StreamingWords.java that modifies the StreamingIntegers.java from Task 1. The program should...

  1. [15] Create a program called StreamingWords.java that modifies the StreamingIntegers.java from Task 1. The program should be able to do the following: JAVA

    1. accepts user input. User inputs can be under the following forms:

      1. one or more words, separated by white space.

      2. User can provide multiple inputs (each input is completed after user hits Enter)

      3. To stop inputting data, user will enter END then hit Enter.

    2. reads inputs from users into a queue data structure. Each data element in the queue contains one sentence (from whence user starts typing until they hit Enter)

    3. Once users finish entering input (END has been read), the program should begin remove the queue from the front, printing out the data elements in the queue.  

    4. The data elements should be printed out already sorted based on the number of words each element contains, regardless of the order of arrival or the total length of the characters. Each data element is to be printed on one line, and words within a data element are separated by a single white space.

Solutions

Expert Solution

Program:

import java.util.*;

//Queue class
class Queue
{
   //member variables
   int front, rear;
   String q[];
  
   //constructor
   public Queue()
   {
       front = -1;
       rear = -1;
       q = new String[10];
   }
  
   //method to count number of words
   private int numWords(String str)
   {
       int c = 1;
      
       for(int i=0; i<str.length()-1; i++)
       {
           if(str.charAt(i)==' ' && str.charAt(i+1)!=' ')
               c++;
       }
      
       return c;
   }
  
   //enqueue method
   public void enqueue(String word)
   {
       //when queue is empty
       if(isEmpty())
       {
           rear++;
           q[rear] = word;
           return;
       }
       int i;
       rear++;
       //insert into the queue according to the number of words
       //in such a way that after insert the queue remains sorted
       for(i = rear-1; i>front && numWords(word) < numWords(q[i]); i--)
       {
           q[i+1] = q[i];
       }
      
       q[i+1] = word;
   }
  
   //dequeue method
   public String dequeue()
   {
       front++;
       return q[front];
   }
  
   //isEmpty method
   public boolean isEmpty()
   {
       return front==rear;
   }
}

//StreamingWords class
public class StreamingWords
{
   //main method
   public static void main (String[] args)
   {
       //create instance of Scanner class
       Scanner sc = new Scanner(System.in);
      
       //create instance of Queue class
       Queue q = new Queue();
      
       System.out.println("Enter input: ");
       //read multiple inputs
       while(true)
       {
           //read a line
           String str = sc.nextLine();
          
           //stop inputting data, user will enter END
           if(str.equals("END")) break;
          
           //inputs store into a queue data structure
           q.enqueue(str);
       }
      
       //printing out the data elements in the queue
       while(!q.isEmpty())
       {
           //remove the queue from the front
           String str = q.dequeue();
      
           //printing out the data elements
           System.out.println(str);
       }
      
   }
}

Output:

Enter input:
Good day
Have a nice day
One two three
END

Good day
One two three
Have a nice day


Related Solutions

Creation of Program Application (Development Task 1) This program should create two output files. You may...
Creation of Program Application (Development Task 1) This program should create two output files. You may use .txt files. Create the following files: Deposists.txt Withdrawls.txt 1. The program application should have the following inputs: Amount for Deposits (Only Accept Positive Amounts) Amount for Withdrawals (Only Accept Positive Amounts). The subsequent program will subtract the positive amount. 2. The program application should have the following outputs: Deposists.txt Withdrawals.txt Total of Deposits and Withdrawals back to the console
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should...
Create a program called DualPivotQuickSort.java that implements the QuickSort algorithm using Yaroslavskiy’s algorithm. The program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file...
1.Ginny downloads and modifies an open source software program, then uploads the program with a different...
1.Ginny downloads and modifies an open source software program, then uploads the program with a different name. which type of software license is she most likely to be working under? 2. which of the following is an example of financial management software? 3. image-editing software allows you to? 4. digital audio software can be used to? 5. when you subscribe to software, no license is necessary. true or false.
1.Ginny downloads and modifies an open source software program, then uploads the program with a different...
1.Ginny downloads and modifies an open source software program, then uploads the program with a different name. which type of software license is she most likely to be working under? 2. which of the following is an example of financial management software? 3. image-editing software allows you to? 4. digital audio software can be used to? 5. when you subscribe to software, no license is necessary. true or false.
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...
Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for how many lines of text they wish to enter –Declare and initialize an array of Strings to hold the user’s input –Use a while loop to prompt for and read the Strings (lines of text) from the user at the command line. Typically, this would be a 'for' loop since we know the number of times to execute the loop based upon the number...
Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a...
Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a location of a text file, with (FileNotFound) exception handling: Keep asking for a valid file name and location If an invalid file is provided, display an error message. Once provided, proceed. Opens the text file and finds the following: Minimum value Maximum value The average value of all number in the text Prints a message displaying the values in step No. 2
B. Creation of Program Application (Development Task 1) This program should ask the following questions to...
B. Creation of Program Application (Development Task 1) This program should ask the following questions to determine the number of hotdogs and buns (with minimum number of leftovers) needed for the Annual Hotdog Eating contest: Assumptions are as follows: • Hotdogs come in packages of 10. • Hotdog buns come in packages of 8. 1. The program application should have the following inputs: • How many people will be competing in the contest? • How many hotdogs will each person...
Question 1. Your task is to create a full program for a Restaurant of your choice....
Question 1. Your task is to create a full program for a Restaurant of your choice. You will have the output appear at first saying Welcome to ______ Restaurant where the _____ will be whatever you name your restaurant Today’s choices are 1.__________ 2._________ 3._______ 4.________ NOTE each _____ will say an item and its price for example pizza 2.99 Please choose 1 of the choices. In each case, as the user picks a choice It will calculate the total...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT