CSE 4208: Studio 2

State and Operations on it


In today's studio you will explore how to represent program state using a simple enum class representation, for a couple of different kinds of state transition systems (days of the week, and pressure) that were discussed in today's lecture. In this studio you will also implement operators over variables of those enumerated types to (1) print out string representations of those states to an output stream, and (2) change from one state to another where allowed by the state transition system. Your experience with those representations and the operators over them (and with a few modifications possibly also some of your code from this studio) will help you get started on similar state representations and operators in the Lab 0 assignment.


Please complete the following required exercises. I encourage you to please work in groups of 2 or 3 people on each studio (and the groups are allowed to change from studio to studio) though if you would prefer to complete any studio by yourself that is also allowed.

As you work through these exercises, please record your answers, and when you finish them please log into Canvas, select this course in this semester, and then upload a .txt file containing your answers on the Canvas page for this studio assignment. Only one submission per team, please, and if you need to re-submit it the person who originally submitted the studio should please be the one to do that.

Make sure that the name of each person who worked on these exercises is listed in the first answer, and make sure you number each of your responses so it is easy to match your responses with each exercise.


Required Exercises

  1. As the answer to the first exercise, list the names of the people who worked together on this studio.
  2. First, ssh into shell.cec.wustl.edu using your WUSTL Key id and password and then use qlogin to log into one of the Linux Lab machines and then confirm that the version of g++ there is correct (as you did in Studio 0). Then cd into your directory for this course this semester, create a new subdirectory for this studio, and cd into it.

    Create a header (.h) and source (.cpp) file in that directory, with the source file including the header file. In them declare an enumerated type (enum class Weekday) for the days of the week example that was discussed in today's lecture, and then declare and define a shift operator (operator<<) that (1) takes a reference to an ostream and a const reference to a variable of that enumerated type, (2) uses a switch statement to insert a string corresponding to the variable's value (e.g., "Sun" for Sunday, etc.) into the ostream, (3) inserts a blank space into the ostream, and then (4) returns a reference to that same ostream.

    Define a main function for your program, and in it declare a variable of the Weekday enumerated type that is initialized to one of the enumerated values. It should then use the shift operator you wrote to insert the variable into the standard output stream (cout) and then return a zero value to indicate success.

    Compile and run your program, and as the answer to this exercise please show (1) the code you wrote for this exercise and (2) the output your program produced by running it.

  3. Declare and define a postfix increment operator (operator++) for the Weekday enumerated type that uses a switch statement to change the variable's state to the next-higher value in the enumeration if there is one, or if the variable already has the highest value possible the operator should change it to the lowest value (looping around from highest to lowest in a semantics like that seen in modulus arithmetic). In your main function use that operator within a do while loop to cycle the variable through all of its possible values, using the shift operator from the previous exercise repeatedly to print out a line to the standard output stream (cout), with each value's corresponding string appearing exactly once in that line.

    Compile and run your program, and as the answer to this exercise please show (1) the new code you wrote for this exercise and (2) the output your program produced by running that code.

  4. Declare and define a postfix decrement operator (operator--) for the Weekday enumerated type that uses a switch statement to change the variable's state to the next-lower value in the enumeration if there is one, or if the variable already has the lowest value possible it should change it to the highest value (looping around from lowest to highest). In your main function use that operator within an additional do while loop to cycle the variable through the possible values in the reverse order of the previous exercise, again using the shift operator repeatedly to print out a line to the standard output stream (cout), with each value's corresponding string appearing exactly once in that line.

    Compile and run your program, and as the answer to this exercise please show (1) the new code you wrote for this exercise and (2) the output your program produced by running that code.

  5. Declare an enumerated type (enum class Pressure) for the pressure example that was discussed in today's lecture, and then declare and define a shift operator (operator<<) that (1) takes a reference to an ostream and a const reference to a variable of that enumerated type, (2) uses a switch statement to insert a string corresponding to the variable's value (e.g., "Low" for low, etc.) into the ostream, (3) inserts a blank space into the ostream, and then (4) returns a reference to that same ostream.

    In the main function for your program, declare a variable of the Pressure enumerated type that is initialized to one of the enumerated values, and then use the shift operator you wrote to insert the variable into the standard output stream (cout).

    Compile and run your program, and as the answer to this exercise please show (1) the new code you wrote for this exercise and (2) the output your program produced by running it.

  6. Declare and define a prefix increment operator (operator++) for the Pressure enumerated type that uses a switch statement to change the variable's state to the next-higher value in the enumeration if there is one, or if the variable already has the highest value possible the operator should leave the variable's value unchanged. In your main function use that operator within a for loop to cycle a variable through its low, medium, and then high pressure values using the shift operator from the previous exercise repeatedly to print out a line to the standard output stream (cout), with the corresponding string for each of those values (but not the string for the pop state) appearing exactly once in that line.

    Compile and run your program, and as the answer to this exercise please show (1) the new code you wrote for this exercise and (2) the output your program produced by running that code.

Things to Turn In:

For this studio, please turn in the following: