CS2 - PowerPoint PPT Presentation

1 / 72
About This Presentation
Title:

CS2

Description:

A friend owns a restaurant. ... Let's go back and look at the menu ... So our nouns were: program, maitre d, restaurant, menu, customer, choices, list. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 73
Provided by: ccGa
Category:
Tags: cs2

less

Transcript and Presenter's Notes

Title: CS2


1
CS2
  • Module 32
  • Category OO Concepts
  • Topic Design
  • Objectives
  • Some basic design concepts

2
CS 2
  • Introduction to
  • Object Oriented Programming
  • Module 32
  • OO Concepts
  • Design

3
Some Guidelines
  • Make all fields private and consistently use
    accessors and modifiers.
  • Any variable that "can" be a method variable
    should be a method variable.
  • Common error Making instance variables or class
    variables of items that should be (temporary)
    method variables.
  • All classes should have
  • toString
  • test main which tests all methods (if possible).
  • DEBUG constant

4
Some Guidelines
  • Initialize appropriate instance variables in the
    constructor.
  • Initialize static (class) variables when
    declared.
  • Applications that require a method call to
    startup should do so from a main method and not a
    constructor.
  • Rule of thumb Constructors should be "fast",
    initialize things and exit.

5
Questions?
6
Basic Design
  • Software design is an art not a science
  • It is best learned by experience hopefully with
    some expert guidance
  • This lecture will not make you an expert
  • The goal is to give you a simple basic structure
    to get you started in the process.
  • No matter what your major you will take a number
    of design courses before you leave Tech.

7
Simple Steps
  • 1. Understand the problem
  • Problem statement
  • Discussion with clients
  • 2. Develop cases or scenarios
  • Functions that your program must perform
  • Might be menu items
  • Might be events which trigger action
  • 3. Plain English
  • Describe situation
  • Describe scenarios

8
Simple Steps
  • 4. Apply Object Oriented thinking
  • Analyze the plain English
  • Nouns - Objects
  • Verbs - Methods
  • Imagine the structure
  • "has a" relationships (fields)
  • "is a" hierarchical structures
  • Consider communication
  • Who has to communicate with whom?
  • Who has to know about what?

9
Simple Steps
  • 5. Put the design on paper (or the wall)
  • Indicate classes
  • Their methods
  • Their fields
  • Who they need to talk to
  • Indicate hierarchies
  • 6. Run the cases or scenarios against the design
  • Modify, modify, modify
  • 7. Code a class at a time
  • USE A TEST/DEBUG main IN EACH
  • Start at the bottom
  • Compile test frequently

10
An Example
  • A friend owns a restaurant. He knows that youre
    a Java wizard and he asks for your help with a
    little problem.
  • His business is very good and he is so busy that
    his maitre d has trouble keeping track of people
    waiting for tables.
  • He would like a program that would manage the
    various lists of people waiting. These include
  • Waiting for a table in smoking
  • Waiting for a table in non-smoking
  • Waiting for a very big table
  • etc.
  • Since different maitre ds may use different
    techniques he would like something very simple
    and very flexible.

11
An Example
  • After some discussion with your friend you reach
    the following statement of program function
  • The program will display a menu with the
    following choices
  • Create a new list
  • Add a customer to a list
  • Seat a customer from a list
  • Quit
  • This seems like a good starting point although
    you keep in the back of your mind the fact that
    most people will realize they want any given
    program to do more once they see it in operation.

12
Designing a Solution
  • So now you have accomplished the first step
  • 1. Understand the problem
  • Problem statement
  • Discussion with clients
  • Now lets look at the next step
  • 2. Develop cases or scenarios
  • Functions that your program must perform
  • Might be menu items
  • Might be events which trigger action

13
Designing a Solution
  • Lets go back and look at the menu
  • The program will display a menu for the maitre d
    with the following choices
  • Add a new list
  • Add a customer to a list
  • Seat a customer from a list

14
Designing a Solution
  • This seems like a pretty good description of the
    scenarios we need to be able to perform. So this
    will serve as a start for step 3 also
  • 3. Plain English
  • Describe situation
  • Describe scenarios

