Question

In: Computer Science

***USING JAVA Scenario: You will be writing a program that will allow a user to find...

***USING JAVA Scenario: You will be writing a program that will allow a user to find and replace misspelled words anywhere in the phrase, for as many words as the user wishes. Once done (see example runs below), the program will print the final phrase and will show the Word Count, Character Count, the Longest Word and the Shortest Word.

Requirements:

Do not use Arrays for this assignment.

Do not use any String class methods (.phrase(), replace methods, regex methods) that parse the phrase for you. You will be writing the search and replace algorithm yourself

The blank spaces between words are not included when replacing a word. Your code should ignore them as it matches and replaces misspelled words.

For now, leave off any punctuation at the end of the phrase (or ignore it).

Make sure your code is neat and well structured / well-tabbed (this will help with debugging!), appropriately commented, with a comment header at the top.

This is a fantastic cumulative learning assignment. Write out pseudocode for this assignment and plan your implementation. This will help immensely!

Each occurrence of a misspelled word should be replaced, and I should be able to replace many different words through the use of the program (see below).

By “replaced” I mean that the final string that contains your phrase should contain the version with all misspellings desired by the user corrected.

If there are “longest” and “shortest” words that tie for character length, the most recent occurrence of either should “win.” For example: “Its either of or to but not both!” the shortest word would be “to”, the most recent shortest word towards the end of the phrase.

Due: Tuesday, September 24th by 11:59 p.m. on Canvas. (See Example Output on next page )

Short Run:

Please enter phrase to adjust: We aer teh Dukes of JMU

Enter word to be replaced: aer

Enter word to replace with: are

Current Phrase Version: "We are teh Dukes of JMU "

Continue Replacing Words? (y or n):y

Enter word to be replaced: teh

Enter word to replace with: the

Current Phrase Version: "We are the Dukes of JMU "

Continue Replacing Words? (y or n):n

Final Phrase:

============================

We are the Dukes of JMU

# of Words: 12

# of Characters: 24

Longest Word: "Dukes"

Shortest Word: "of"

============================

Longer Run:

Please enter phrase to adjust: When in teh course of humen events it becomes necessary for one poeple to dissolve the political bands that have connaected them with another

Enter word to be replaced:teh

Enter word to replace with:the

Current Phrase Version: "When in the course of humen events it becomes necessary for one poeple to dissolve the political bands that have connaected them with another "

Continue Replacing Words? (y or n):y

Enter word to be replaced:poeple

Enter word to replace with:people

Current Phrase Version: "When in the course of humen events it becomes necessary for one people to dissolve the political bands that have connaected them with another "

Continue Replacing Words? (y or n):y

Enter word to be replaced:connaected

Enter word to replace with:connected

Current Phrase Version: "When in the course of humen events it becomes necessary for one people to dissolve the political bands that have connected them with another "

Continue Replacing Words? (y or n):y

Enter word to be replaced:humen

Enter word to replace with:Human

Current Phrase Version: "When in the course of Human events it becomes necessary for one people to dissolve the political bands that have connected them with another "

Continue Replacing Words? (y or n):n

Final Phrase:

============================

When in the course of Human events it becomes necessary for one people to dissolve the political bands that have connected them with another

# of Words: 96

# of Characters: 141

Longest Word: "connected"

Shortest Word: "to"

============================

Solutions

Expert Solution

Program

import java.util.*;

