An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 8: Putting a System Together.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Chapter 22 Implementing lists: linked implementations.
Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 5 : Programming By Contract.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
CS12230 Introduction to Programming Lecture 4-x – Consolidation 1.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 14: Sorting and Searching.
CS18000: Problem Solving and Object-Oriented Programming.
CS12230 Introduction to Programming Tying things together.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Getting to know Greenfoot
Case Study Lecture 4 UML Huma Ayub Department of Software Engineering
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Progressively Finite Games
Two examples of Problem Solving in Programming H. Chad Lane University of Pittsburgh CS7: Introduction to Programming.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 0 : Introduction to Object.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Using Reduction for the Game of NIM. At each turn, a player chooses one pile and removes some sticks. The player who takes the last stick wins. Problem:
1 CSE1301 Computer Programming: Lecture 25 Software Engineering 2.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 5 : Programming By Contract.
Great Theoretical Ideas in Computer Science.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 7 : Building a Text-based.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 7 : Building a Text-based.
Ranga Rodrigo. Class is central to object oriented programming.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
M1G Introduction to Programming 2 4. Enhancing a class:Room.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 11 : Modeling with Abstraction.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Composition - 1 J. Sant.. Agenda Define Composition Reuse and Composition. Composition in Java. Composition Exercise with simple containment.
Chapter 9 Searching and Sorting
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Java the UML Way version Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
111 Protocols CS 4311 Wirfs Brock et al., Designing Object-Oriented Software, Prentice Hall, (Chapter 8) Meyer, B., Applying design by contract,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 3: Designing interactive.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 6 : Testing.
An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 13 – Implementing lists:
Chapter 9 Putting together a complete system. This chapter discusses n Designing a complete system. n Overview of the design and implementation process.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
M1G Introduction to Programming 2 5. Completing the program.
Winning Strategies of Games Played with Chips. I got a interesting game Now we show the game P 1 =4 P 2 =6 P 3 =8 Rule 1: Two players.
Class Design I Class Contracts Readings: 2 nd Ed: Section 9.5, Advanced Topic nd Ed: Section 8.5, Advanced Topic 8.2 Some ideas come from: “Practical.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
MMTS Use Case Diagram 1. Choose Number of Players in each Game (2-6) 3. Choose to End Game After n Players Go Bankrupt Software User 4. Choose Option to.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
CS12230 Introduction to Programming Extra example– More on responsibilities 1.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
The need for Programming Languages
More Sophisticated Behavior
Chapter 5 Ordered List.
Agenda Warmup AP Exam Review: Litvin A2
Chapter 4: Writing Classes
Introduction to Programming
Chapter 5 Ordered List.
Outline Software Development Activities
Presentation transcript:

An Introduction to Programming and Object Oriented Design using Java 2 nd Edition. May 2004 Jaime Niño Frederick Hosch Chapter 8: Putting a System Together.

1May 2004NH-Chapter 8 Objectives ñAfter studying this chapter, you should understand the following: ñphases of the software life cycle; ñiterative and compositional nature of the software life cycle; ñfunctional specification of a system; ñidentifying classes, their responsibilities, and relationships among the classes, as fundamental system activities.

2May 2004NH-Chapter 8 Objectives ñAlso, you should be able to: ñdifferentiate between system design and algorithm design; ñidentify potential classes, their responsibilities, and relationships, for a simple problem; ñdraw a static diagram of the design of a simple system; ñimplement a simple system.

3May 2004NH-Chapter 8 Software Life cycle ñThe software life cycle involves ñanalysis; ñspecification; ñdesign; ñimplementation; ñtesting; ñmaintenance.

4May 2004NH-Chapter 8 Software Life cycle ñThe process is ñiterative; ñincremental; ñcompositional.

5May 2004NH-Chapter 8 Design of a System ñGame of “simple nim”: there are two players and a pile of sticks. Each player, in turn, removes one, two, or three sticks from the pile. The player who removes the last stick loses. ñInitial implementation games will be played “computer vs. computer.” ñUser determines whether to play another game and how many sticks to start with.

6May 2004NH-Chapter 8 Functional specification ñBasic function of the system: Play a number of games of “simple nim,” reporting the results to the user.

7May 2004NH-Chapter 8 Functional specification ñSystem functionality: ñAllow user to specify number of sticks at start of game. ñFor each player in turn, determine what play to make (number of sticks to remove) and make the play. ñDisplay state of game after each play: ñnumber of sticks taken, ñnumber of sticks left, ñwhen game is over, who won. ñAllow user to choose to play another game.

8May 2004NH-Chapter 8 Functional specification ñEntering 2 terminates the program. ñEntering 1 produces the following prompt: ñUser interface ñWhen the program is run, the user is offered the following menu: Enter the number denoting the action to perform: Run game Exit Enter choice: Enter number of sticks (a positive integer):

9May 2004NH-Chapter 8 Functional specification ñThe original menu is again displayed. ñUser interface ñA play by play description of game is displayed, similar to the following: Player Player1 takes 1 stick(s), leaving 4. Player Player2 takes 3 stick(s), leaving 1. Player Player1 takes 1 stick(s), leaving 0. Player Player2 wins.