15
Designing a Solution
  • We might consider adding some additional detail
    and an extra scenario
  • The program will be run by the maitre d of a
    restaurant
  • The program will startup
  • The program will display a menu with the
    following choices
  • Create a new list
  • Add a customer to a list
  • Seat a customer from a list
  • Quit

16
Designing a Solution
  • We might want to consider sketching some screens

MAITRE D Enter 1. To create a new list 2. To
add a customer to a list 3. To seat a customer
from a list 4. To quit Choice 1
17
Designing a Solution
  • We might want to consider sketching some screens

MAITRE D Create a New List Enter the name for
the new list Non-smoking
18
Designing a Solution
  • We might want to consider sketching some screens

MAITRE D Create a New List Enter the name for
the new list Smoking
19
Designing a Solution
  • We might want to consider sketching some screens

MAITRE D Create a New List Enter the name for
the new list Big group
20
Designing a Solution
  • We might want to consider sketching some screens

MAITRE D Add a Customer to a List Enter 1.
For Non-smoking 2. For Smoking 3. For Big
group Choice 2 Enter Customer Smith
21
Designing a Solution
  • We might want to consider sketching some screens

MAITRE D Seat a Customer from a List Enter 1.
For Non-smoking 2. For Smoking 3. For Big
group Choice 1 You may now seat the Jones
party. Press Enter when done. ltENTERgt
22
OO Thinking
  • Lets do the first part of step 4
  • 4. Apply Object Oriented thinking
  • Analyze the plain English
  • Nouns - Objects
  • Verbs - Methods
  • Imagine the structure
  • "has a" relationships (fields)
  • "is a" hierarchical structures
  • Consider communication
  • Who has to communicate with whom?
  • Who has to know about what?

23
Designing a Solution
  • Now lets go back and look at our program
    information
  • The program will be run by the maitre d of a
    restaurant
  • The program will startup
  • The program will display a menu with the
    following choices
  • Create a new list
  • Add a customer to a list
  • Seat a customer from a list
  • Quit
  • Lets mark nouns in blue.

24
Designing a Solution
  • Now lets go back and look at our program
    information
  • The program will be run by the maitre d of a
    restaurant
  • The program will startup
  • The program will display a menu with the
    following choices
  • Create a new list
  • Add a customer to a list
  • Seat a customer from a list
  • Quit
  • Lets mark nouns in blue.
  • Lets mark verbs in red

25
Designing a Solution
  • Now lets go back and look at our program
    information
  • The program will be run by the maitre d of a
    restaurant
  • The program will startup
  • The program will display a menu with the
    following choices
  • Create a new list
  • Add a customer to a list
  • Seat a customer from a list
  • Quit
  • Lets mark nouns in blue.
  • Lets mark verbs in red

26
OO Thinking
  • Now for the remainder well need an additional
    tool
  • 4. Apply Object Oriented thinking
  • ...
  • Imagine the structure
  • "has a" relationships (fields)
  • "is a" hierarchical structures
  • Consider communication
  • Who has to communicate with whom?
  • Who has to know about what?

27
Design Technique
  • The basic building block on Object Oriented
    Programming is the class so lets use some kind
    of symbol for a class. This scheme could be used
    on a big piece of paper, 3 x 5 cards or even a
    whiteboard.

28
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

Certainly were going to have some kind of list.
29
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

In fact, if we think about it we might want it to
be like a Queue???
30
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

Deep in our heart we know that a Queue is
somehow going to require nodes
31
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

Well put the Node under the list and come back
to it later
32
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

Were also pretty sure that we need some customers
33
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

And perhaps a menu?
34
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Well select some likely candidates for classes
  • Note There is no right/wrong for this step i.e.
    use pencil!

Maybe well include the Maitre d (Imagine him
holding a clipboard?)
35
Design Technique
  • So our nouns were program, maitre d, restaurant,
    menu, customer, choices, list.
  • Remember, we can always go back and add things.
    At this point we are trying to see the big
    picture...

36
Design Technique
  • Now lets think about some of the things that
    these objects would contain...

37
Design Technique
  • Now lets think about some of the things that
    these objects would contain...

38
Design Technique
  • Now lets think about some of the things that
    these objects would contain...

39
Design Technique
  • Now lets think about some of the things that
    these objects would contain...

