cs12230 introduction to programming tying things together

18
CS12230 Introduction to Programming Tying things together

Upload: luca-channon

Post on 14-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS12230 Introduction to Programming Tying things together

CS12230Introduction to Programming

Tying things together

Page 2: CS12230 Introduction 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 …

Page 3: CS12230 Introduction to Programming Tying things together

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

Page 4: CS12230 Introduction to Programming Tying things together

• 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.

Page 5: CS12230 Introduction to Programming Tying things together

Use-case diagram

Final question is – will your system be able to do all these things?

Page 6: CS12230 Introduction to Programming Tying things together

Object Diagram - Library

Page 7: CS12230 Introduction to Programming Tying things together

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

Page 8: CS12230 Introduction to Programming Tying things together

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

Page 9: CS12230 Introduction to Programming Tying things together

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

Page 10: CS12230 Introduction to Programming Tying things together

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) { …}

Page 11: CS12230 Introduction to Programming Tying things together

EXAMPLE – HERE IS USE-CASEThink about a X and O game

-play game()

Page 12: CS12230 Introduction to Programming Tying things together

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

Page 13: CS12230 Introduction to Programming Tying things together

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

Page 14: CS12230 Introduction to Programming Tying things together

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

Page 15: CS12230 Introduction to Programming Tying things together

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

Page 16: CS12230 Introduction to Programming Tying things together

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

Page 17: CS12230 Introduction to Programming Tying things together

End up with:

 

Actually could be private?

Page 18: CS12230 Introduction to Programming Tying things together

Notice how

responsibilities

are passed around