Lab 10: Book Reading
Concepts
In this assignment, you will implement a trivial simulation of a person reading a book. Focus on two main concepts: learning about unfamiliar APIs and how to instantiate objects.
Simulation
"We live in a world where there is more and more information, and less and less meaning."
- Jean Baudrillard, Simulacra and Simulation
Despite it's theoretical roots in computer science, the application of computer simulations are one of the most important and effective uses of computing power today. Computer simulation is used in engineering, weather analysis, disease outbreaks, flight and automobile training, stock market modeling and games.
In this assignment, you will create a simple simulation that models the process of a person reading a book. While we humans may take this process for granted, describing this process to a machine is more complicated than you think. At the end of the assignment, take a step back and compare the way you think about reading a book and compare it to the process your program describes.
Then consider your perception of your own engineering subjects, or weather systems or the stock market: how might you write computer programs to model and simulate such topics?
How does the simplicity of a simulation affect the accuracy of the results it attempts to model?
Object Instantiation
In class we introduced some terminology, particularly that of object instantiation. Unlike the cases of using primitive values such as numbers, when you create objects you say that "I am instantiating an object." This means you're creating a single instance of a certain type of thing. For example, you are a distinct, single instance of a human being. The cell phone in your pocket is a distinct, single instance of, say, the Samsung Galaxy.
To begin, consider the fact that there is at least one way to create or instantiate an object. You're already familiar with it:
vector
Above we have to examples of object instantiation. The first line tells the computer, "Computer, instantiate a string called name." The second line tells the computer, "Now instantiate a vector of ints called numbaz."
When you create an object, you are instantiating an object or "creating an instance."
There are many ways to instantiate objects, and this is determined by the code that actually implements the objects, which we will learn about next week. For now, we'd like you to think of two ways of instantiating objects: the "default" way, and a "custom" way.
Default Instantiation
Every object typically has a "default" mechanism of creation. For example, to create a "default" string object you might write something like this:
Look familiar? Here, in fact, you are instantiating a string in the "default" manner, which is to instantiate a string whose content is empty. Let's take a look at another example, using a Person object:
This tells the computer, "Hey computer, instantiate a Person in the default manner." But what is this "default manner?" What is someone
's name? What is someone
's age? This is entirely dependent on the code that is used to provide you Person objects, and is determined by that code's author.
One more example, this time with a book:
Here we've told the computer, "Hey computer, instantiate a Book in the default manner." But what is that someBook
's title? How many pages does someBook
have?
(In these examples we are, in fact, invoking the default constructor of the string, Book or Person class. More on this next week.)
Non-Default Instantiation
You should be thinking, "Ok, so how do I create my own books or people without using this default instantiation stuff?" (but we understand if you're thinking about the wild programming party instead). Let's dive in with an example:
Person jack("Jack Johnson", 43); // "non-default" Person object
Whoah! Syntax alert!
Above we have two Person objects. One is called anonymous
and is created in the "default" manner. The second is called jack
and is created in a non-default or "custom" manner, initializing it's name to "Jack Johnson" and its age attribute to 43. (How do we know what values we can provide an object? See the API below.) Notice how, when instantiating a non-default object, we append a parenthesized list of values. It looks like a function call... and you may or may not want to think of it that way (we'll see why next week).
Confused? Great! It takes a while to remember this object instantiation syntax. Let's take a look at another example:
If you take a look at the API below, you'll notice that we can create Book objects by providing a title and number of pages during instantiation.
The single most important syntax rule to remember is this: when instantiating a "default" object we do it just like a variable declaration:
And when instantiating a "custom" or non-default object we use a parenthesized list of arguments:
Lastly, what arguments you can use when instantiating an object are entirely dependent on the API and the programmer who creates it.
Instructions
© PBS
Create a simple simulation of a person reading a book entirely in one sitting, using a Person object and a Book object which are provided for you. An example console output will look like this:
The default person is No Name (age 21)
Opening the book called The RSpec Book (5 pages)
Reading page 1
Turning to page 2
Reading page 2
Turning to page 3
Reading page 3
Turning to page 4
Reading page 4
Turning to page 5
Reading page 5
Closing the book called The RSpec Book
In order to generate the above output, your program will:
- Instantiate a default Person object, and print its name and age to the screen.
- Instantiate a default Book object, and print its title and number of pages on the screen.
- Instantiate custom Person and Book objects, and successfully model the process of reading a book, as described below.
Note that this assignment is very much like a puzzle. We're providing you a task and an end goal... now let's look at the problem constraints and the "puzzle pieces" you have to work with.
The Model
This simulation uses a very simple model of book reading. We will assume that People read Books through the process of:
- Opening the book.
- Reading a page.
- Turning a page.
- Repeating steps 2 and 3 until the last page is read.
- Closing the book.
There is one main constraint to this simulation: the book must be read in "one sitting." That is, the goal of your simulation is to model a person opening a book, reading every page and then closing that book. The API enforces this constraint, requiring the person to read every page before the book can be closed. It's like a curse... or a really, really good book that you just can't put down.
The Person API
Person objects have two attributes: a name
and an age
.
You are provided the ability to instantiate Person objects. There are two mechanisms for this.
The default:
Creates an instance of a Person with the name "No Name" and an age of 21.
You may also instantiate Person objects like so:
This instantiates a Person called me
with the name "My Name" and the age 21.
Person objects have two accessible attributes:
string name
(eg,me.name
)int age
(eg,me.age
)
Person objects have the following member functions:
bool openBook(Book b)
Opens the Bookb
to the first page and automatically prints "Opening book" to the console.bool readPage(Book b)
Reads the current page of Bookb
and automatically prints "Reading page N" to the console.bool turnPage(Book b)
Increments the current page of Bookb
and automatically prints "Turning to page N" to the console.bool closeBook(Book b)
Attempts to close Bookb
. Successful only if each page ofb
has been read. Automatically prints "Closing book X" to the console (where X is the book's title).
You may find the need to experiment with the Person API! You may also look at Person.cpp
to see what each member function actually does.
The Book API
Book objects have two attributes: a title and a number of pages.
You are provided the ability to instantiate Book objects. There are two mechanisms for this.
The default:
Creates a Book with a title of "Untitled" having 10 pages.
You may also instantiate Book objects like so:
This instantiates a Book called myBook
with the title "Simulacra and Simulation" having 238 pages.
Book objects have the following member functions:
string getTitle()
which returns the title of the book.int getPages()
which returns the total number of pages in the book.int getCurrentPage()
which returns the current page number the book is open to.
If you look at book.cpp
you'll find additional behavior and attributes, but you do not need to use them for this assignment.
Hints
In order to use the Book and Person objects in your program you must "include" them in main like so:
#include "person.h"
Stick to the API described above.
Despite the fact that they return bool
s, you do not need to use the return values of openBook(), turnPage(), readPage()
or closeBook()
.
Realize that the console output for opening, reading and closing the book are generated automatically for you, when calling the respective member functions.
Think about the simulation process described above, and how the API "fits" into that process.
Experiment and ask questions!
Requirements and Rubric
A friendly message from The Terminator, our grading program
Hello *bzzt*. I will check for the following:
Your program must not require user input.
Your program must print the name and age of a default Person, and the title and number of pages of a default Book.
Program must generate output describing the reading process, as described above.
This work is worth 205 points.
Requirement | Points | Notes |
---|---|---|
Place your name in the comment header in main.cpp | 2 | |
Correct submission of src directory as a .zip file. | 3 | |
Correctly includes the header file "book.h" | 5 | |
Correctly includes the header file "person.h" | 5 | |
Correctly instantiates default Person | 10 | |
Correctly instantiates default Book | 10 | |
Prints "The default book is TITLE (N pages)" where TITLE is the default title and N is the default number of pages. | 20 | |
Prints "The default person is NAME (age N)" where NAME is the default name and N is the default age. | 20 | |
Correctly instantiates non-default Person | 20 | |
Correctly instantiates non-default Book | 20 | |
Successfully opens book | 20 | |
Successfully reads all pages | 30 | while loop? |
Successfully closes book | 20 | |
Decent style, whitespace, formatting | 20 | check the style guide? |
Concepts Exercised: application, simulation, OOP, APIs, reading code, loops, declaring facts, making decisions
© 2011 Yong Joseph Bakos.