40
Design Technique
  • Now look at the fields and also remember the
    verbs run, startup, display, create, add, seat,
    quit.

41
Design Technique
  • Now stop and think about structure
  • Well have lists of Nodes, the Nodes will contain
    Customers, the MaitreD will have perhaps an array
    of lists. Make sense?
  • The Menu class...will instances be different
    menus? Does it need to be a class? Could it just
    be methods in MaitreD?

42
Design Technique
  • Lets consider some scenarios...
  • Startup We might have a small Driver class or
    perhaps just let MaitrD contain the startup main
    java MaitreD

(main)
43
Design Technique
  • Lets consider some scenarios...
  • Startup We would simply have to have a looping
    method that displayed a menu and used a switch to
    do the right thing.

(main)
44
Design Technique
  • Lets consider some scenarios...
  • Create Get the name (maybe the list needs a
    name), make a list object, put it into the array
    of lists. No problem?

(main)
45
Design Technique
  • Lets consider some scenarios...
  • Add Loop through the list array and show the
    list names . Get a choice, then get a customer
    name, create a customer object enqueue it.
    Hmmm, maybe this separate Menu class is not
    helping?

(main)
46
Design Technique
  • Lets consider some scenarios...
  • Note The objective here is not to be pretty!

Class Menu
(main)
Fields choice
Comm
Methods printMenu getChoice
47
Design Technique
  • Okay, maybe neat?

Class MaitreD
(main)
Fields array? of lists
Methods create add seat quit printMenu getChoice

Comm
48
Design Technique
  • Okay, maybe neat?

Class MaitreD
(main)
Fields array? of lists
Methods create add seat quit printMenu getChoice

Comm
49
Design Technique
  • The other scenario is when we wish to seat a
    customer. We assume that a table is now available
    that corresponds to a given list and we simply
    dequeue the head of that list and print out that
    name.

Class MaitreD
(main)
Fields array? of lists
Methods create add seat quit printMenu getChoice

Comm
50
Design Technique
  • What about these "Comm" areas?
  • In this case its pretty straightforward. The
    object/class that creates another object will be
    the one communicating with it so this is simple.
    We would use these for example if a customer
    needed to know about which list he was in.

51
Time for Coding?
  • Stack up the cards or otherwise try to establish
    a bottom up priority for coding.

52
Time for Coding?
  • class Customer
  • private String strName
  • public Customer(String strName)
  • setName(strName)
  • public void setName(String s)
  • strName s
  • public String getName()
  • return strName

53
Time for Coding?
  • // class Customer (continued)
  • public static toString()
  • return getName()
  • public static void main(String args)
  • Customer c1, c2
  • c1 new Customer("Smith")
  • c2 new Customer("Jones")
  • System.out.println(c1 "," c2)
  • c1.setName("Fred")
  • System.out.println(c1.getName())
  • // customer

54
Time to Code
  • Our Node could be specific to this problem.
  • Or we could use the linked list Node (and perhaps
    even the DataNode) we already discussed.
  • Why do we need a special node for a customer?
  • So far we dont.
  • But experience says that perhaps well be adding
    additional information about each customer?

55
Time to Code
  • Our List which we recognize as a Queue type data
    structure could be implemented in several
    different ways
  • We could use an existing Java class such as
    Vector which well discuss in the near future.
  • We could write one of our own.
  • Typical solution in a beginning programming class

56
  • class List
  • private Node head
  • private Node tail
  • private String name
  • public List(String name)
  • setName(name)
  • setHead(null)
  • setTail(null)
  • public void setHead(Node head)
  • this.head head
  • public Node getHead()
  • return head

57
  • // class List (continued)
  • public void setTail(Node tail)
  • this.tail tail
  • public Node getTail()
  • return tail
  • public String getName()
  • return name
  • public void setName(String name)
  • this.name name

58
  • // class List (continued)
  • public boolean isEmpty()
  • return (getHead null)
  • public void add(Object obj)
  • if (isEmpty())
  • setHead(new Node(obj))
  • setTail(getHead())
  • else // add to end
  • getTail().setNext(new Node(o))
  • setTail(getTail().getNext())
  • // add

