Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

SPECIFICATIONS: Class Name: StringLab02.java These if-else statements should be contained in a separate method that is called by the main method. If/Else-Statements: Use if and/or

SPECIFICATIONS:

  • Class Name: StringLab02.java
  • These if-else statements should be contained in a separate method that is called by the main method.
  • If/Else-Statements: Use if and/or if/else-statements in the program.
  • Run Once: Note that the sample output shows many different outcomes bymany different program-executions. Only one menu item (with appropriate user-defined input parameters) will be performed per program-execution.
  • Default: Include a default message ("Not a valid number!") if the user chooses an invalid menu-option.

NOTES:

  • Caution: Part 1: All of the sample output shown is from using the input String "this is a test", so the sample output will show 14 and 13 as the length and maximum index of the String, respectively. However, those numbers will change based on other input Strings used by Mimir to automatically test the program. If the String had been "Java is Life", then the numbers for length and maximum index would be 12 and 11, respectively. Therefore, do not hard-code results into a menu-option.
  • Scanner: Programmers will likely experience a problem using the Scanner object after reading in alternating primitive data (such as an int) and a String object. Refer to 10:50 - 19:05 in the video that can be referenced from the "Chapter 4.4 - Scanner" webpage in Mimir's "Lesson" tab, the Scanner section of the Relevant API (Pro-Tip), and/or Section 4.4 in the textbook (Quirky Behavior) for a remedy. Judiciously call the nextLine() method of the Scanner object to remove the new line character (' '), when appropriate.
  • Caution: Part 2: The nextLine() method of a Scanner object is used to "clear the buffer" of the carriage return that may have been left by a preceding nextInt() or nextDouble() method of the Scanner object. This is sometimes a necessary practice. However, do not call the nextLine() method of the Scanner objectunless the program will subsequently (absolutely for sure) call another method of the Scanner object to read from the input buffer in Mimir!
    • Note: In a real-IDE, calling the nextLine() method of the Scanner object will not cause a problem, regardless of the existence of data in the input buffer (provided by the user via the keyboard). However, Mimir is using a simulated-IDE that will experience a run-time error when the nextLine() method of the Scanner object is called without data in the input buffer.
  • For example, if the last method call to a Scanner object for a particular run (execution) of a program is a nextLine(), then a run-time error will occur!
  • Fabricate Tests: Since many of the test cases are hidden, the impetus on the student is to thorough vet code! Do not simply rely on the visible test cases (especially with the "equals" and "compare-to" operations.
  • Test Often: Instead of writing the whole program (and debugging a gazillion syntax/logic mistakes), submit the program for testing in Mimir after the code forone menu-option is coded.
  • String:Refer to the video in the "Chapter 4.3 - The String Class" webpage in Mimir's "Lesson" tab, the relevant API, the Java API for String, and/or Section 4.3 in the textbook for more information regarding the String class (and in particular, the methods of the String class)
  • If/Else: Recall that an if-else structure will always execute exactly one result.
  • Sample Output: The plethora of sample-output is provided in order to demonstrate several possible outcomes
STRING ACTION SAMPLE CODE OUTPUT
New line System.out.print("a b"); a b
\" Quotation mark System.out.print("a and \"b\"."); a and "b".

SAMPLE OUTPUT:

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

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 1

The length of the phrase is 14

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 2

Enter a number between 0 and 13: 8

The character at index 8 is 'a' ==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 3

Enter a phrase that will be compared with "this is a test": this is a test

The two phrases DO have the same sequence of characters.

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 3

Enter a phrase that will be compared with "this is a test": this is NOT a test

The two phrases DO NOT have the same sequence of characters.

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 4

Enter a phrase that will be compared with "this is a test": this is a test

The two phrases are equivalent.

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 4

Enter a phrase that will be compared with "this is a test": abc

Alphabetically, "this is a test" comes after "abc"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 4

Enter a phrase that will be compared with "this is a test": xyz

Alphabetically, "this is a test" comes before "xyz"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 5

Enter a String to search "this is a test" for: hi

The first occurrence of "hi" is at index 1

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 5

Enter a String to search "this is a test" for: zzz

"zzz" is not in the phrase "this is a test"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 6

Choose one of the methods:

1. Compose a substring from a selected index until the end,2. Compose a substring from a selected index until another selected index

Enter selection: 1

Which index (between 0 and 13) would you like to start with? 10

The new phrase is: "test"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 6

Choose one of the methods:

1. Compose a substring from a selected index until the end,2. Compose a substring from a selected index until another selected index

Enter selection: 2

Which index (between 0 and 13) would you like to start with? 2

Which index (between 2 and 13) would you like to end with? 7

The new phrase is: "is is"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 7

All lowercase looks like this: "this is a test"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 8

All uppercase looks like this: "THIS IS A TEST"

==========

Please enter a phrase: this is a test

  1. Find the length of the string
  2. Perform charAt
  3. Perform equals
  4. Perform compareTo
  5. Perform indexOf
  6. Perform substring
  7. Perform toLowerCase
  8. Perform toUpperCase

Please make a selection: 42

Not a valid number!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students explore these related Programming questions