Java 2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What do we mean when we say "An ADT is Abstract"?

"An ADT is Abstract" means: - Implementation details are NOT part of an ADT. - An ADT does NOT specify how the data is to be represented.

Methods in a list to: (1) add elements (2) get elements (3) remove elements (4) find elements (5) set elements

(1) list.add(new Word("hello")) (2) list.get(1) (3) list.remove(2) or: list.remove(new Word("hello")) --> Word has to have a well-defined equals method (4) list.contains("hello") or: list.indexOf("hello") (-1 if not found) (5) list.set(index, "hello")

Right or Wrong? (int x) -> x+1 int x -> x+1 (x) -> x+1 x -> x+1 int x,int y -> x+y (x,int y) -> x+y () -> 85

(int x) -> x+1 int x -> x+1 ==> W (brackets) (x) -> x+1 x -> x+1 int x,int y -> x+y ==> W (x,int y) -> x+y ==> W () -> 85

What is Window in GUI?

(rectangular) portions of the screen

How many types of ADTs?

- Bags - Stacks - Queues - Lists - Dictionary - Tree - Graph

What is the disadvantage of using a type parameter?

- Within the defnition of a parameterized class, there are places where a type name is allowed, but a type parameter is NOT allowed. - Type parameters canNOT be used in simple expressions that use new to create a new object.

Operations on queues

.enqueue() --> add an element to the rear of a queue .dequeue() --> return and remove the 1st element on the front of a queue .first() --> *return* the 1st element .isEmpty() --> determine if a queue is empty .size() --> check the size of a queue .toString()

What are operations on stacks?

.push() --> to add an element to the stack .pop() --> to return and remove the first element of a stack .peek() --> to *return* the first element of a stack .isEmpty() .size() .toString()

Operations on maps?

.put(K,V) --> to add an element to the table AND to change the value of an existing key. .get(K) --> to return the value associated with the key .containKey(K) .containValue(V) .remove(K) --> to remove the pair (K,V)

What is a default load factor?

0.75

Types of Layout Managers?

1. FlowLayout 2. BorderLayout 3. GridLayout 4. BoxLayout

What are 3 parts of a GUI program?

1. Graphical components = view of a program and contains everything that makes up the visual part of the graphical user interface. 2. The event listeners = controllers. The controller listens for messages from the view and operates according to these messages on the data model of the program. 3. The data model = the logic of the program, also the part where the data of your program is stored and manipulated by the view.

What are typical parts of a GUI?

1. Windows 2. Buttons 3. Menus 4. Selection Lists 5. Text Fields

T/F? We can control the flow of events in GUI, similar to our text-based interface?

False. The application cannot rely on a certain order when things happen.

T/F? Similar to Arrays, We can use all data types with ArrayLists.

False. The base type of an ArrayList can't be a primitive type --> (have to use wrapper classes).

T/F? To delete a node, we need to write a function to delete it.

False. To delete a node, we need to set the previous node's link to the node right after it. Then we set its link to null.

4 steps of the push operation in stacks.

1. create a new node containing the data 2. set the new node's next reference to top 3. set top to the new node 4. increment the size of the stack

Order of 3 ways to traversal a tree:

1. pre-order: root, left, right 2. in-order: left, root, right 3. post-order: left, right, root

5 steps of the pop operation:

1. throw an exception if the stack is empty 2. get the data at the top node 3. update top 4. decrement the size of the stack 5. return the data portion of the popped node

Define terminology of a tree: 1. a tree 2. root 3. children of a node 4. parent of a node 5. siblings 6. descendants of a node 7. ancestors of a node 8. a leaf 9. subtree of a node 10. path 11. length of a path 12. height of a tree

1. tree: a set of nodes connected by edges that show a relationship between the nodes. 2. root: the node at the top level 3. children of a node: the nodes directly below it 4. parent of a node: the node directly above it 5. siblings: nodes that share a common parent 6. descendants of a node: all the nodes that are below it 7. ancestors of a node: he nodes that are above it 8. a leaf: a node that has no children 9. subtree of a node: a tree rooted at one of that node's children 10. path: the path to reach any node in a tree, which begins at the root and goes from node to node along the edges that connect them 11. length of a path: the number of edges that compose it 12. height of a tree: the number of levels in the tree

