DSA Quiz 2

Ace your homework & exams now with Quizwiz!

Restrictions for defining abstract classes and methods

- A class has to be declared abstract if it contains any abstract methods - Abstract methods cannot be private or static - A constructor cannot be abstract

Conditions for polymorphism

- Classes are in the same hierarchy - All subclasses override the same method - Subclass reference is assigned to superclass reference - Superclass object reference is used to call the method

What should you do if you get a run time error?

- Look at the stack trace

Using print statements in debugging

- Put before, in, or after method call - Use boolean debug statements to turn off and on - Use logger class

What should you do if you get a syntax error?

- Read compiler message - Look for red lines

What should you do if you get a logic error?

- Use print statements or the debugger

How to figure out if an algorithm is a certain time complexity

1. Put in terms of f(n) <= c*g(n) for n >= n_0 (plug in f(n) and g(n)) 2. Simplify 3. Pick c and n_0

Abstract classes

A class that cannot be instantiated (can't use new on it). It can only be extended and is used to define base classes.

Analysis of algorithms

A measure of the amount of resources necessary to execute a section of code. The efficiency or running time of an algorithm stated as a function relating the input size/length to the number of steps (time complexity) or storage locations (space complexity).

.equals()

A method used to check equality of two Objects.

.toString()

A method used to return a text version of an Object. The .toString() method in the Object class returns the class name and the memory address of the object (Class@32134af, for instance)

protected access modifier

Access is restricted to the containing class and to any class that is derived directly or indirectly from the containing class.

Logic error

An error in a program that makes it do something other than what the programmer intended.

Syntax error

An error in a program that makes it impossible to parse — and therefore impossible to interpret.

Runtime error

An error that does not occur until the program has started to execute but that prevents the program from continuing.

Big Omega

Best case (lower bound)

Pros and cons of protected

Can be accessed directly by subclasses, but subclasses also need to enforce validation rules. Only use protected when you need high performance and avoid setting values of protected fields in subclasses

Overriding methods

Changing the definition (body) of an inherited method to suit the subclass.

Highly cohesive

Code for any given state or behavior is only located where it is necessary for the definition of that (inheritance is one way Java enables this, as it allows subclasses to specify the code from the superclass further in a way that is appropriate only to their definitions)

shallow copy

Copying only the reference to an object.

.clone()

Creates and returns a SHALLOW copy of an object (i.e., if something is set equal to a clone, and the original or copy is changed, both are changed)

Subclass constructor

Default constructor of subclass automatically calls that of superclass. super(argument list) explicitly calls the superclass's constructor (has to be the first statement in the subclass constructor)

Writing your own .equals()

Default equals method is probably not going to work. Your equals method should be (5): - Reflexive (a.equals(a) == true) - Symmetric (a.equals(b) == b.equals(a)) - Transitive (a.equals(b) && b.equals(c)) == a.equals(c)) - Consistent - Null-check

final modifier

Does not allow the method to be overridden

Object (the class) inheritance

Every object inherits from the Object class

Object default constructor

Has no arguments, allocates memory (on the heap) and returns reference

Subclass substitution rule

If a superclass object is called for in an expression, a subclass may be substituted

Finding Big O

Ignore constants and low-order terms

Choosing an algorithm

In general, choose the asymptotically superior algorithm. For lower inputs, it doesn't really matter

Loosely coupled

Modules that are relatively independent. Loosely coupled modules are easier to maintain and modify, because the logic in one module does not affect other modules.

Child of an abstract class

Must override all of the abstract methods, or it will be abstract itself

Rank the common complexity classes from lowest growth to highest

O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n), O(n!)

Complexity classes

O(1), O(log n), O(n), O(n log n), O(n^2), O(n^3),..., O(2^n)

Constant time

O(1). An operation whose runtime does not depend on the size of the data structure.

Exponential time

O(2^n). Example: Towers of Hanoi, recursive Fibonacci implementation, etc.

Logarithmic time

O(log n). Usually base 2, meaning the algorithm divides some space in half repeatedly. Example: binary search

Log-linear time

O(n log n). Example: best sorting algorithms (quicksort and mergesort)

Quadratic time

O(n^2). the number of steps is proportional to the square of the input size. Example: worse sorting algorithms, pretty much anything with nested for loops

.hashCode()

Returns a unique int for the object

T(n) = O(f(n))

T(n) is the time for algorithm on input size n. f(n) is a simpler function that grows at about the same rate

instanceof

The Java language provides the ________ keyword to determine an object's class type at run time

f(n) is O(g(n))

The growth of f(n) must be less than or equal to the growth of g(n). The function with higher growth goes in the O( ), as the function with lower growth is included in the set of functions whose worst case is the higher growth.

extends keyword

The keyword the specifies that a class inherits data and functionality from an existing class. A class can only extend at most 1 other class

Object class

The name of the Java class that is the "mother" of all objects. All other Java class automatically inherit the Object class as the top-level parent class in the hierarchy. This class provides basic methods such as the toString, hashCode, and equals methods.

Overloading

The process of creating multiple methods of the same name but with different signatures

Inheritance in Java

The relationship between a more general superclass and a more specialized subclass

abstract modifier

This modifier indicates that the thing being modified has a missing or incomplete implementation. This modifier can be used with classes, methods, properties, indexers, and events.

Shadowing variables

When a child class declares a variable with the same name as one that is inherited from the parent. Should be avoided

Method overriding

When a child class overwrites a method inherited from a parent class.

Widening conversion

When java converts a lower-ranked value to a higher-ranked type, it is called a

Big O is what case

Worst case (upper bound)

Can you declare an abstract class?

Yes, but you cannot instantiate one.

this keyword

a keyword used to refer to the current object in a method or constructor

Big O

an abstracted function that describes the amount of computer time or memory space required by an algorithm, as a function of problem size. For problems larger than a certain size, the actual time or space required will be less than the Big O multiplied by some constant.

super reference

can be used to refer to the parent class, and often is used to invoke the parent's constructor

A deep copy of an object:

is an operation that copies an aggregate object, and all the objects it references

static keyword

is used to declare members that do not belong to individual objects but to a class itself.

Dynamic binding

making a run time decision about which instance method to call.

Getters and Setters

public method of accessing private members. Important to validate input in setter

Basic operation

the operation that contributes most towards the running time of the algorithm

Big theta

tight bound


Related study sets

APEH Unit 2: Reformation and Religious Wars

View Set

MAGIC TIME - How are you ? - What's your name?

View Set

Chapter 14: Pricing Concepts for Establishing Value

View Set

Wordsmart I and II only words(Partial Bangla), Word Smart for the GRE (2nd Ed)

View Set

Anthropology Human Origins Exam 2

View Set