Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS12230 Introduction to Programming Tying things together.

Similar presentations


Presentation on theme: "CS12230 Introduction to Programming Tying things together."— Presentation transcript:

1 CS12230 Introduction to Programming Tying things together

2 What do you do when faced with a blank page? A ‘methodology’ is a way of working that you can follow Simple methodology …

3 START PROGRAM Design PhaseImplementation Phase Test with diagrams and on paper against USE CASES to see if it should work Figure out basic classes And algorithms OBJECT AND CLASS DIAGS and PSEUDOCODE Define problem USE CASE Code into program Test against USE CASEs To see if it does all it should Requirements Phase

4 Understand the problem. Try sketching out how you would solve it without a computer What are the objects and classes in the problem? What information do you have about them – attributes? What do they do (their responsibilities) – methods? Code a couple of classes – even simple stuff will get you started. When stuck put in a comment like //this is where I sort things - don’t know how yet Think about the main application again – how can you use your classes? Test out your idea by running through an example.

5 Use-case diagram Final question is – will your system be able to do all these things?

6 Object Diagram - Library

7 Class Diagram Book - String title -String author -String isbn -Person checkedOutTo +Book(String t, String a, String code) +checkout(Person p); Library - Person[] customers -Book[] books Person - String name -Book[] booksOut +Person (String n) +addBook(Book b) 0..* 0..1

8 Now the trickiest part –can you trace through the use cases to see that your system can do them? :Person :Book :Library List of Books List of Person

9 Pseudocode – then code To check out a book (you know the person’s name and the book’s call number): Find theCustomer in customer list using name Find theBook in book list using call number theCustomer.addBook(theBook) theBook.setCheckedOutTo(theCustomer) } If you don’t have these methods then you must add them

10 Turning to java is relatively easy! public class Book{ private String isbn; private Person checkedOutTo; //etc void setCheckedOutTo(Person p) { checkedOutTo = p; } //etc } public class Person{ private String name; private ArrayList booksOut; //etc void addBook(Book b) { booksOut.add(b); } //etc } public class Library{ private ArrayList customers; private ArrayList books; void checkout(String custName, String isbn) { … }

11 EXAMPLE – HERE IS USE-CASE Think about a X and O game -play game()

12 Classes: Game, Board, Player What do they ‘have’? Game has 2 players and a Board Player has a symbol and a link to theBoard Board has a 3x3 grid of characters What do they obviously ‘do’? Game plays the game > Player takes a turn by placing a piece Board displays

13 Player -String name -char symbol -Board board +takeTurn() Board -char grid[3][3] + toString() + ????? Game - Player p1,p2 -Board board +play() 1..1 2..2 1..1

14 How do we play the game then? (this was the only use case) While (not 9 moves) { player1.takeTurn() if (board.winner()) break; player2.takeTurn() if (board.winner()) break; } this method in Player this one in Board

15 Investigate takeTurn() further Let them enter a row and column If (board.valueAt(row,col)==‘ ‘) board.newMove(row, col, symbol) So need these 2 methods in Board

16 So, 3 new methods of Board to investigate winner(): 3 ways of winning (tedious and tricky so might code each with a private method) valueAt() newMove() Also, toString() to display

17 End up with: Actually could be private?

18 Notice how responsibilities are passed around


Download ppt "CS12230 Introduction to Programming Tying things together."

Similar presentations


Ads by Google