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

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...
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.
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...
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
*****Using Java Write a program that finds the standard deviation while also using a graphical user...
*****Using Java Write a program that finds the standard deviation while also using a graphical user interface.
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT