Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045...

33
Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS G029 Sample solutions are on the web site. Previous years’ exams were sent last week along with the detailed email about the midterm. HW3 will be assigned this week Due 12 nd August Friday next week.

Transcript of Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045...

Page 1: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

AnnouncementsMidterm is TOMORROW!

On August 2nd Tuesday at 19:40 (~ 100 minutes) in FENS L045Today at 14:30-17:30 there is an extra recitation in FENS G029Sample solutions are on the web site.Previous years’ exams were sent last week along with the

detailed email about the midterm.

HW3 will be assigned this weekDue 12nd August Friday next week.

Page 2: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Implementation of Robot Class - 1

Your next homework will be about updating the Robot class you will add some new member functions that requires to deal with

robots.h and robots.cpp files (actually in the homework, you will use an updated class for which the file names are robots_modified.h and robots_modified.cpp)

and you will use those newly added functions in an application It is a good idea to have a look at how this class is implemented

It is designed and implemented by Ersin Karabudak We have made some changes later

Robot class implementation is quite complexRobot, RobotWindow and RobotWorld are different structures

we will not deal with RobotWindow and RobotWorld, but the implementation file contains robot class implementation and the details of RobotWindow and RobotWorld too. Do not get confused.

Robots are maintained as a circular doubly linked list it is a data structure that uses pointers (probably will see in CS202) but do not get thrilled! you will not need those complex structures for the

member functions that you will add.

Some details you have to know will be given now and more details will be given in recitations this week

Page 3: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Implementation of Robot Class - 2

enum Direction { east, west, north, south };

enum Color { white, yellow, red, blue, green, purple, pink, orange };

class Robot

{

public:

Robot (int x, int y, Direction dir = east, int things = 0);

~Robot ();

void Move (int distance = 1);

bool Blocked ();

void TurnRight ();

bool PickThing ();

bool PutThing ();

void SetColor (Color color);

bool FacingEast ();

bool FacingWall ();

bool CellEmpty ();

bool BagEmpty ();

constr

uctor

Destructor (not needed in HW5)

member functions

continued on the next slide

Page 4: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Implementation of Robot Class - 3

private:int xPos; //x coordinate of the location of robotint yPos; //y coordinate of the location of robotDirection direction; //current direction of robotColor color; //current color of robotint bag; //current # of things in the bag of robotbool stalled; //true if the robot is deadbool visible; //true if the robot is visible

Robot *next;Robot *prev;static Robot *list;

friend struct RobotWindow; };

Private Data

pointers for the data structure you will not need them

RobotWindow may refer Robot’s private data

Page 5: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Implementation of Robot Class - 4

Previous two slides were in the robots.h (now robots_modified.h). Now let’s go over the robots.cpp (now robots_modified.cpp) file in

VC++ environment

In the next homework, you are going to add 6 member functions to the robot class Some of the member functions will be done in recitations this week

Hints try to use currently available member functions

e.g. for PickThings, try to use PickThing in a loop rather than writing some thing similar to PickThing

do not hesitate to modify or access private data members when needed e.g. you will need such an update for TurnFace function

if you change the state of a robot within the current cell, use the following to update the windowtheRobotWindow->Redraw(this);

Page 6: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Implementation of Robot Class - 5

Hints for the next homework (cont’d) you will need to use the function called IsPressed defined in miniFW.h

(it is going to be renamed as miniFW_modified.h) so include this header file to your main program file this function (IsPressed) is to check whether a key (e.g. an arrow key) is

pressed or not - details are in recitations

Some other changes in the Robot World and Robot Class If a robot hits another robot, both die! No automatic message is displayed when a robot dies Now the bag content is written in robots (if not zero)

Use robots_modified.h, robots_modified.cpp, miniFW_modified.h and miniFW_modified.cpp files in HW5 They will be provided to you in the homework and/or recitation package

Page 7: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

The class DateThe class Date is accessible to client programmers#include "date.h"

to get access to the classThe compiler needs this information.It may also contain documentation for the programmer

Link the implementation in date.cppAdd this cpp to your project

The class Date models a calendar date:Month, day, and year make up the state of a Date objectDates can be printed, compared to each other, day-of-week

determined, # days in month determined, many other behaviors Behaviors are called methods or member functions

Page 8: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Constructing Date objects – see usedate.cpp Date today; Date republic(10,29,1923); Date million(1000000); Date y2k(1,1,2000); cout << "today: " << today << endl; cout << "Republic of Turkey has been founded on: "

<< republic << endl;

cout << "millionth day: " << million << endl; OUTPUT

today: April 12 2009Republic of Turkey has been founded on: October 29 1923millionth day: November 28 2738

Page 9: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Constructing/defining an object

Date objects (as all other objects) are constructed when they’re first definedThree ways to construct a Date

default constructor, no params, initialized to today’s date single long int parameter, number of days from January 1, 1 three params: month, day, year (in this order).

Constructors for Date objects look like function callsconstructor is special member functionDifferent parameter lists mean different constructors

