26
CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

Embed Size (px)

Citation preview

Page 1: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

CS12230Introduction to ProgrammingLecture 4-x – Consolidation

1

Page 2: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

What have we learned:

• Design • Java

• Rest of these slides 2 examples – • nightclass and nim

2

Page 3: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 4: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

ANALYSIS

4

Page 5: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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)

Page 6: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

DESIGN

6

Page 7: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

Design Tools

• Diagrams: – Object diagram– Class diagram

NEW:– Pseudocode or flow charts

7

Page 8: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 9: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 10: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 11: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 12: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

12

Page 13: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 14: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

JAVA

14

Page 15: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 16: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 17: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 18: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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.

Page 19: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 20: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

20

Page 21: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

21

Page 22: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 23: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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)

Page 24: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

DESIGN - Diagrams

24

• Object diagram• Class diagram• How do you do the various use-cases

Page 25: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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

Page 26: CS12230 Introduction to Programming Lecture 4-x – Consolidation 1

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