Upload
joslyn-jarvis
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
CS12230Introduction to ProgrammingLecture 4-x – Consolidation
1
What have we learned:
• Design • Java
• Rest of these slides 2 examples – • nightclass and nim
2
Simple Nim is a two-player game in which players take turns removing objects from a pile. On each turn, a player must remove at least one stick, and may remove any number of objects up to some maximum. The Loser is the player who takes the last stick.
Example with 7 sticks, maximum of 3 per turn:player 1 takes 3 - 4 leftplayer 2 takes 3 - 1 leftplayer 1 has to take the last one and loses(See wikipedia for more interesting versions)
3
ANALYSIS
4
Use Case Diagram
5
- put in initial info- play game
2 users
• Describes what happens in broad terms • Shows who the users are (sometimes more than one kind)
DESIGN
6
Design Tools
• Diagrams: – Object diagram– Class diagram
NEW:– Pseudocode or flow charts
7
Object Diagram
8
nimpile p1 p2
pile“fred”
pile“bill”
12
:Game
:Pile
:Player
:Player
:NimGame
Could amalgamate Game and NimGame by putting main() in Game
• A snapshot of a program at some time – different at different times
Class Diagram
9
Application
Pile-int numsticks
Player-String name-Pile pile
Game-Player p1, p2-Pile pile
1..1
2..2
1..11..1
• A static description of the kinds of things (classes) in a program
Pseudocode or Flowchart
Playgame:While ! Game over
-play one turn
10
Put in initial info: – this is constructor of Game-Make pile-Make player1-Make player 2-Link up players and pile Play one turn:
-get num sticks from curent_plr-current_ plr.takeTurn(num)
These are all in Game (and more too)
Game over:Is the pile empty?
• A way of describing the behaviour of a program
Pseudocode (continued)
11
This is in Player - Remember: responsibilities
takeTurn (int num):-pile.remove(num)
remove (int num):-do a bunch of checking-piletotal=piletotal-num
This is in Pile - Remember: responsibilities
12
Notice how you can rely on the objects to call other objects to fulfil responsibilities
Eg. in Player we have void takeTurn(int num){
pile.remove(int num)}With a little extra checking
13
JAVA
14
Java
• Notes 01-08• Basic syntax of classes• Being able to link• Flow of control• We have also done collections (not used in Nim)
15
So, start with Use Case and ‘top level’ classIn main() //can be in Game or in another classGame nim=new Game(val,max,a,b);
nim.playGame();
In Game class elaborate as you go – use methods of THIS class if complicated
public Game (int initial, int max, String p1n, String p2n) { etc….
public void playGame () { while (!gameOver()) { onePlay(); } System.out.println("\n\n***THE WINNER IS***: "+winner()); }
16
But can also pass responsibilities to objects of OTHER classes
public boolean gameOver () {//true if the number of sticks is zero
return pile.getSticks() == 0;
}
public void onePlay () {
// find out how many player wants,
// check and fix if necessary,
// take that many away from the pile17
18
Notice how by referring back to the documentation, we know what objects can do.
If the method at the next level is needed and not there – then write it.
Example in Game:
public void onePlay () { if (!gameOver()) {System.out.println("Player " + currentPlayer.getName() +" how many?"); int numTaken =in.nextInt(); if (numTaken<=0 || numTaken>maxOnATurn) { System.out.println("bad-taking 1“); numTaken=1; } currentplayer.takeTurn(numTaken);etc.
Think about it as a play• With the object diagram as a picture of the
actors• And the actors telling other actors what to do
1919
nimpile p1 p2
pile“fred”
pile“bill”12
:Game
:Pile
:Player
:Player
:NimGame
20
21
A nightclass
• A night class has a teacher and students• We want to be able to administer the list and
the information with a simple java program
22
ANALYSIS - Use Case Diagram
23
- put in initial info-Add students-Remove students-Check information
administrator
• Describes what happens in broad terms • Shows who the users are (sometimes more than one kind)
DESIGN - Diagrams
24
• Object diagram• Class diagram• How do you do the various use-cases
The lines here are NOT the same as the ones we have talked about
(they are ‘refers to’ but they do give some flavour – in class do this properly!)
25
IMPLEMENTATION - java
26
• See the codein the nightclass• Not the readKeyboard() method for Teacher
and Student as an alternative to setting values• Fill in some of the missing parts