Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercises on Basic OOP TCP1201: 2013/2014.  Problem: Design and build a computer hockey game.  Object: Hockey player  Attribute: Position, height,

Similar presentations


Presentation on theme: "Exercises on Basic OOP TCP1201: 2013/2014.  Problem: Design and build a computer hockey game.  Object: Hockey player  Attribute: Position, height,"— Presentation transcript:

1 Exercises on Basic OOP TCP1201: 2013/2014

2  Problem: Design and build a computer hockey game.  Object: Hockey player  Attribute: Position, height, weight, salary, number of goals  Behaviors: Pass the puck, shoot, skate forward, skate backward, etc. Identify one object, with its attribute and behaviors: Exercise 1a

3  Let’s assume we only have the following attributes for the object hockey player:  Object: Hockey player  Attribute: Position, height, weight  Behaviors: Pass the puck, shoot, skate Design the UML Diagram. Provide the C++ code and separate the interface and implementations into.h and.cpp files. You can leave the actual implementation in the methods empty.

4 UML Diagram Player -height: float -weight: float -position: int [*] +getHeight():float +setHeight(h:float):void +getWeight():float +setWeight(w:float):void +getPos(x:int,y:int):void +setPos(x:int,y:int):void +passPuck(Player p):void +shoot():bool +skate():void

5 5 #ifndef PLAYER_HPP #define PLAYER_HPP #include Using namespace std; class Player { private: int position[2]; float weight, height; public: void setWeight(float weight); void setHeight(float height); void setPos(int x, int y); float getWeight(); float getHeight(); void getPos(float &x, float &y); int passPuck(Player p); int shoot (); void skate (); }; // End of class #endif Player.hpp

6 6 #include #include “Player.hpp” using namespace std; void Player::setWeight(float weight) { this->weight = weight;} void Player::setHeight(float height) { this->height = height; } void Player::setPos(int x, int y) { position[0]=x; position[1]=y; } float Player::getWeight() { return weight;} float Player::getHeight() { return height; } void Player::getPos(float &x, float &y) { return position} void Player::passPuck(Player p) {...} bool Player::shoot() {... return goal; } void Player::skate() {...} Player.cpp

7 Exercise 1b  The Hockey Player class:  Object: Hockey player  Attribute: Position, height, weight  Behaviors: Pass the puck, shoot, skate 7 Can we replace the attribute “position” (integer array) with “Point” type? If yes, what is the type of relationship between “Point” and “Player”? class Point { int x, y; public: void readPoint(); void printPoint(); double distPoint (Point q); }; Task: Recall the Point class.

8 8 #ifndef POINT_HPP #define POINT_HPP #include class Point { int x, y; public: void setPoint(int x, int y); int getX(); int getY(); void printPoint(); double distPoint (Point q); }; #endif Point.hpp #include #include “Point.h” using namespace std; void Point::setPoint(int x, int y){ this->x = x; this->y = y; } int getX() { return x; } int getY() { return y; } void Point::printPoint() { cout << "(x = " << x << ", y = " << y << ")" << endl; } double Point::distPoint (Point q) { double dx = x - q.getX(); double dy = y - q.getY(); double dsquared = dx*dx + dy*dy; return sqrt(dsquared); } Point.cpp

9 9 UML Class Diagram Player -height: float -weight: float -position: int [*] +getHeight():float +setHeight(h:float):void +getWeight():float +setWeight(w:float):void +getPos(x:int,y:int):void +setPos(x:int,y:int):void +passPuck(Player p):void +shoot():bool +skate():void Player -height: float -weight: float -position: Point +getHeight():float +setHeight(h:float):void +getWeight():float +setWeight(w:float):void +getPos():Point +setPos(pos:Point):void +passPuck(Player p):void +shoot():bool +skate():void Composition

10 10 UML Class Diagram Player -height: float -weight: float -position: Point +getHeight():float +setHeight(h:float):void +getWeight():float +setWeight(w:float):void +getPos():Point +setPos(pos:Point):void +passPuck(Player p):void +shoot():bool +skate():void 11 Point -x: int -y: int +void setPoint(int x, int y); +void readPoint(); +void printPoint(); +double distPoint (Point q); Composition

11 11 #ifndef PLAYER_HPP #define PLAYER_HPP #include #include “Point.hpp” using namespace std; class Player { private: Point position; float weight, height; public: void setWeight(float weight); void setHeight(float height); void setPos(Point p); Point getPos(); float getWeight(); float getHeight(); int passPuck(Player p); int shoot (); void skate (); }; // End of class #endif Player.hpp

12 12 #include #include “Player.hpp” #include “Point.hpp” using namespace std; void Player::setWeight(float weight) { this->weight = weight;} void Player::setHeight(float height) { this->height = height; } void Player::setPos(Point p) { (this->position).setPoint(p.getX(), p.getY()); } float Player::getWeight() { return weight;} float Player::getHeight() { return height; } Point Player::getPos() { return position} void Player::passPuck(Player p) {...} bool Player::shoot() {... return goal; } void Player::skate() {...} Player.cpp

13 Further Discussion: Next, we want to create a complete hockey game that would allow the setup of two teams of players. What are the additional classes that we can design? What is the relationship between the new classes and the existing classes (Player, Point)? Exercise 1c


Download ppt "Exercises on Basic OOP TCP1201: 2013/2014.  Problem: Design and build a computer hockey game.  Object: Hockey player  Attribute: Position, height,"

Similar presentations


Ads by Google