CSU Banner

CS 150, Fall 2017

Lab 10 - More Practice with Arrays, For Loops, a User Menu, and the Start of P8

Monday - October 30, 2017


Objectives of this Lab

  1. Practice writing more for loops,
  2. practice using for loops to manipulate array contents,
  3. begin to implement code for options from a user menu, and
  4. start the code necessary to complete P8.

Getting Started

Create a new Java Project named P8, and make a class named P8. Once complete, P8 will have a similar look to the Simple ATM program you wrote for P5. This program is based on several of the String class methods you have used previously in this class. Except this time, you will not be able to explicitly call those methods. Instead you will need to manually implement six String methods using for loops and a character array. Today's lab asks you to implement two of the six menu options - exiting the program, and determining a character at a particular index, which imitates charAt() of the String class.

Review of a While Loop

Consider the following while loop:
int j = 0;
while (j <= 6) {
    System.out.println(++j);
} // end while loop

For review from lecture, the first part of a while loop is the reserved word while and its associated boolean expression. If the expression evaluates to true, the statements inside the while loop will be executed. Once all statements have been executed, a program will reevaluate the boolean expression again to determine if the condition is still true. If the expression evaluates to false, a program will flow to the closing brace of a while loop and begin to execute additional program statements. Remember it is always good practice to comment the closing braces in your program to help find bugs when they occur.

Note of caution: When working with loops it is essential that you make sure to terminate your program before rerunning if you encounter a problem in the execution of your code. If your code has an infinite loop and you do not terminate before running the program again, you run the risk of slowing the performance of your computer down significantly.

Today's Assignment

    Part One
  1. Create a new Java Project in Eclipse called P8. Make a class also called P8 and check the box to have Eclipse add the main method for you.
  2. It is recommended you read through the rest of the instructions first before beginning to write any code. Once you understand what you are expected to do in this assignment, you can better organize your thoughts and make a more efficient plan of action. Additionally, there is not a description accompanying each of the variables below; therefore reading the instructions first will also help you name your variables in such a way so you can easily understand what each one is for as you program. You may also need to read the specifications for P8 as well in order to do this.
  3. Declare and initialize a Scanner object to read from the keyboard.
  4. Declare and initialize a String to The quick brown fox jumps over the lazy dog.
  5. Declare and initialize two additional String variables to the empty string.
  6. Declare and initialize two character arrays both with a size of 50.
  7. Declare a character variable.
  8. Declare and initialize five integer variables to -1.
  9. Next, write a for loop that assigns each character of the given sentence above to sequential elements of one of your character arrays starting at index 0 of the character array. You will not assign a character to every element, so do not worry about several empty elements at the end of the character array. Additionally, this is one of the only two places in P8 where you are allowed to use a String class method. For this step, you may use length() and charAt() to help assign values to the character array.
  10. Following this loop, print the welcome message to the console:
    Welcome to the CS150 Pseudostring Program.

  11. Part Two
  12. Create a do-while loop that continues to execute as long as the user's selected menu option is not zero. All of the code for this assignment, except closing your Scanner, should reside in the do-while loop.
  13. Inside the do-while loop, first print the following:
    0: Exit Program
    1: Determine Character at an Index
    Enter option number:

    Make sure there is a space at the end of the last prompt.
  14. After the three prompts, read the user's menu choice into your associated variable.
  15. Next, write an inner while loop that verifies the menu choice entered is 0 or 1. Using a boolean expression with an unacceptable range of input is advised here.
  16. This while loop should first print a blank line.
  17. Then it will print:
    That is not a valid menu option.
    Enter option number:
  18. Read the user's menu choice into the associated variable again, which will complete the inner while loop.

  19. Part Three
  20. Print a blank line to the console.
  21. Write an if conditional that prints the following if the user enters 0. Make sure the following text ends with a new line character.
    Thank you for using the CS150 Pseudostring Program. Goodbye.
  22. Next, write another if conditional for the menu choice of 1.
  23. This conditional starts by printing the following prompt:
    Enter an index value:
  24. Read the user's menu choice into your associated variable.
  25. Inside this conditional print out the character at the requested index using the following as a guide:
    Character at Index 5: u
    You may not use any Strings methods to do so.
  26. Lastly, include an additional new line character in the above console statement.

Questions to Consider


Sample Console Output

Welcome to the CS150 Pseudostring Program.

0: Exit Program
1: Determine Character at an Index
Enter option number: 1

Enter an index value: 7
Character at Index 7: c

0: Exit Program
1: Determine Character at an Index
Enter option number: 0

Thank you for using the CS150 Pseudostring Program. Goodbye.



CS Banner
CS Building