29
CS 2511 Fall 2014

CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Embed Size (px)

Citation preview

Page 1: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

CS 2511Fall 2014

Page 2: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Diagramspublic class ClassX extends ClassW {

…public ClassY getY()…private ClassZ localZ;

}

Draw the Class Diagram.

ClassX

ClassW

ClassYClassZ

Page 3: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Binary Heaps

Draw the Binary Heap corresponding to the array above as a tree structure.

2 4 3 8 7 9 14 16 10

1 2 3 4 5 6 7 8 9 10 11 120Array IndexArray Value

2

4 3

8 7 914

16

10

Page 4: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Binary Heaps (cont.)Draw the heap (as a tree structure) after one

priority queue removal.Original: 2

4 3

8 7 914

16

10

4

3

8 7

9

14

16

10

Page 5: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class ExampleSuppose we want to define an interface type

called BankAccount whose values can be objects that have balances like SavingsAccount and CheckingAccount:<<Interface>>

BankAccount

getBalance(): double

SavingsAccount CheckingAccount

Page 6: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)Define the BankAccount interface

public interface BankAccount{

double getBalance();}

Page 7: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)Define the SavingsAccount Class

public class SavingsAccount implements BankAccount{

private double balance;public SavingsAccount(double startingBalance){

balance = startingBalance;}public double getBalance(){

return balance;}

}

Page 8: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)We want to be able to compare bank

accounts on the basis of their balances (use the Comparable<T> interface). Redefine BankAccount to reflect the change.

public interface BankAccount extends Comparable<BankAccount>{

double getBalance();}

Page 9: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)Define the compareTo method that must be

added to the SavingsAccount and CheckingAccount Classes.public int compareTo(BankAccount other) {

if(getBalance() < other.getBalance())return -1;

else if(getBalance() > other.getBalance())return 1;

elsereturn 0;

}

Page 10: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)SavingsAccount and CheckingAccount now

share an identical compareTo method. Draw a modified class diagram that incorporates a class called AbastractAccount that implements BankAccount and from which SavingsAccount and CheckingAccount inherit the compareTo method

Page 11: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)

<<Interface>>BankAccountgetBalance():

double

SavingsAccount CheckingAccount

<<Interface>>Comparable<T>

compareTo(T other): int

AbstractAccount

Page 12: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Class Example (cont.)What Design Pattern does AbstractAccount

exemplify?

Template method

Page 13: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Button Listeners/UML Sequence DiagramsWrite the class definition for the

ButtonListener for the following scenario:There is a button that initially has the text

“Start”When the user clicks the button, the text

should change to “Stop”When the user clicks the “Stop” button, the

text should change to “Start”

Page 14: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Button Listeners/UML Sequence Diagrams (cont.)class ButtonListener implements ActionListener{

private JButton button;public ButtonListener(JButton b) {

button = b;}public void actionPerformed(ActionEvent e) {

if ( button.getText().equals("Start") ) {button.setText("Stop");

}else {

button.setText("Start");}

}}

Page 15: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Button Listeners/UML Sequence Diagrams (cont.)Given the following test class:public class ButtonTest{

public static void main(String[] args){

JButton button = new JButton("Start");button.addActionListener(new ButtonListener(button));JFrame frame = new JFrame(“Button Test");frame.getContentPane().add(button);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(200, 100);frame.show();

}}Draw a UML Sequence Diagram for the Main method

Page 16: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Button Listeners/UML Sequence Diagrams (cont.)

Page 17: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Hash TablesConsider the hash table of capacity 10 below.

Suppose that for integer key k and table capacity c, we define the hash function h(k) = k mod c.

Show how the hash table looks after elements with the following keys are inserted into the table in the following order:99, 161, 2, 44, 88, 54, 23, 84

Show just the keys

Page 18: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Hash Tables (cont.)0123456789 99

1612

44

88

542384

Page 19: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

AnalysisA _____ is/does the following:

Completely defines tasks to be solvedFree from internal contradictionsReadable both by domain experts and software

developersReviewable by diverse interested partiesTestable against reality

Options:Functional SpecificationsUse Case

Page 20: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Design

Page 21: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Implementation

Page 22: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Data StructuresIn an array Implementation of a Binary Heap,

if a node is located at index i of the array A its parent (if any) is located at:A[i-1]A[i-2]A[i*2]A[i/2]

Page 23: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Data StructuresThe time it takes to do an add operation in a

Binary Heap where n is the number of nodes in the heap is:O(n^2)O(n)O(n*log(n))O(log(n))

Page 24: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Data StructuresIn an array Implementation of a Binary Heap,

if a node is located at index i of the array A its left child (if any) is located at:A[i-1]A[i-2]A[i*2]A[i/2]

Page 25: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Data StructuresA disadvantage of using adjacency matrix

representation for a sparse graph is:Memory usage is O(v^2) where v is the

number of verticesMemory usage is O(v+e) where v is the

number of vertices and e is the number of edges

Determining if two edges are connected by an edge takes O(1) time

It is well suited for sparse graphs

Page 26: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Data StructuresWhat structures are used for the following

search types:Breadth-firstDepth-firstBest-first

Queue

Stack

Priority Queue

Page 27: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Data StructuresWhich is a requirement for a heuristic for the

A* search to return an optimal result:Never underestimates the distance to the goalNever overestimates the distance to the goal

Page 28: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Design PatternsWhat design patterns will you use in the

following situations?1.) A change in one object needs to be notified to

other objects.2.) We need to supply different algorithms of a

particular type to a component.3.) Only one object of a class is needed.4.) Allow a composite object made up of primitive

objects to behave similar to how each primitive would behave.

5.) Create different types of objects by overriding one method.

Page 29: CS 2511 Fall 2014. Class Diagrams public class ClassX extends ClassW { … public ClassY getY() … private ClassZ localZ; } Draw the Class Diagram. ClassX

Design Pattern Answers1.) Observer2.) Strategy3.) Singleton4.) Composite5.) Factory Method