Once constructed, there are many ways to manipulate a DateIncrement it using ++, subtract an integer from it using -, print it using cout, …

MonthName(), DayName(), DaysIn(), …See date.h for more info on date constructors and member functions

Page 10: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Date Member FunctionsDate MidtermExam(4,6,2011);

Construct a Date object given month, day, year

MidtermExam.DayName()Returns the name of the day (“Monday” or “Tuesday”, or ...)

in this particular case, returns “Wednesday” since April 6, 2011 is a Wednesday

MidtermExam.DaysIn()Returns the number of days in the particular month

in our case return 30, since April 2011 has 30 days in it

Add, subtract, increment, decrement days from a dateDate GradesDue = MidtermExam + 2;GradesDue is April 8, 2011

Let’s see usedate.cpp in full and datedemo.cpp now

Page 11: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Example: Father’s day (not in book)

Father’s day is the third Sunday of Junewrite a function that returns the date for the father’s day of a given

year which is the parameter of the functionIn main, input two years and display father’s days between those

years

Date fathersday(int year)// post: returns fathers day of year{ Date d(6,1,year); // June 1

while (d.DayName() != "Sunday") { d += 1; }

//d is now the first Sunday, 3rd is 14 days later return d + 14;}

See fathersday.cpp for full program

Page 12: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

What if there were no date class?

It would be very cumbersome to deal with dates without a date classimagine banking applications where each transaction has

associated date fields

Classes simplify programming they are designed and tested.then they can be used by programmers

You are lucky if you can find ready-to-use classes for your needsotherwise ???

Page 13: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Updating a Class (not in book)

Suppose you want to add more functionality to the date classneed to change the header file (date.h)need to add implementation of new function(s) to

date.cppExample: a new member function to calculate and

return the remaining number of days in the object’s month any ideas? do you think it is too difficult?have a look at the existing member functions and see if

they are useful for you

Page 14: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Updating a Class (not in book)We can make use of DaysIn member function

Prototype in Date class (add to the header file)int RemainingDays () const;

Implementationint Date::RemainingDays () const{ return DaysIn() - myDay;

}

In a member function implementation private data and other member functions referred without the dot operator. They operate on the object for which the member function is called

Page 15: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Updating a Class (not in book)

Example use of RemainingDays

Date today;cout << "There are " << today.RemainingDays() << " days left in the current month" << endl;

See date_modified.h, date_modified.cpp and demodatemodified.cpp

When RemainingDays is called,call to DaysIn is for object today

since it is the object on which RemainingDays is calledmyDay is today’s myDay

since it is the object on which RemainingDays is called

Page 16: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Design HeuristicsWhat is an heuristic?

a set of guidelines and policies may not be perfect, but mostly useful exceptions are possible

e.g. making all state data private is an heuristicwe will see two more class design heuristics

cohesion and couplingMake each function or class you write as single-purpose as

possibleAvoid functions that do more than one thing, such as reading numbers and

calculating an average, standard deviation, maximal number, etc., If source of numbers changes how do we do statistics? If we want only the average, what do we do?

Classes should embody one concept, not several. This heuristic is called Cohesion.

Functions (both member and free functions) and classes should be cohesive, doing one thing rather than several things.

Easier to re-use in multiple contexts and several applications

Page 17: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Design Heuristics continued (Coupling)

Coupling: interactions among functions and classesFunctions and classes must interact to be useful

One function calls anotherOne class uses another, e.g., as the Dice::Roll() function

uses the class RandGen

Keep interactions minimal so that classes and functions don’t rely too heavily on each other: it is better if we can change one class or function (to make it more efficient, for example) without changing all the code that uses it

Some coupling is necessary for functions/classes to communicate, but keep coupling loose and be aware of them

Page 18: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Designing classes from scratch

Chapter 7 (especially 7.1 and 7.2)a good development strategy

“iterative enhancement” approachREAD those sections, you are responsible

we won’t cover all, because it takes too much time and becomes boring!

I will give a simpler class design example hereless iterativebut similar application

Page 19: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Implementing Classes – Iterative Enhancement It is difficult to determine what classes are needed, how they should be implemented, which functions are required

Experience is a good teacher, failure is also a good teacher

Good design comes from experience, experience comes from bad design

Design and implementation combine into a cyclical process: design, implement, re-visit design, re-implement, test, redesign, …

Grow a working program, don’t do everything at the same time

Page 20: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Design and Implementation Heuristics

A design methodology says that

“look for nouns, those are classes”, and then “look for verbs and scenarios, those are member functions”

Not every noun is a class, not every verb is a member functionsome functions will be free ones or will be implemented in main

(these are design decisions)

Concentrate on behavior (member functions) first when designing classes, then on state (private part)private data will show its necessity during the implementation of

the public part

Page 21: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Example class designQuiz class

simple quiz of addition questionsScenarios

user is asked a number of questionscomputer asks random questionsuser enters his/her answer

correct / not correct feedback and correct answer are displayed

correct answers are countedThere may be two classes

questionquiz

but I will have one class which is for question and implement quiz in mainBe careful! This example is similar but different than the one in