59
  • // class List (continued)
  • public Object remove()
  • Object retVal
  • if (isEmpty())
  • retVal null
  • else
  • retVal getHead().getData()
  • if(getHead() getTail())
  • setHead(null)
  • setTail(null)
  • else
  • setHead(getHead().getNext())
  • return retVal
  • // remove

60
  • // class List (continued)
  • public String toString()
  • if (isEmpty())
  • return "Empty"
  • else // call the Node classs toString()
  • return getHead().toString()
  • public static void main(String args)
  • List ell new List("TestList")
  • ell.add("Item 1")
  • ell.add("Item 2")
  • System.out.println(ell)
  • System.out.println(ell.remove())
  • System.out.println(ell.remove())
  • if(ell.remove() null)
  • System.out.println("null test passed")
  • // main
  • // List

61
Time to Code
  • Now the method that will get sent to java at
    startup.
  • Lets review what it needs to do
  • Hold a reference to an array of lists
  • Constructor
  • Make the array to hold lists
  • MenuLoop
  • do
  • Print menu
  • Get Choice
  • Switch
  • while (not quit)
  • Main
  • Instantiate
  • Call MenuLoop

62
  • public class MaitreD
  • private List lists
  • int listCount
  • public MaitreD()
  • list new ListMAXARRAY
  • listCount 0
  • // constructor

63
  • // class MaitreD (continued)
  • public void menuLoop()
  • do
  • choice printMenu1()
  • switch(choice)
  • case CREATE
  • create()
  • break
  • case ADD
  • add()
  • break
  • case SEAT
  • seat()
  • break
  • case QUIT
  • break
  • default
  • break

64
  • // MaitreD (continued)
  • private int printMenu1()
  • System.out.print(MENU1)
  • return Integer.parseInt(IOHelper.readLine())
  • // printMenu1
  • private void create()
  • System.out.print(MENU2)
  • listCount
  • if(listCount lt MAXARRAY)
  • listslistCount - 1
  • new List(IOHelper.readLine())
  • else // cannot add the list
  • System.out.println(
  • "Maximum number of lists exceeded -- "

65
  • // MaitreD (continued)
  • private void add()
  • int choice
  • System.out.println(MENU3)
  • for (int i0 i lt listCount i)
  • System.out.print(" " (i1) MENU4)
  • System.out.println(listsi.getListName())
  • System.out.print(MENU5)
  • choice Integer.parseInt(IOHelper.readLine())
  • // Should put error handler here to insure
    valid
  • // list number
  • System.out.print(MENU6)
  • listschoice.add(newCustomer(IOHelper.readLine
    ()))
  • // add

66
  • // MaitreD (continued)
  • private void seat()
  • int choice
  • System.out.println(MENU7)
  • for(int i0 i lt listCount i)
  • System.out.println(" " (i1) MENU4
    listsi.getListName())
  • System.out.print(MENU5)
  • choice Integer.parseInt(IOHelper.readLine())
  • // Would put error handler here to insure valid
  • // list number
  • System.out.println(
  • MENU8 listschoice.remove() MENU9")
  • IOHelper.readLine() // Wait for "Enter"

67
  • // MaitreD (continued)
  • public static void main(String args)
  • // a little testing main
  • MaitreD m new MaitreD()
  • m.menuLoop()

68
  • interface MaitreDConstants
  • public static final int MAXARRAY 20
  • public final static String MENU1
  • "MAITRE D\n\nEnter\n
  • "1. To create a new list\n
  • "2. To add a customer to a list\n
  • "3. To seat a customer from a list\n
  • "4. To quit\n\n
  • "Choice "
  • public final static String MENU2
  • "MAITRE D Create a New List\n\n
  • "Enter the name for the new list "

69
  • // MaitreDConstants (continued)
  • public final static String MENU3
  • "MAITRE D Add a Customer to a List\n\n"
  • "Enter "
  • public final static String MENU4 " For "
  • public final static String MENU5 "Choice "
  • public final static String MENU6
  • "Enter Customer "
  • public final static String MENU7
  • "MAITRE D Seat a Customer from a List\n
  • "Enter\n"
  • public final static String MENU8
  • "You may now seat the "

70
Summary
  • Design before coding.
  • Think OO
  • Nouns,
  • verbs,
  • communication.
  • Code incrementally
  • Test incrementally!
  • Use test mains

71
Questions?
72
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com