10May 2004NH-Chapter 8 System design ñNeed a model and user-interface components. ñNo data component required.

11May 2004NH-Chapter 8 System design ñIdentifying model objects ñTwo players modeled by class Player. ñPile of sticks modeled by class Pile ñGame, (rules of the game), modeled by class Game.

12May 2004NH-Chapter 8 System Design Responsibilities: do: remove sticks know: number of sticks remaining in the Pile Collaborators Class: Pile a pile of sticks for playing simple nim

13May 2004NH-Chapter 8 System Design Responsibilities: do: make a play by removing sticks from the Pile know: Player’s name the number of sticks removed on this Player’s most recent turn Collaborators Pile Class: Player a player of the simple nim game

14May 2004NH-Chapter 8 System Design Responsibilities: do: conduct a play of game, instructing appropriate Player to take a turn know: the Players the Pile number of sticks that can be taken on a turn which Player plays next which Player played last when the game is over which Player won when game is over Collaborators Players, Pile Class: Game a manager of a simple nim game

15May 2004NH-Chapter 8 System Design Responsibilities: do: allow user to indicate whether or not another game is to be played allow user to specify number of sticks to be used in a game have a game played display each play of a game, when the game is over, and which player has won Collaborators Game Game, Player(s) Class: NimTUI text-based user interface for the simple nim system

16May 2004NH-Chapter 8 Relationship between objects

17May 2004NH-Chapter 8 Case: game not over, player1 turn.

18May 2004NH-Chapter 8 Pile specifications class Pile A pile of sticks for playing simple nim public Pile (int sticks) Create a new Pile, with the specified number of sticks. require: sticks >= 0 public int sticks () The number of sticks remaining in this Pile. ensure: this.sticks() >= 0 public void remove (int number) Reduce the number of sticks by the specified amount. require: number >= 0 and number <= this.sticks() ensure: this.sticks() == old.sticks() - number.

19May 2004NH-Chapter 8 Player specifications class Player A player in the game simple nim. public Player (String name) Create a new Player with the specified name. ensure: this.name().equals(name) public String name () This Player’s name. public int sticksTaken () Number of sticks removed on this Player's most recent turn. Returns 0 if this Player has not yet taken a turn. ensure: this.sticksTaken() >= 0.

20May 2004NH-Chapter 8 Game specifications class Game A game manager in the game simple nim. public Game (Player player1, Player player2, int sticks) Create a nim Game, with specified Players and specified number of sticks. First Player specified ( player1 ) plays first in game. require: sticks > 0.

21May 2004NH-Chapter 8 Game specifications public int sticksLeft () The number of sticks remaining in the pile. ensure: this.sticksLeft() >= 0 public Player nextPlayer () The Player whose turn is next. public Player previousPlayer () The Player who last played; returns null if no play has been made yet. public boolean gameOver () The game is over. public Player winner () winning Player: did not make last play in game. Returns null if game is not over. ensure: if this.gameOver(), this.winner() != this.previousPlayer() public void play () Conduct a play of the game, allowing the appropriate Player to take a turn. Has no effect if the game is over.

22May 2004NH-Chapter 8 User interface specifications ñUser ser interface is a client of the Game and the Players. ñGive user interface creation responsibility for Game, as several games might be played. ñGive user interface creation responsibility for Players since we might want to let user name the Players.

23May 2004NH-Chapter 8 User interface specifications class NimTUI A simple text-based user interface for the simple nim system. public NimTUI () Create a new user interface. public void start () Start the interface.

24May 2004NH-Chapter 8 Initializing class ñThe initiating class will look like this: public class NimGame { public static void main (String[] argv) { (new NimTUI()).start(); } }

25May 2004NH-Chapter 8 Creation responsibilities

