need help with lab5

0 comments

Lab#5 (Due: 16th July (Friday), 2021)

Today’s lab will test your knowledge of classes, methods, using the API, and solving problems.

You will create two classes. One class will be the driver that has your main method. The main method will take the user input of a sentence and call each method in the second class to solve the problems presented. Use a do/while loop to show the menu, take user input, call and output the results of each step.

Your second class will have several methods using condition statements (if/switch), and loops (for, while or do/while) to solve the problems.

The problem:

You are to write a program that will complete the following tasks:

  • Start: Prompt the user to give you a sentence (String) of any kind. Send the String to the constructor of the second class.
    • You will need an instance of the Scanner class to take the String from the user. Use the nextLine() method.
  • After the input is set, you have the String from the user, you will give the user a menu of choices that will manipulate the String, 5 menu items needed:
    • Option1: Count the number of occurrences of a single character in the String. You will take user input for the character you will search for.
      • So if the String is “fellow fall” and the user chooses l to search the method will return 4.
      • “why the white whale wallows” and user asks for w the method will return 5.
    • Option2: Count the words in the String, ignore white space “This is a string” and “This is a String” would both count 4 words. The method will return 4.
    • Option3: Return the String as a String without spaces.
      • If the string is “why the white whale wallows” the method returns a String “whythewhitewhalewallows”
    • Option4: Change the original input String (just reset the String you took at the startup to a new String given by the user).
    • Option5: Exit.
  • According to whatever choice the user request you will use if/switch statement to call the method that handles that chore.
  • The program will continue to run until the user chooses the exit choice. Use a do/while loop for this purpose.
  • Use the Scanner class to take user input.

Hints:

You can use charAt() to count a specific character in the given sentence, and split() function to count the number of words in the given sentence.

The steps you should take to make it easier:

Go step by step.

  1. First get the user input working.
  2. Then get the menu to print out. You can use numbers to represent each menu option.
  3. Then get the if/switch statement set up.
  4. Then get exit to work.
  5. Then get the resetting the String to work.
  6. Then work on counting letters or words.
  7. Wrap up your code (from step2 to step7) inside a loop. The loop should terminate when the user input exit option.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}