How is GridLayout arranged?

A GridLayout manager arranges components in a grid of rows and columns. All cells in the layout are of the same size. No Region in a GridLayout manager may contain more than one component. To create a manager for 2 rows and 3 Columns: getContentPane.setLayout(new GridLayout(2,3)); The cell of a compenent is determined by the order of the add() call. The first add() call adds the component to cell 1, the second call to cell 2, and so on. You are not allowed to skip a grid position.

What to pay attention when creating a button in Swing?

A JButton object may invoke an action when you click on it. The class which performs the action must implement the ActionListener interface. The interface consist of a single method void actionPerformed(ActionEvent e). Eg: firstButton.addActionListener(new firstButtonHandler()); private class firstButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame,"First button was clicked"); } }

What is a NullPointerException error?

A NullPointerException indicates that access to an object has been attempted using a null reference. A null reference means that no object is referenced by the variable.

What is a hashing function?

A hashing function will calculate the hash code of keys and determine which elements are stored in which locations in a hash map or a hash table.

What is a linked data structure?

A linked data structure is a group of objects (called nodes) that are connected by references (called links).

What is a load factor?

A load factor is used to determine when to resize the table. Eg: a load factor of 0.75 indicates that the table should be resized when it is 75% full.

What are maps?

A map is a kind of generalized array, sometimes also called an associative array. In a map, we use an object (a key) to access elements. Each key is associated with a value.

What is a recursive method?

A method that calls itself with ever simpler cases, until a base case is reached which can be resolved without any recursive calls.

What is collection class?

A new group of classes implement the Collection interface --> Collection class ArrayList is a collection class There is a special for-loop syntax that can be used with collection classes

What is a perfect hashing function?

A perfect hashing function: A hashing function that maps each element to a unique position in the table. It is not necessary to develop a perfect hashing function to use a hash table.

Difference between a recursion method and an iteration method?

A recursive method can be written using a loop statement. --> an iterative version. Iteration is faster than recursion. A recursive version of a method typically executes less efciently than the iterative version. Reason: This is because the computer must keep track of the recursive calls and the suspended computations. However, sometimes it is much easier to write a method recursively than iteratively.

What is an n-ary tree?

A tree in which the nodes have at most n children is called an n-ary tree. In particular, a tree whose nodes have at most 2 children is called a binary tree.

What is ADT?

Abstract Data Types represent the way of organizing data. ADT specifies: (1) data that is stored, and (2) operations that can be done on the data

What is an inner class?

An inner class is a class defied within another class.

What is an iterator?

An iterator allows us to "step through" a collection of objects (in this case a list of nodes).

What is the difference between Array and ArrayList?

ArrayList is dynamic, which means its size can be shrink and grow when the program is running -> more flexible. However, ArrayLists are less efficient than arrays. Also, ArrayList can't store values of primitive types.

T/F? Similar to push and pop in stacks, dequeue is the inversion of enqueue

False.

T/F? We need to declare NullPointerException error in a throw clause if we suspect that our code can throw it later on.

False. A NullPointerException does not need to be caught or declared in a throws clause. It indicates that the code needs to be fxed.

How do we determine a node the last node in the list?

By assigning a null value to the link instance variable of that node

How can we implement an interface?

By providing an implementation of each abstract method in an interface. A class that implements an interface uses the reserved word implements followed by the interface name.

How to form a binary tree?

By using a similar node class with 2 references, we can form binary trees. Each node contains a reference to its left and its right subtree.

What is the advantage of using a type parameter (T or E) instead of a particular type base (String, etx.)?

By using a type parameter instead of a particular base type (String, for example), we can create a linked list with any type of data (Word, Car, BankAccount,...)

How to implement a stack or queue's interface?

By using an array or a linked list.