book (Sections 7.1 and 7.2)

Page 22: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question classQuestion behaviors (verbs). A question is

createdaskedansweredchecked

These are candidate member functionsmore? less? we will see

A question is simply two random integers (to keep it simple say between 1 and 100) to be addedthose numbers are definitely in class private datawhat else?

we will see

Page 23: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question classsimplemathquest.h (first draft)

class Question{ public: Question(); // create a random question void Ask() const; // ask the question to user

int GetAnswer() const; //input and return user answer bool IsCorrect(int answer) const; //check if correct private: int myNum1; // numbers used in question int myNum2;};

Page 24: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Quiz program (main - simplequiz.cpp) – Draft 1

int qNum = PromptRange("how many questions: ",1,5); int k, ans, score =0;

for(k=0; k < qNum; k++) {

Question q;q.Ask();ans = q.GetAnswer();if (q.IsCorrect(ans)){ cout << ans << " correct answer" << endl << endl; score++;}else{ cout << "Sorry, not correct. Correct answer was "

<< ???????? << endl << endl;}

} cout << "Score is " << score << " out of " << qNum << " = " << double(score)/qNum * 100 << "%" << endl;

Something missing: a function to return the correct result

Page 25: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question class simplemathquest.h (second draft)

class Question{ public: Question(); // create a random question void Ask() const; // ask the question to user

int GetAnswer() const; //input and return user answer bool IsCorrect(int answer) const; //check if correct int CorrectAnswer() const; //return the correct answer private: int myNum1; // numbers used in question int myNum2;};

Page 26: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Quiz program (simplequiz.cpp) – Draft 2

int qNum = PromptRange("how many questions: ",1,5); int k, ans, score =0;

for(k=0; k < qNum; k++) {

Question q;q.Ask();ans = q.GetAnswer();if (q.IsCorrect(ans)){ cout << ans << " correct answer" << endl << endl;

score++;}else{ cout << "Sorry, not correct. Correct answer was " <<

q.CorrectAnswer() << endl << endl;}

} cout << "Score is " << score << " out of " << qNum << " = " << double(score)/qNum * 100 << "%" << endl;

Page 27: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question class implementationsimplemathquest.cpp (draft 1)

void Question::Ask() const

{

cout << myNum1 << " + " << myNum2 << " = ";

}

Question::Question(){ RandGen gen; myNum1 = gen.RandInt(1,100); myNum2 = gen.RandInt(1,100);}

constructor

int Question::GetAnswer() const

{

int ans;

cin >> ans;

return ans;

}

Ooops! We did not access or modify the object’s state. It is better not to have this function

as a member function

Page 28: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question class implementationsimplemathquest.cpp (draft 1) - continued

Problem: Where is the correct answer stored? a new private data field would be good

bool Question::IsCorrect(int answer) const

{

return ?????? == answer;

}

int Question::CorrectAnswer() const

{

return ??????;

}

Page 29: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question class simplemathquest.h (final)

class Question{ public: Question(); // create a random question void Ask() const; // ask the question to user

bool IsCorrect(int answer) const; //check if correct int CorrectAnswer() const; //return the correct answer private: int myNum1; // numbers used in question int myNum2; int myAnswer; // store the answer};

int GetAnswer() const; //input and return user answer

Page 30: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question class implementation simplemathquest.cpp (final)Question::Question(){ RandGen gen; myNum1 = gen.RandInt(1,100); myNum2 = gen.RandInt(1,100); myAnswer = myNum1 + myNum2;}

void Question::Ask() const{ cout << myNum1 << " + " << myNum2 << " = ";}

int Question::GetAnswer() const{

int ans;cin >> ans;return ans;

}

Page 31: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Question class implementationsimplemathquest.cpp (final) - continued

bool Question::IsCorrect(int answer) const

{

return myAnswer == answer;

}

int Question::CorrectAnswer() const

{

return myAnswer;

}

Page 32: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Quiz program (simplequiz.cpp) – Final

int qNum = PromptRange("how many questions: ",1,5); int k, ans, score =0;

for(k=0; k < qNum; k++) {

Question q;q.Ask();cin >> ans;if (q.IsCorrect(ans)){

cout << ans << " correct answer" << endl << endl;

score++;}else{ cout << "Sorry, not correct. Correct answer was " <<

q.CorrectAnswer() << endl << endl;

} } cout << "Score is " << score << " out of " << qNum << " = " << double(score)/qNum * 100 << "%" << endl;

Page 33: Announcements Midterm is TOMORROW! On August 2 nd Tuesday at 19:40 (~ 100 minutes) in FENS L045 Today at 14:30-17:30 there is an extra recitation in FENS.

Thinking furtherWhat about a generic question class

not only addition, but also other arithmetic operationsmay need another private data member for the operation that is

also useful to display the sign of operation in Askmay need parameter in the constructor (for question type)will do this week in recitations

What about questions for which answers are strings? maybe our generic question class should have string type answers to serve not

only to arithmetic questions but any type of questions see Sections 7.1 and 7.2