Download presentation
Presentation is loading. Please wait.
Published byJoslyn Jarvis Modified over 10 years ago
1
CS12230 Introduction to Programming Lecture 4-x – Consolidation 1
2
What have we learned: Design Java Rest of these slides 2 examples – nightclass and nim 2
3
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 left player 2 takes 3 - 1 left player 1 has to take the last one and loses (See wikipedia for more interesting versions) 3
4
ANALYSIS 4
5
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)
6
DESIGN 6
7
Design Tools Diagrams: – Object diagram – Class diagram NEW: – Pseudocode or flow charts 7
8
Object Diagram 8 nim pile p1 p2 pile “fred” pile “bill” 12 :Game :Pile :Player :NimGame Could amalgamate Game and NimGame by putting main() in Game A snapshot of a program at some time – different at different times
9
Class Diagram 9 Application Pile -int numsticks Player -String name -Pile pile Game -Player p1, p2 -Pile pile 1..1 2..2 1..1 A static description of the kinds of things (classes) in a program
10
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
11
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
12
13
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
14
JAVA 14
15
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
16
So, start with Use Case and ‘top level’ class In main() //can be in Game or in another class Game 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
17
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 pile 17
18
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 maxOnATurn) { System.out.println("bad-taking 1“); numTaken=1; } currentplayer.takeTurn(numTaken); etc.
19
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 19 nim pile p1 p2 pile “fred” pile “bill” 12 :Game :Pile :Player :NimGame
20
20
21
21
22
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
23
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)
24
DESIGN - Diagrams 24 Object diagram Class diagram How do you do the various use-cases
25
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
26
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.