class ReplaceWord
{
   static String sentence;
//method to replace oldword with newword
   static void replace(String oldword, String newword)
   {
       int j;
      
       for(int i=0; i<sentence.length() - oldword.length(); i++)
       {
           if(oldword.charAt(0)==sentence.charAt(i))
           {
               for(j=0; j<oldword.length(); j++)
               {
                   if(oldword.charAt(j)!=sentence.charAt(i+j)) break;
               }
              
               if(j==oldword.length())
                   sentence = sentence.substring(0, i) + newword +
                       sentence.substring(i+newword.length());
           }
       }
      
   }
//main method
   public static void main (String[] args)
   {
       Scanner sc = new Scanner(System.in);
      
       char ch;
      
       System.out.print("Please enter phrase to adjust: ");
       sentence = sc.nextLine();
      
       do{
      
           System.out.print("Enter word to be replaced: ");
           String oldword = sc.next();
          
           System.out.print("Enter word to replace with: ");
           String newword = sc.next();
          
           replace(oldword, newword);
          
           System.out.println("Current Phrase Version: " + sentence);
          
           System.out.print("Continue Replacing Words? (y or n): ");
           ch = sc.next().charAt(0);
          
   }while(ch=='y' ||ch=='Y');
  
   int wc = 1;
   int t = 0, k = 0;
  
   String longest = "", word;
  
   for(k=0; k<sentence.length(); k++)
   {
       if(sentence.charAt(k)==' ') break;
   }
  
   String shortest = sentence.substring(0, k);
  
  
   for(int i=0; i<sentence.length(); i++)
   {
       if(sentence.charAt(i)==' ' && i<=sentence.length() && sentence.charAt(i+1)!=' ')
       {
           wc++;
          
           word = sentence.substring(t, i);
           if(shortest.length() >= word.length())
               shortest = word;
              
           if(longest.length() <= word.length())
               longest = word;
          
           System.out.println(word);
          
           t = i + 1;
       }
   }
          
   System.out.println("Final Phrase:");
   System.out.println("============================");
   System.out.println(sentence);
  
   System.out.println("# of Words: " + wc);
   System.out.println("# of Characters: " + sentence.length());
   System.out.println("Longest Word: " + longest);
   System.out.println("Shortest Word: " + shortest);
  
}
}

Output:

Please enter phrase to adjust: We aer teh Dukes of JMU
Enter word to be replaced: aer
Enter word to replace with: are
Current Phrase Version: We are teh Dukes of JMU
Continue Replacing Words? (y or n): y
Enter word to be replaced: teh
Enter word to replace with: the
Current Phrase Version: We are the Dukes of JMU
Continue Replacing Words? (y or n): n
We
are
the
Dukes
of
Final Phrase:
============================
We are the Dukes of JMU
# of Words: 6
# of Characters: 24
Longest Word: Dukes
Shortest Word: of

Process completed.


Related Solutions

C++ code Inventory Item Stack You are writing an Inventory program that will allow the user...
C++ code Inventory Item Stack You are writing an Inventory program that will allow the user to enter a part into the inventory, take a part from the inventory, or quit. You are provided with the InvItem class (InvItem.h) and a partial Driver.cpp. You will be creating a DynamicStack class that should implement a Stack data structure. The DynamicClass should be implemented as a template class to allow any data type be added/removed from the stack. You will submit three...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
Using Tkinter for GUI, A Python program that will allow the user to perform (i) generate...
Using Tkinter for GUI, A Python program that will allow the user to perform (i) generate RSA keys, (ii) encrypt a given message and (iii) decrypt the message without using any cryptographic library. Your program will generate the values of p unless the user provides them manually. Then the program will generate the keys (private and public). Then the program will allow the user to enter his/her message to be encrypted. Finally, the program will perform the decryption operation as...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
Using Java, write a program named MyAngles that will prompt the user for the measure of...
Using Java, write a program named MyAngles that will prompt the user for the measure of the three sides of a triangle and then reports the measurement of each interior angle of the triangle and the area of the triangle.
You are writing a billing program that adds up the items a user enters. Your program...
You are writing a billing program that adds up the items a user enters. Your program should use a loop and prompt the user for the item number for each item in inventory to add to the bill. (You can ask the user for the number of items you will enter and use a counter controlled loop or a sentinel controlled loop that has a stop number. Either way is fine.) Here is the table of items in inventory: Item...
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Java Programming Language Scenario: write a program that will prompt a user for 10 legendary people/characters....
Java Programming Language Scenario: write a program that will prompt a user for 10 legendary people/characters. After the entries are complete the program will display all 10 characters information. Store these characters in an array. Requirements: Create a user-defined class with the fields below: First Last Nickname Role Origin Create a main-source that will use the user-defined class and do the prompting. Create get/set methods in your user-defined class and methods in the main-source. Use an array to store the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT