Java Lab #5: Clacky, version 0.1

For this lab you will write a simple four function calculator. This lab combines several features into a single lab experience but it doesn't actually introduce any new features. The instructions below will help guide your work by having you build up the program in small steps.

  1. Create a new project in your Eclipse workspace named Clacky with a class of the same name. Put a main method in that class.

  2. Start by writing a main loop that executes forever, reading a line from the user (print some kind of prompt) repeatedly. Right after reading the line check if the user entered the word "quit" and, if so, terminate the program (for example by calling System.exit).

  3. Use the split method in class String to break the input line into words. The method gives you an array of strings where each element of the array is one word from the line. For example:

            String[] words = line.split("\\s+");
          

    The string "\\s+" is a regular expression that defines the delimiter between the words. It means "one or more spaces." We will talk more about regular expressions later.

  4. Loop over the words in the array computed above and use a switch statement to process each word. You should have a case for each of four operators "+", "-", "*", "/", and a default case. For this step just have the program print out a message about which case it is in.

  5. Create an array of four integers at the start of your program. Before getting to the main loop fill the array with zeros (use a separate loop). Inside the main loop before asking the user to enter a line, print all the elements of this array on a single line. For example, the output of the program might look like:

            0 0 0 0
            >
          
  6. In the default case of the switch statement, convert the word to an integer using Integer.parseInt and then "push" it onto the array by copying all the values "to the right" and putting the new integer into the zeroth array element (on the left). When the program loops back it will then print the updated array along with a second prompt. It should look like this:

            0 0 0 0
            > 42
            42 0 0 0
            >
          
  7. In the "+" case of the switch statement, take the first two elements of the array, add them together, and "push" the result back onto the array as above. You should also arrange for the original first two elements to disappear (this will entail moving the array to the left one position). For example:

            0 0 0 0
            > 42
            42 0 0 0
            > 16
            16 42 0 0
            > +
            58 0 0 0
            >
          

    Because your program processes all words on the line in a loop, the user will be able to type in as many words as she pleases at once. For example:

            0 0 0 0
            > 42 16 +
            58 0 0 0
            >
          
  8. OPTIONAL: Implement the "-", "*", and "/" operations as well.

Turn in your final program with appropriate comments. Be sure your name is on your submission.