Data Structures
Queue Methods (5)
1) add() 2) remove() 3) poll() 4) element() 5) peek()
For LinkedList<String> wordsFromFile = new LinkedList<String>(); ["The", "fowl", "is", "the", "term"] At what index does the following statement insert the word "end"? Enter a number. wordsFromFile.add("end");
5 (if index isn't specified, add() adds to the end of the list)
For LinkedList<String> wordsFromFile = new LinkedList<String>(); ["The", "fowl", "is", "the", "term"] How many elements does wordsFromFile contain after the following statement? Enter a number. wordsFromFile.add(4, "big");
6 (The add() method increases the list's size by one because add() does not replace elements.)
What are the advantages of using a linked implementation as opposed to an array implementation?
A <term> allocates space only as it is needed and has a theoretical limit on the size of the hardware
What is the difference between a queue and a stack?
A <term> is FIFO, whereas a <term> is LIFO
Concrete Class
A class that is not abstract, and hence can be instantiated
Inheritance
A form of software reuse in which a new class is created by absorbing an existing class's members and extending them with new or modified capabilities.
Generic method
A method definition having a special type parameter that may be used in place of types in the method
What is a data type?
A set of values and operations on those values defined within a programming language
Shape is what kind of class?
Abstract
What are the basic methods for accessing an indexed list?
Accessing an index Accessing the ends Accessing a particular value
What are the additional operations required of implementations that are part of the Java Collections API framework?
All Java Collections API framework classes implement the Collections interface, the Serializable interface, and the Cloneable interface
How do object references help us define data structures?
An <term> can be used as a link from one object to another. A group of linked objects can form a <term>, such as a linked list, on which a collection can be based
What is the difference between an indexed list, an ordered list, and an unordered list?
An <term> is a collection of objects with no inherent order that are ordered by index value. An <term> is a collection of objects that are ordered by value. An <term> is a collection of objects with no inherent order
What are the advantages of using an array implementation as opposed to a linked implementation?
An <term> uses less space per object since it only has to store the object and not an extra pointer. However, the array implementation will allocate much more space than it needs initially
Encapsulation
An OOP concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Leads to the important OOP concept of data hiding. An object publishes its interface and users of the object adhere to the interface to use the object without knowing the details how the object is implemented
A class that provides an API that must be implemented and no other code.
An interface does not restrict future inheritance, so is the best choice if no other code is provided.
What is a collection?
An object that gathers and organizes other objects
Initialize a string linked list named authorsList
LinkedList<String> authorsList = new LinkedList<String>();
Will the following code compile? Horse creature = new Mammal();
No a reference variable for a child or subclass may not hold a reference to a parent or superclass.
If the Circle class omitted the computeArea() implementation, could Circle objects be instantiated?
No. The subclass would be abstract.
LinkedList implementation of a Queue declared and created
Queue<T> queue = new LinkedList<T>();
Interface
Specifies a set of methods that an implementing class must override and define. Interface must be in the class definition. (Ex: public interface Drawable { ) If a class uses an interface, it must use the keyword "implements".
For LinkedList<Double> accelerometerValues = new LinkedList<Double>(); [9.8, 10.2, 15.4] Write a statement to print the first list element.
System.out.println(accelerometerValues.get(0)); (Must have index)
Write a statement to print the element at the head of jobsDeque
System.out.println(jobsDeque.removeFirst());
Write a statement to print the element at the head of ordersQueue
System.out.println(ordersQueue.remove());
What is the time complexity of the contains operation and the find operation for both implementations?
The <term> and <term> operations for both implementations are O(n) because they are simply linear searches
What is the potential problem with the java.util.Stack implementation?
The <term> implementation is an extension of the Vector class and thus inherits a large number of operations that violate the basic assumptions of a stack
Why is a circular array implementation not as attractive as an implementation for a list as it was for a queue?
The <term> implementation of a queue improved the efficiency of the dequeue operation from O(n) to O(1) because it eliminated the need to shift elements in the array. That is not the case for a list because we can add or remove elements anywhere in the list, not just at the front or the rear.
How do we use the growth function of an algorithm to determine its order?
The <term> of an algorithm is found by eliminating constants and all but the dominant term from the algorithm's growth function
(T or F) An interface cannot be instantiated.
True. An interface only declares methods, and does not provide complete method definitions. Thus, it cannot be instantiated.
Abstract Class
a class that guides the design of subclasses but cannot itself be instantiated as an object. Uses "extends" For example, a Shape class might not only have behavior for setting/getting the Shape's name and color, but also specifies that any subclass must define a method named computeArea()
For LinkedList<Double> accelerometerValues = new LinkedList<Double>(); [9.8, 10.2, 15.4] Write a statement to remove the value 9.8 from the list.
accelerometerValues.remove(9.8);
For LinkedList<Double> accelerometerValues = new LinkedList<Double>(); [9.8, 10.2, 15.4] Write a statement that updates the element at index 1 to 10.6.
accelerometerValues.set(1,10.6);
Encapsulation is implemented in Java through
access modifiers (private/protected/public) in combination with methods (getter/setters methods)
compareTo()
compares a Comparable object to otherComparable, returning a number indicating if the Comparable object is less than, equal to, or greater than otherComparable. Returns 0 if the same, -1 if less, 1 is more ArrayList
Write a statement to add the value "Draw" at the head of jobsDeque
jobsDeque.addFirst("Draw");
Write a statement to add the value 354 to the tail of the ordersQueue
ordersQueue.add(354);
For LinkedList<String> wordsFromFile = new LinkedList<String>(); ["The", "fowl", "is", "the", "term"] Write a statement to insert the word "not" between the elements "is" and "the".
wordsFromFile.add(3, "not");
For LinkedList<Double> accelerometerValues = new LinkedList<Double>(); [9.8, 10.2, 15.4] Write a statement that assigns currentValue with the element at index 2.
currentValue = accelerometerValue.get(2);
Key Object Oriented Programming Concepts (4)
1) Abstraction 2) Encapsulation 3) Inheritance 4) Polymorphism
Any class that implements an interface must: (2)
1) List the interface name after the keyword implements 2) Override and implement the interface's methods
Common Deque Methods (10)
1) addFirst() 2) addLast() 3) removeFirst() 4) removeLast() 5) pollFirst() 6) pollLast() 7) getFirst() 8) getLast() 9) peekFirst() 10) peekLast()
What are the four basic operations on a queue?
1) enqueue 2) dequeue 3) first 4) isEmpty
Linked List methods (7)
1) get() 2) set() 3) add() 4) remove() 5) isEmpty() 6) size() 7) clear()
What are some of the other operations that might be implemented for a queue?
1) makeEmpty 2) destroy 3) full
What are some of the other operations that might be implemented for a stack?
1) makeEmpty() 2) destroy() 3) full()
List Iterator methods (9)
1) next() 2) nextIndex() 3) previous() 4) previousIndex() 5) hasNext() 6) hasPrevious() 7) add() 8) remove() 9) set()
What are the five basic operations on a stack?
1) push(i) 2) pop() 3) peek() 4) size() 5) isEmpty()
Compare and contrast a linked list and an array
A <term> has no capacity limitations, whereas an <term> does. However, <term> provide direct access to elements using indexes, whereas a <term> must be traversed one element at a time to reach a particular point in the list
What is the difference between a doubly linked list and a singly linked list?
A <term> maintains a reference to the first element in the list and then a next reference from each node to the following node in the list. A <term> maintains two references: front and rear. Each node in the <term> stores both a next and a previous reference
What is an abstract data type?
A data type that is not defined within the programming language and must be defined by the programmer
Why is the time to increase the capacity of the array on an add operation considered negligible for the ArrayList implementation?
Averaged over the total number of insertions into the list, the time to enlarge the array has little effect on the total time
What is the advantage of postfix notation?
Avoids the needs for precedence rules that are required to evaluate <term> expressions
What are the advantages of the java.util.Stack implementation of a stack?
Because the <term> implementation is an extension of the Vector class, it can keep track of the positions of elements in the stack using an index and thus does not require each node to store an additional pointer. This implementation also allocates space only as it is needed, like the linked implementation
Which implementation has the worst space complexity?
Both of the array implementations waste space for unfilled elements in the array. The linked implementation uses more space per element stored
What do the LinkedStack<T> and ArrayStack<T> classes have in common?
Both the <term> and <term> classes implement the StackADT<T> interface.This means that they both represent a stack collection, providing the necessary operations needed to use a stack. Although they both have distinct approaches to managing the collection, they are functionally interchangeable from the user's point of view
Abstraction implemented in Java through
Classes and Objects
Circle is what kind of class?
Concrete
Can be used as a stack and because
Deque addFirst() removeFirst()
Initialize a string deque named jobsDeque
Deque<String> jobsDeque = new LinkedList<String>();
LinkedList implementation of a Deque declared and created
Deque<T> deque = new LinkedList<T>();
(T or F) An abstract class can be instantiated as an object.
False An abstract class is missing some behaviors. A subclass that implements those behavior can then be instantiated.
(T or F) Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, and location. This program will benefit from an abstract class to represent the trees.
False Because there is no information specific to each species of tree, each tree object can simply possess the species type, age, and location information.
(T or F) Consider a program that maintains a grocery list. Each item, like eggs, has an associated price and weight. Each item belongs to a category like produce, meat, or cereal, where each category has additional features, such as meat having a "sell by" date. This program will benefit from an abstract class.
False Normal inheritance is sufficient. A superclass like Item might implement price and weight. Then subclasses like MeatItem might add behavior like a "sell by" date. But no behavior was mentioned that must be implemented by all subclasses.
(T or F) A class may not simultaneously "extend" a class and "implement" an interface.
False. A class can both extend a class and implement an interface.
(T or F) The EmployeeData class, as defined above, is not required to override the compareTo() method declared by the Comparable interface.
False. Methods declared by an interface must be defined by any implementing classes.
(T or F) The following statement sorts an ArrayList called prevEmployees. Assume prevEmployees is an appropriately initialized ArrayList of EmployeeData elements. sort(prevEmployees);
False. sort() is a static method. Therefore, both the Collections class name and a dot should be appended before the method call, as in Collections.sort(prevEmployees).
Define the term polymorphism
Having many forms. <term> reference is a reference variable that can refer to different types of objects at different points in time. Can change from one invocation to the next.
Abstraction
Helps the helper hide the irrelevant details of an object. Through this, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency of the program
Deque
Interface defined within the Java Collections Framework defines a Collection of ordered elements that supports element insertion and removal at both ends
Queue
Interface defined within the Java Collections Framework defines a Collection of ordered elements that supports element insertion at the tail and element retrieval from the head.
A class that provides only static fields.
Interfaces can declare public static final fields and don't restrict a class' inheritance.
What is the characteristic behavior of a stack?
It is last in first out (LIFO)
Why should a linked list node be separate from the elements stored on the list?
It is unreasonable to assume that every object that we want to put in a collection can be designed to cooperate with the collection implementation. Furthermore, the implementation details are supposed to be kept distinct from the user of the collection, including the elements that the user chooses to add to the collection
What impact would the use of sentinel nodes or dummy nodes have on a doubly linked list implementation?
It would take two dummy records in a <term>, one at the front and one at the rear, to eliminate the special cases when dealing with the first and last nodes
Why does speeding up the CPU not necessarily speed up the process by the same amount?
Larger speedup occurs only if the algorithm has constant order, O(n). As the complexity of the algorithm grows, faster processors have significantly less impact
A class that provides default code to other classes that use that class.
Only abstract classes can provide code to the subclasses. Interfaces provide method declarations but no code implementing methods.
A class that provides default variables
Only abstract classes can provide variables/fields to the subclasses.
Why is a class an excellent representation of an abstract data type?
Only the methods that provide services to other classes have public visibility
Initialize an integer queue named ordersQueue
Queue<Integer> ordersQueue = new LinkedList<Integer>();
What is the difference between the growth function of an algorithm and the order of that algorithm?
The <term> of an algorithm represents the exact relationship between the problem size and the time complexity of the solution. The <term> of the algorithm is the asymptotic time complexity. As the size of the problem grows, the complexity of the algorithm approaches the asymptotic complexity
What are the trade-offs in time complexity between an ArrayList and a LinkedList?
The ArrayList can access any element of the list in the same amount of time if the index value is know, while the LinkedList requires the list to be traversed from one end or the other to reach a position.
What is abstraction and what advantage does it provide?
The concept of hiding the underlying implementation of operations and data storage in order to simplify the use of a collection
What are the trade-offs in space complexity between an ArrayList and a LinkedList?
The major difference is access to a particular index position of the list. The <term> implementation can access any element of the list in the same amount of time if the index value is known. The <term> implementation requires the list to be traversed from one end or the other to reach a particular index position LinkedList requires more space per object, while the ArrayList is resizable.
Which implementation has the worst time complexity?
The noncircular array implementation with an O(n) dequeue or enqueue operation
What special case exists when managing linked lists?
The primary special cas in a <term> processing occurs when dealing with the first element in the list. A special reference variable is maintained that specifies the first element in the list. If that element is deleted or if a new element is added in front of it, the front reference must be carefully maintained
Define the term inheritance
The process in which a new class is derived from an existing one. The new class automatically contains some or all of the variables and methods in the original class. Then, to tailor the class as needed, the programmer can add new variables and methods to the derived class, or modify the <term> ones.
What is a data structure?
The set of objects necessary to implement an abstract data type
How do we determine the time complexity of a method call?
The time complexity of a <term> is found by determine the time complexity of the <term> and then substituting that for the <term>
How do we determine the time complexity of a loop?
The time complexity of a <term> is found by multiplying the time complexity of the body of the <term> by the number of times the <term> will execute
What would be the time complexity of the push operation if we chose to push at the end of the list instead of at the front?
To push at the end of the list, we would have to travers the list to reach the last element. This traversal would cause the time complexity to be O(n). An alternative would be to modify the solution to add a rear reference that always pointed to the last element in the list. This would help the time complexity for add but would also have consequences if we try to remove the last element
(T or F) Consider a program that catalogs the types of trees in a forest. Each tree object contains the tree's species type, age, location, and estimated size based on age. Each species uses a different formula to estimate size based on age. This program will benefit from an abstract class.
True A superclass Tree might store the age and location, and specify an estimateSize() behavior. Subclasses for each species like OakTree then implements the species particular growth rate formula.
(T or F) An interface contains method declarations, as opposed to method definitions.
True. An interface provides method declarations, which only consist of the method's return type, name, and parameters.
What is the purpose of generics in the Java language?
We can define a class so that it stores, operates on, and manages objects whose type is not specified until the class is instantiated
Can a class implement more than one interface?
Yes using a comma separated list
Can the Shape class define and provide code for non-abstract methods?
Yes, an abstract class can include both method signatures for abstract methods and complete code for non-abstract methods.
Is it possible for the front and rear references in a circular array implementation to be equal?
Yes, it can happen when the <term> is empty or full
Is it possible for the head and tail references in a linked implementation to be equal?
Yes, it happens when the <term> is empty or when there is only one element
Will the following code compile? Animal creature = new Parrot();
Yes. A reference variable of a parent class or any superclass may hold a reference to one of its descendants.