26May 2004NH-Chapter 8 Implementing class Pile class Pile { private int sticks; public Pile (int sticks) { this.sticks = sticks; } public int sticks () { return sticks; } public void remove (int number) { assert number <= sticks : "precondition: number <= this.sticks()"; sticks = sticks - number; } public String toString () { return "Pile: " + sticks + " sticks."; }

27May 2004NH-Chapter 8 Test-driven implementation of Player ñStubbed implementation of Player. class Player { public Player (String name) { } public String name () { return null; } public int sticksTaken () { return 0; } public void takeTurn (Pile pile, int maxOnATurn) { }

28May 2004NH-Chapter 8 Test-driven implementation of Player ñTest initial state of the Player. private void testInitialState () { setUp(); verify(player.name().equals("Player"), "name set initially"); verify(player.sticksTaken() == 0, "sticksTaken initially 0"); } private void setUp () { player = new Player("Player"); }

29May 2004NH-Chapter 8 Test-driven implementation of Player  To satisfy initial state test, implement queries name, sticksTaken and constructor. private String name; private int sticksTaken; public Player (String name) { this.name = name; this.sticksTaken = 0; } public String name () { return name; } public int sticksTaken () { return sticksTaken; }

30May 2004NH-Chapter 8 Testing method takeTurn  Method requires two arguments, a Pile and maxOnATurn. ñCases to consider testing:  maxOnATurn is smaller than number of sticks in Pile;  maxOnATurn is equal to the number of sticks in Pile;  maxOnATurn is larger than number of sticks in Pile.  Test boundaries of Pile size and maxOnATurn.

31May 2004NH-Chapter 8 Testing method takeTurn ñwe’ll test the following cases: Pile sizemaxOnATurn

32May 2004NH-Chapter 8 Testing method takeTurn ñInclude four Piles in the test fixture: private Player player; private Pile pile5;// Pile with 5 sticks private Pile pile3;// Pile with 3 sticks private Pile pile2;// Pile with 2 sticks private Pile pile1;// Pile with 1 stick private void setUp () { player = new Player("Player"); pile5 = new Pile(5); pile3 = new Pile(3); pile2 = new Pile(2); pile1 = new Pile(1); }

33May 2004NH-Chapter 8 Testing method takeTurn /** * Test the takeTurn method with maxOnATurn 1. */ private void testTakeTurnMax1 () { player.takeTurn(pile5,1); verify(pile5.sticks() == 4, "takeTurn size 5, max 1"); verify(player.sticksTaken() == 1, "sticksTaken size 5, max 1"); player.takeTurn(pile1,1); verify(pile1.sticks() == 0, "takeTurn size 1, max 1"); verify(player.sticksTaken() == 1, "sticksTaken size 1, max 1"); }

34May 2004NH-Chapter 8 Testing method takeTurn /** * Test the takeTurn method with maxOnATurn 3. */ private void testTakeTurnMax3 () { player.takeTurn(pile5,3); verify(1 <= player.sticksTaken() && player.sticksTaken() <= 3,"sticksTaken size 5, max 3"); verify(pile5.sticks() == 5 - player.sticksTaken(), "takeTurn size 5, max 3"); player.takeTurn(pile3,3); verify(1 <= player.sticksTaken() && player.sticksTaken() <= 3, "sticksTaken size 3, max 3"); verify(pile3.sticks() == 3 - player.sticksTaken(), "takeTurn size 3, max 3"); player.takeTurn(pile2,3); verify(1 <= player.sticksTaken() && player.sticksTaken() <= 2, "sticksTaken size 2, max 3"); verify(pile2.sticks() == 2 - player.sticksTaken(), "takeTurn size 2, max 3"); player.takeTurn(pile1,3); verify(player.sticksTaken() == 1,"sticksTaken size 1, max 3"); verify(pile1.sticks()== 0, "takeTurn size 1, max 3"); }

35May 2004NH-Chapter 8 Testing method takeTurn ñSimplest implementation of the method : always remove one stick from the Pile. public void takeTurn (Pile pile, int maxOnATurn) { pile.remove(1); sticksTaken = 1; }

36May 2004NH-Chapter 8 Implementing class Game  Implementation easily follows from the specs: class Game { private static final int MAX_ON_A_TURN = 3; private Player player1; private Player player2; private Player nextPlayer; private Player previousPlayer; private Pile pile;

37May 2004NH-Chapter 8 Implementing class Game public Game (Player player1, Player player2, int sticks) { assert sticks > 0 : "precondition: initial sticks > 0"; this.player1 = player1; this.player2 = player2; this.nextPlayer = player1; this.previousPlayer = null; this.pile = new Pile(sticks); }

38May 2004NH-Chapter 8 Implementing class Game public int sticksLeft () { return pile.sticks(); } public Player nextPlayer () { return nextPlayer; } public Player previousPlayer () { return previousPlayer; } public boolean gameOver () { return pile.sticks() == 0; } public Player winner () { if (gameOver()) return otherPlayer(previousPlayer); else return null; }

39May 2004NH-Chapter 8 Implementing class Game public void play () { if (!gameOver()) { nextPlayer.takeTurn(pile,MAX_ON_A_TURN); previousPlayer = nextPlayer; nextPlayer = otherPlayer(nextPlayer); } public String toString () { return "Game with players: " + player1 + ", and “ + player2; } private Player otherPlayer (Player player) { if (player == player1) return player2; else return player1; } }//end of Game implementation

40May 2004NH-Chapter 8 Implementing the TUI ñThe implementation is similar to those seen in chapter 7. ñThe actual implementation is shown in the textbook in chapter 8.

41May 2004NH-Chapter 8 Summary ñPut together a complete, simple system. ñConsidered the life cycle of a system: ñproblem analysis, ñspecification, ñdesign, ñimplementation, ñtesting, and ñmaintenance.

42May 2004NH-Chapter 8 Summary ñLifecycle: Not a series of sequential steps. ñProcess is ñiterative, ñincremental, ñcompositional.

43May 2004NH-Chapter 8 Summary ñDesigned and implemented a system to play the simple nim game. ñSystem design involved ñidentifying classes, ñassigning responsibilities to the classes, ñdetermining fundamental relationships between classes, ñ writing detailed class specifications.

44May 2004NH-Chapter 8 Summary ñDuring implementation noticed that the it was quite straightforward given the system specification. ñImplementing the Player class gave us an opportunity to see another example of test-driven design. ñThe user interface we built was a simple, text-based interface, similar to what we’ve seen before.