What is a cell or bucket in a hash map?

Cell / Bucket: a location in the hash table

What is chaining in hash table?

Chaining: The most common way of resolving collisions. With chaining, each location in the table has a linked list of elements to be stored at that location. When an element is added to a location, it is simply appended to the linked list.

The terms _____________________ and Container can be used interchangeably

Collection The terms Collection and Container can be used interchangeably

____________________: a class that implements the collection

Container Container: a class that implements the collection

What is a parameterized class?

Eg: ArrayList<BaseType> A parameterized class is used to specified the type of data stored in a data structure (list, map, etc.) Its parameter, the BaseType, can be replaced by any class type. These defnitions are called generic definitions , or simply generics

What is external iteration?

External iteration means to place all the elements of a linked list into an array and "step through" the elements by iterating the array.

How are elements added and removed from queues?

FIFO: First In First Out Like a stack, only one element can be added or removed at a time. Unlike a stack, which operates on only one end of the collection, a queue operates on both ends.

How are elements added or removed from a stack?

FILO: Last In, First Out Only one element can be added or removed at a time.

In XML, which object do we use to create another object?

Factory Factory factory = Factory.newInstance(); Builder builder = factory.newDocument();

T/F? head is a node in a linked list.

False. head is a reference to the frst node in the list, but is not itself one of the nodes. The reference called head is an instance variable of the StringLinkedList class. It references an object of the node type.

How is FlowLayout arranged?

Flow layouts are typically used to arrange buttons in a panel. It arranges buttons horizontally, until no more buttons fit on the same line. The line alignment is determined by the align property. The possible values are: FlowLayout.LEFT FlowLayout.RIGHT FlowLayout.CENTER FlowLayout.LEADING FlowLayout.TRAILING

In Maps, objects used as keys must have well-defined ________ and ________ methods.

In Maps, objects used as keys must have well-defined *equals* and *hashCode* methods.

What is Menu in GUI?

In a menu, all available commands of the program are grouped together. Usually the menus are placed in a menu bar at the top of a window. Groups of logically related commands are represented as menus which are elements of the menu-bar.

What is internal iteration?

Internal iteration means to use an instance variable to step through the nodes / elements.

What is List<T> interface used for?

It is to provide a common way to store and maintain an ordered collection (sequence/list) of data

What is collision in a hash map?

It is when two or more elements map to the same position.

In a lambda expression, what would happen if a variable is assigned a value the second time?

Lambda expression throws a compilation error, if a variable is assigned a value the second time. Local variables must be final or effectively final when used in the body of a lambda expression.

Can we add more components to a region in a BorderLayout?

No. No Region may contain more than one component. If you add more components only the last one is visible. If you want to add more components to a region, you must create a wrapper component, usually a JPanel, and add the components to the JPanel. The default layout manager of a JPanel is the FlowLayout manager.

What is Selection List in GUI?

Selection lists show a list of strings and the user can select one or more items in the list by clicking on an item.

How many ways to traversal a tree?

Three ways: 1. pre-order 2. in-order 3. post-order They refer to the visitation of the root node. We can use the recursive nature of a binary tree to defne its traversal

How is BoxLayout arranged?

To create a BoxLayout manager you must pass the container and an orientation to the constructor of BoxLayout: BoxLayout aBoxLayout = new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS); The orientation is determined by the following constants: BoxLayout.Y_AXIS - layout components top to bottom BoxLayout.X_AXIS - layout components left to right

Which are two factors that table size depends on?

Table size depends on two factors: - the number of elements to store (n) - whether or not we have a perfect hashing function. If we have a perfect hashing function, the table size is n. If not, it should be n*1.5

What is Text Field in GUI?

Text fields provide a way to enter text which should be passed into a program.

How is BorderLayout arranged?

The BorderLayout places its content into 5 different regions: BorderLayout.NORTH BorderLayout.WEST BorderLayout.CENTER BorderLayout.EAST BorderLayout.SOUTH.

What is dynamic resizing in array or hash table?

