Академический Документы
Профессиональный Документы
Культура Документы
@Test
public void testBabyDilloCanShelter() {
assertFalse(babyDillo.canShelter(babyDillo));
}
@Test
public void testHugeDeadDilloCanShelter() {
assertTrue(babyDillo.canShelter(hugeDeadDillo));
}
@Test
public void testEdgeCaseDilloCanShelter() {
assertFalse(babyDillo.canShelter(edgeCaseDillo));
}
assertEquals(4,4);
@Test
public void testMyBoaAnotherBoaLikesSameFood() {
assertFalse(myBoa.likesSameFood(anotherBoa);
}
@Test
public void testMyBoaAnotherBoaLikesSameFood() {
assertTrue(myBoa.likesSameFood(myBoa);
}
overloading (have more than one constructor for a class) or overloaded constructor
-can just rearrange parameters of constructor
reqHamlet.book // Book
hamlet // Book
LinkedList<Book> books;
LONGER way to count (can count num of books with given title)
int numOfBooks = 0;
//accumulator
for(book aBook : this.books) {
if (book title matches my title) {
numOfBooks++;
}
}
return numOfBooks;
//overloading (method has same name but different parameters, usually within same class)
public Book checkOut(String title) {
return null;
}
@Before
public void setUp() {
lib = new Library();
lib.addBook(hamlet).addBook(alice).addBook(harryPotter);
}
@Test
public void testCheckedOut() {
LinkedList<Book> result = new LinkedList<Book>();
hamlet.isAvailable = false;
alice.isAvailable = false;
result.add(hamlet);
result.add(alice);
assertEquals(result, lib.checkedOut());
}
checkOut take in linked list of CartItems and return double for price
}
class ItemType {
LinkedList<CartItem> typeOfItem;
ItemType() {
this.typeOfItem = new LinkedList<CartItem>();
}
}
———————————
for (
-create team class with name, linked list of employees, total sick days
-sort employees into teams
-if team name matches any in the list of teams, add employee and its sick days to team
-else create new team and add employee
- using index based for loop, add team to right place in list (.add(int index, E element))
-return linked list of teams names
An abstract data type is a data abstraction where the implementation is hidden behind a set of operations.
The operations are precisely specified independent of any particular implementation.
interface ISet {
ISet addElt(int elt);
ISet removeElt (int elt);
boolean hasElt (int elt);
int size();
}
add 12, add 4, add 12, add 12, add 9 -> list (9, 4, 12)
Most efficient if median is root of tree and root of every subtree is the median of the subtree
Heap
A heap is a binary tree (NOT a BST) such that the smallest item is the root of the tree and left and right
subtrees are heaps
Priority Queue
remove minimum, creates two sub-heaps
find new min, now have 3 heaps
merge two smallest heaps
Encapsulation Rule
Don’t ask for the information you need to do the work. Ask the object that has the information to do the
work for you.
if (cust.checkPassword(withPwd))
General Rule: all FIELDS are private, all METHODS are public
Hashing
Desire
We want to store objects in some structure and be able to retrieve them extremely quickly
the number of items to store might be big
Why?
reduce access time dow to nearly O(1)
Idea
shrink the address space to fit the population size
HashMap<key, value>
HashMap<String, LinkedList<Contact>)
.get(string) and .put(aContact.name, aContact)
What a Hash Code does: takes an index value and turns it into a hash value?