CS12230Introduction to Programming
Tying things together
What do you do when faced with a blank page?
• A ‘methodology’ is a way of working that you can follow
• Simple methodology …
START
PROGRAM
Design Phase Implementation Phase
Test with diagrams and on paper against USE CASES to see if it should work
Figure out basic classesAnd algorithms OBJECT AND CLASS DIAGS and PSEUDOCODE
Define problem USE CASE
Code into program
Test against USE CASEsTo see if it does all it should
Requirements Phase
• 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.
Use-case diagram
Final question is – will your system be able to do all these things?
Object Diagram - Library
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..*
0..*
0..1
Now the trickiest part –can you trace through the use cases to see that your
system can do them?
:Person :Book
:LibraryList of Books
List of Person
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 nameFind theBook in book list using call numbertheCustomer.addBook(theBook)theBook.setCheckedOutTo(theCustomer)}
If you don’t have these methods then you must add them
Turning to java is relatively easy!
public class Book{private String isbn; private Person checkedOutTo; //etcvoid setCheckedOutTo(Person p) {
checkedOutTo = p;}
//etc}
public class Person{private String name; private ArrayList<Book> booksOut; //etcvoid addBook(Book b) {
booksOut.add(b);}
//etc}
public class Library{private ArrayList<Person> customers;private ArrayList<Book> books;void checkout(String custName, String isbn) { …}
EXAMPLE – HERE IS USE-CASEThink about a X and O game
-play game()
Classes: Game, Board, Player What do they ‘have’?
Game has 2 players and a BoardPlayer has a symbol and a link to theBoardBoard has a 3x3 grid of characters
What do they obviously ‘do’?Game plays the game <<will need more here>>Player takes a turn by placing a pieceBoard displays
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
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
Investigate takeTurn() further
Let them enter a row and columnIf (board.valueAt(row,col)==‘ ‘)
board.newMove(row, col, symbol)
So need these 2 methods in Board
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
End up with:
Actually could be private?
Notice how
responsibilities
are passed around
Top Related