CSU Banner

CS 150, Fall 2017

Programming Assignment - P8

Pseudostring with a Character Array

Due - November 7th, 2017 at 6:00pm

Late - November 8th, 2017 at 8:00am


Objectives of this Assignment

  1. To write a program that imitates some of the methods defined in the String class,
  2. specifically charAt(), indexOf(), lastIndexOf(), substring(), and toUpperCase(),
  3. by utilizing a character array and for loops combined with if statements, and
  4. to practice writing code that outputs different results based on user input.

Instructions

Create a Java program called P8 with a class named P8, which after created should contain a file named P8.java in the src folder. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the main method. Testing for this assignment will not include invalid values since the only error handling required is checking the user's menu choice. For this assignment, you must follow the directions below exactly:

    Part One
  1. First, add four more prompts to the user options displayed. Afterward, the user menu should look like the following:
    0: Exit Program
    1: Determine Character at an Index
    2: Determine Index of a Character
    3: Determine Last Index of a Character
    4: Print a Section of an Array
    5: Print All Characters in Upper Case
    Enter option number:
  2. Following this, update the boolean expression of your while loop that verifies user input to accept option values 0-5, but nothing outside this range.
  3. Next, write a conditional for menu option 2.
  4. Within this conditional, first print the prompt:
    Enter a character to search for:
  5. Then read the value entered and assign it to your associated String variable.
  6. Using charAt() on this String variable, assign your appropriate character variable the same value that was entered by the user. Because the Scanner class has no nextChar(), this is how we will go about assigning the character variable a value, which we will next try to find a match to within the character array.
  7. Next, iterate over the character array that has the characters of the original string.
  8. Write a conditional in the for loop to check if each character of the array is equal to the character the user is searching for.
  9. If found, assign the index value to your associated integer variable.
  10. Additionally, once a matching character is found and you have assigned your integer variable a value inside the for loop's conditional, exit the loop.
  11. Before the conditional for option 2 ends, print the result based on the following formatted example:
    Index of 'a': 36
  12. The code for the conditional related to menu option 3 will be almost identical to menu option 2, except you will not exit the loop once a matching character is found.
  13. In effect, repeat steps 2-10 for menu option 3, excluding step 9.

  14. Part Two
  15. Inside the conditional for menu option 4, first print the following prompt:
    Enter a starting index value:
  16. Read an integer value into your associated variable.
  17. Next, print the following associated prompt:
    Enter an ending index value:
  18. Read an integer value into your associated variable.
  19. Iterate over the section of the character array using the previous two integers, and build the appropriate String by concatenating one character at a time to your associated String variable. Just like the substring() method, do not include the character at the ending index.
  20. After the for loop, print a blank line.
  21. Lastly, print the resulting String based on the following formatted example:
    Array Section: ver the
  22. Begin a new conditional for menu option 5.
  23. First, iterate over the character array and by using the Character wrapper class, change every element of the array to upper case.
  24. Next, print the following prompt:
    Character Array in Uppercase:
  25. In order to print the resulting upper case characters, iterate over the array with the upper case characters and print each character to the console one at a time as the loop iterates.
  26. After printing the result, print two blank lines to the console.
  27. Finally, close your Scanner.

Sample Output (One Run With Multiple Options Selected)

Welcome to the CS150 Pseudostring Program.

0: Exit Program
1: Determine Character at an Index
2: Determine Index of a Character
3: Determine Last Index of a Character
4: Print a Section of an Array
5: Print All Characters in Upper Case
Enter option number: 7

That is not a valid menu option.
Enter option number: 15

That is not a valid menu option.
Enter option number: 1

Enter an index value: 27
Character at Index 27: v

0: Exit Program
1: Determine Character at an Index
2: Determine Index of a Character
3: Determine Last Index of a Character
4: Print a Section of an Array
5: Print All Characters in Upper Case
Enter option number: 2

Enter a character to search for: p
Index of 'p': 23

0: Exit Program
1: Determine Character at an Index
2: Determine Index of a Character
3: Determine Last Index of a Character
4: Print a Section of an Array
5: Print All Characters in Upper Case
Enter option number: 3

Enter a character to search for: e
Index of 'e': 33

0: Exit Program
1: Determine Character at an Index
2: Determine Index of a Character
3: Determine Last Index of a Character
4: Print a Section of an Array
5: Print All Characters in Upper Case
Enter option number: 4

Enter a starting index value: 6
Enter an ending index value: 11

Array Section: ick b

0: Exit Program
1: Determine Character at an Index
2: Determine Index of a Character
3: Determine Last Index of a Character
4: Print a Section of an Array
5: Print All Characters in Upper Case
Enter option number: 5

Character Array in Uppercase: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

0: Exit Program
1: Determine Character at an Index
2: Determine Index of a Character
3: Determine Last Index of a Character
4: Print a Section of an Array
5: Print All Characters in Upper Case
Enter option number: 0

Thank you for using the CS150 Pseudostring Program. Goodbye.

Specifications

Grading Criteria


Submit P8.java to Checkin.


CS Banner
CS Building