Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS12230 Introduction to Programming Extra example– More on responsibilities 1.

Similar presentations


Presentation on theme: "CS12230 Introduction to Programming Extra example– More on responsibilities 1."— Presentation transcript:

1 CS12230 Introduction to Programming Extra example– More on responsibilities 1

2 Recap: Responsibilities Our first examples were mostly about storing information in a simple class – methods used are: constructors, gets, sets, toString, readKeyboard That’s fine but we need more! Once you have more than one class you also typically notice that the different classes have different responsibilities 2

3 More on responsibilities example to read (code in directory) Nim is played by 2 players who use a pile of sticks. The idea is that the players take it in turns taking away number of sticks (up to a maximum) until the person who takes the last stick is the loser. Eg. 12 to start – 3 as maximum player 1 takes 3, player 2 takes 3, player 1 takes 3, player 2 takes 2, player 1 takes last and loses! 3

4 Let’s play it And on board draw a rough object diagram Class diagram is next slide 4

5 Classes: Game, Player, Pile Player – Knows: name, how many just taken (and the pile) – Does: take a turn by removing sticks from Pile Pile – Knows: number of sticks left – Does: reduces its number of sticks Game – Knows about players and pile, who just played and who’s next – Does: Conducts the play until done, giving turns Stops play when winner Who was winner 5 Knows – instance variables Does – methods

6 6 Player -String name +Player (String n) +void takeTurn() Game -Player p1 -Player p2 -Pile pile +Game() +void playGame() +void oneTurn() +boolean isOver() +Player whowon() Pile -int sticks +Pile (int initialNum) +void take(int n) +boolean isEmpty 2..2 1..1

7 Code is on next couple of slides it is pretty hard but actually almost all doable at this point – look at it in your own time the code is in the 2-week2-directory 7

8 8

9 9

10 Another example to think about Xs and Os is played by 2 players who put marks on a board. Each player in turn puts their symbol on the board until either someone wins or the board is full. Classes? Game, Player, Board Responsibilities? 10

11 So what have we done this week? Reinforced basic classes and objects Linking – when one object has 0 or 1 object attributes Reading from the keyboard with Scanner Instance variables vs. local variables The dreaded null pointer Examples that have an Application and DO things Notes up to and including 06 11


Download ppt "CS12230 Introduction to Programming Extra example– More on responsibilities 1."

Similar presentations


Ads by Google