Date post: | 14-Dec-2015 |
Category: |
Documents |
Upload: | maleah-bodily |
View: | 215 times |
Download: | 1 times |
CS 2511Fall 2014
Class Diagramspublic class ClassX extends ClassW {
…public ClassY getY()…private ClassZ localZ;
}
Draw the Class Diagram.
ClassX
ClassW
ClassYClassZ
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
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
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
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;
elsereturn 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>>BankAccountgetBalance():
double
SavingsAccount CheckingAccount
<<Interface>>Comparable<T>
compareTo(T other): int
AbstractAccount
Class Example (cont.)What Design Pattern does AbstractAccount
exemplify?
Template method
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”
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 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
Hash Tables (cont.)0123456789 99
1612
44
88
542384
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
Design
Implementation
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]
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))
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]
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
Data StructuresWhat structures are used for the following
search types:Breadth-firstDepth-firstBest-first
Queue
Stack
Priority Queue
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
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.
Design Pattern Answers1.) Observer2.) Strategy3.) Singleton4.) Composite5.) Factory Method