CS 2511

advertisement
CS 2511
Class Diagrams
public class ClassX extends ClassW {
…
public ClassY getY()
…
private ClassZ localZ;
}
ClassW
ClassX
Draw the Class Diagram.
ClassY
ClassZ
Binary Heaps
Array Index
Array Value
0
1
2
3
4
5
6
7
8
9
2
4
3
8
7
9
14
16
10
10
11
12
Draw the Binary Heap corresponding to the array above
as a tree structure.
2
3
4
8
16
7
10
9
14
Binary Heaps (cont.)
Draw the heap (as a tree structure) after one priority
queue removal.
2
Original:
3
4
3
8
16
7
9
14
4
10
8
16
9
7
10
14
Class Example
Suppose 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
Class Example (cont.)
Define the BankAccount interface
public interface BankAccount
{
double getBalance();
}
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;
}
}
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();
}
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;
else
return 0;
}
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
Class Example (cont.)
<<Interface>>
Comparable<T>
compareTo(T other): int
<<Interface>>
BankAccount
getBalance(): double
AbstractAccount
SavingsAccount
CheckingAccount
Class Example (cont.)
What Design Pattern does AbstractAccount
exemplify?
Template method
Button Listeners/UML Sequence
Diagrams
Write 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”
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");
}
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
Button Listeners/UML Sequence
Diagrams (cont.)
Hash Tables
Consider 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
Hash Tables (cont.)
0
1
2
3
4
5
6
7
8
9
161
2
23
84
88
99
54
44
Analysis
A _____ is/does the following:
 Completely defines tasks to be solved
 Free from internal contradictions
 Readable both by domain experts and software
developers
 Reviewable by diverse interested parties
 Testable against reality
Options:
 Functional Specifications
 Use Case
Data Structures
In 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]
Data Structures
The 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))
Data Structures
In 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]
Data Structures
A disadvantage of using adjacency matrix
representation for a sparse graph is:
 Memory usage is O(v^2) where v is the number of
vertices
 Memory 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
Data Structures
What structures are used for the following search
types:
 Breadth-first
 Depth-first
 Best-first
Queue
Stack
Priority Queue
Data Structures
Which is a requirement for a heuristic for the A*
search to return an optimal result:
 Never underestimates the distance to the goal
 Never overestimates the distance to the goal
Design Patterns
 What 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.
Design Pattern Answers
1.) Observer
2.) Strategy
3.) Singleton
4.) Composite
5.) Factory Method
Download