Assignment 09

Second, for the windows and walls, you will use the first and last columns and top and bottom rows to be the walls of the school. In so doing, each cell will represent a section of the boundary wall. Regardless of the dimension of the school (10 x 10 in this example), you will place a window at every fourth section, not including corners. Thus, if you start in a corner spot, you will "mark" each 4th cell as a window until you get to the next corner (remember, no windows in the corners).

I cannot stress enough how important it is to overload the insertion operator for the school class. IF you can't see it, you don't know what you have. After you declare a school, you'll want to cout<<my_school; so that you can see what you have. I suggest you build your output to look like this:

Due: Friday, April 17, 2020, at noon, 100 points

For this assignment, you will submit multiple files containing a program written in C++. You know how to use the submit program by now; I needn't tell you.

The following is a list of topics we wish to address with this assignment:

    • classes

    • more classes

    • yet another class

    • everything else you learned up to this point

Resources: www.cplusplus.com Check out the different libraries, especially the iostream library.

Background: As some of you might know, Willie has a drinking problem. And when he falls off the wagon, it's almost always 30 days before he cleans up again. Everyday during that time he will get drunk at work and usually end up in a hedge or a closet or a basement sleeping it off. Willie wants you to write a program that will simulate his "behavior" during these times so that he can have more insight into his expected behavior. (With this information, he could just skip work for a week and roll the dice to find out if he's going to fall out a window, pass out in the hallway, or accomplish the assigned task that day. What a concept!)

Programming assignments #9 and #10 are tied together. In this part (#9), you are going to build some of the classes used in the final program. After building them, you will write a main function that will test the functionality of the classes that you have created. This kind of main function is called a "driver". Its only purpose is to test your classes, to make sure their functionality works as expected. When you build hw #10, you replace this main with a "real" main that does something useful to solve the given problem. The details of the problem are going to be described in hw #10. For now, you just build some of the classes (described below) and a driver to test them.

Just so you know what is coming in assignment #10, here's a brief description: You will create his world (the school - an N x N grid of locations for him to be in), create Willie, place him in the middle of the school (that's the 2-D array just mentioned), randomly place a foul-smelling moldy sack lunch for him to try to find, place windows around the edge of the school, then set Willie off to try to find the sack lunch before he passes out or falls out of a window. It's going to be a real hoot! But for now, let's concentrate on hw 9.

Specifications: You are going to create classes in this part of the project for school, janitor, and lunch. Here are descriptions of them:

  • school class:

    • member variables:

        • a 2-dim array of chars statically declared for MAX size of 25 x 25

        • a short integer describing the actual size of the school ( <= MAX). You can assume that the array or grid is square.

    • member functions:

        • a constructor that allows you to pass in a grid (array) size and defaults to MAX. This constructor will call private functions that will clear() the grid, then a build() function that will place walls and windows as described below.

        • a print function or the overloaded insertion operator. In hw #10, it must be an overloaded insertion operator. Of course, this really should be a non-member friend.

        • appropriate accessor/mutator (set/get) functions.

  • janitor class:

    • member variables:

        • a member for the janitor's blood alcohol content (bac).

        • a member for the number of bruises.

        • appropriate variable(s) for his location on the grid (in the school).

        • a char for how he/she is to be represented. J seems like a good choice.

        • state variable(s) to represent whether he/she is sober, drunk, or dead.

        • a string for his/her name.

    • member functions:

        • a constructor that takes arguments for character to represent the janitor and defaults it to 'J', and name. His/her bac will always start at .05, bruises at 0, and he'll/she'll be (alive and) sober. Default the start position to (-1,-1).

        • a place_me() function that has a reference parameter of type school. It will determine the dimensions of the school, and from that will place the janitor in the school's grid and change the janitor's (private) coordinates. Of course, janitor always prefers to be in the middle of things.

        • a rand_walk function() that has a reference to a school parameter. It will pick a random direction in which to take a step, and it will change its own coordinates and the location it occupies in the school passed to it.

        • a print() function or overloaded insertion operator.

  • lunch class:

    • member variables:

        • a member for location.

        • a char member for its representation. L seems like a good choice. (L is for ... lunch)

    • member functions:

        • a constructor that initializes the location variable(s) to (-1, -1).

        • a place_me() function to which you pass a school (by ref). It will place the lunch in the school at a random location in the school based on the dimensions of the school and not on top of anything else.

        • a random_move() function much like that for janitor. Yes, the lunch is soo moldy that it has grown legs. You will need to pass a school object to it for obvious reasons.

Notes: First, I want to say something about the 2-D array that is going to represent the school. Think of the 2-D array as like an N x N grid of places any character (a janitor, a lunch, a bottle of whiskey) can be. It's the (simplified) floor plan of the school. Now remember, you only know static arrays at this point in time: the size of the array must be known at compile time. So, if you include a, say, 25 x 25 array in your school, it's sort of like saying that all schools in the world can not be any larger than 25 x 25. So, you need another variable telling you just what part of that 25 x 25 is actually being used for the school. So, a particular school object might only be 20 x 20. You can think of the rest as being playground or something...maybe closet space. Whattteevvveerrrrrr..Thus, when you declare a school object, you are automatically given a 25 x 25 2-D array of whatever the base type is. And remember, there is something in that array! To declare a 2-D array, it's simple: base_type array_name [rows][cols]; And remember, indexing is 0 -> size-1 in both dimensions. [The only trick to know about using 2-D arrays is that, if you pass it to a function, you must put the second dimension inside the brackets in the prototype for the function.] Now notice that the base type of the grid in the school class is char. That's because you will use different characters to represent the different things the janitor will interact with.

Look carefully and I think you can see how to program this. The characters in the center of each "cell" are the contents of the array; a blank if empty, and a letter representing an "actor" if it is something else. [Note: I made this png with Word. The alignment is off a little. When you program it, it would be perfect.]

Now let's talk about the driver, your main. In your main, you will need to declare instances of each of the above classes and test their functions. For school, declare a school object (with a size less than MAX) and print it to see that the print function works and the "stuff" is placed appropriately. Declare a janitor and print him. Then declare another with different parameters and print to see if he is different. Have the objects call their place_me() functions. Have them move and see that it works. You'll need to print the school to see that the moved. Etc.

Please understand that the final hw 10 program will be more interesting and fun, but you can only do so much at a time. This much will do for now. There will be objects of type whiskey bottle and coffee thermos that the janitor may happen upon! What then? Ha ha, it'll be a hoot.

Special notice: This assignment is NOT easy. Start NOW!!

As always, if you have questions, don't hesitate to ask your instructor, or ask the discord guys.