The automatic resizing action of creating a larger table / array when the old one is too small. We can, however, indicate when it should be done. For array, it is when the array is full. For table, the more full it is, the less efficient it becomes. --> resize it even before it's full.

What is an infinite recursion?

The recursive method calls itself forever (or until resources run out) without reaching the base case.

What are two instance variables in a node of linked list?

The two references in each node are its instance variables. (1) One refers to the node's data. (2) The other refers to the next node in the list. It links one node to the next.

Do you know any real-life application of stacks?

The undo button on text editors. The operations that you perform (cut, paste, copy,...) are stored on a stack. When you choose "undo" from the menu, the last action that you performed gets undone and is popped from the stack.

Ways to iterate a hash map?

Three ways: 1. keySet(): get a Set of the keys in the map and iterate it. 2. entrySet(): get a Set of the entries in the map and iterate it. 3. values(): get a Collection of the values and iterate it. Note: (2) Map.Entry<*String,String*> entry

T/F? Outer class can access an instance variable of an inner class, even if it is declared as private.

True. The inner and outer classes have access to each other's methods and instance variables, even if they are declared private.

Basic structure of a recursive method.

Using an if-else statement: - One branch represents a base case which can be solved directly - without recursion. - Another branch includes a recursive call to the method, but with simpler or smaller arguments. The base case must be reached eventually.

What is an Event in GUI?

Whenever the user interacts with a GUI an event is generated by the widget library. (eg. click a button, enter a text, choose an option)

Assuming that we have an arraylist with 10 elements. Is the line below legal? Explain. list.add(5, new Word("children"))

Yes, in this case, it will insert the new element at index 5, other elements will be shifted down.

Collection: an ADT that contains ____________________

a group of objects Collection: an ADT that contains a group of objects

What is a stack?

a stack is a linear collection of elements of the same type. A stack is represented as a linked list of nodes, with a reference to the top of the stack and a count of the number of nodes in the stack.

What is Button in GUI?

a way to let the user invoke a command. Buttons do not represent data but commands. A special form of a Button is a Menu. To start the command you click on the button.

In Java, an ADT is represented as _________________ , e.g., List<T>

an interface In Java, an ADT is represented as an interface , e.g., List<T>

Data structure: ______________ of an ADT within a programming language

implementation Data structure: implementation of an ADT within a programming language

How to use method compareTo?

int result = obj1.compareTo(obj2); The method returns an integer. The integer that is returned should be negative if obj1 is less than obj2, 0 if they are equal, and positive if obj1 is greater than obj2. compareTo method is included in Comparable<T> interface.

Which one is legal, which illegal? public class LinearNode<T> public LinkedList<E>() public LinkedList() T someObject; someObject = new T(); T[] someArray; someArray = new T[10];

public class LinearNode<T> public LinkedList<E>() --> illegal public LinkedList() T someObject; someObject = new T(); --> illegal T[] someArray; someArray = new T[10]; --> illegal ==> Type parameters cannot be used in simple expressions that use *new* to create a new object.

All operations of stacks are performed on _______________ of stacks.

the first element All operations of stacks are performed on the first element of stacks.

What does this mean? if (head == null)

to determine if the list empty --> head isn't linked to any node.

In a hash table, the order in which we get the elements back from either of these methods is ________________.

unpredictable In a hash table, the order in which we get the elements back from either of these methods is unpredictable.


Conjuntos de estudio relacionados

Human Anatomy, Chapter 2: Foundations The Cell

View Set

Chapter 43- Hepatic Disorders (Liver Cancer)

View Set

Business Studies- Operations- Interdependence of other Key Business Functions

View Set

Essentials of Human Anatomy and Physiology(11) Activity Lab 10

View Set

Peds: Immunity & Infection, Peds- Chapter 44, Mobility, Neuromuscular Disorder peds, Peds: Shock & trauma, SIDS, Emergencies

View Set

Unit 6: civil war and revolution

View Set

AIS Chapter 10 Reading Questions

View Set

In the Middle of Grand Central Station

View Set