5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable,...

32
5.1 and 5.4 through 5.6 Various Things

Transcript of 5.1 and 5.4 through 5.6 Various Things. Terminology Identifiers: a name representing a variable,...

5.1 and 5.4 through 5.6

Various Things

Terminology

• Identifiers: a name representing a variable, class name, method name, etc.

• Operand: a named memory location or object manipulated by the program. Variables are operands and identifiers– Ex: In “var1 + 45”, var1 is an identifier and operand,

while 45 is only an operand.• Operator: Indicate action to be applied to

operands– i.e. +, -, /, and, or, xor, not, etc.

Terminology (Continued)

• Unary Operator: An operator that requires only one operand.– Ex: Negation in boolean expressions, or x++ in

Java.• Binary Operator: An operator that requires 2

operands, like + in x + y

Terminology (Continued) (Continued)

• Actual Parameter, or argument: The variable names that a method calls for– In DoStuff(x), the actual parameter/argument is x.

• Formal Parameter: The variables sent to a method. Parameters get their values from arguments.

Passing By-Value and By-Reference

• By-Value: The value is sent, not a reference to the original variable. It’s a “copy”.

• By-Reference: The original variable is used. If the parameter is manipulated, the original will be, too.

• Most things in Java are sent by-value. Arrays, java classes, and user-defined objects are passed by reference.

• Java doesn’t allow primitive data types (int, char, etc.) to be passed by-reference.

Data Structures

• The way data is organized• LIST data structures store a sequence of the

same data type• Stacks, Queues, and Binary Trees are examples

of lists

Stacks

• A stack is simple. Think of objects being stacked on top of each other.

• When an object is added to the stack, it always goes on top. This is called PUSHing.

• When an object is removed from the stack, it is always the top one, and it is removed. This is called POPping.

• You cannot PUSH into a full stack, or POP from an empty stack.

• However, a stack can be implemented as a linked list, giving it no maximum except available memory.

Queues

• Queue: noun: a file or line, esp. of people waiting their turn.

• A queue in Java is the programming equivalent to this.

• Items are taken from the front of the queue, but they’re not added to the front (like in stacks). Instead, they’re added to the back.

• Called a First-In-First-Out (FIFO) Data Structure

Parity CheckingSimple error detection

• Uses “parity bits” to check that data is communicated properly.

• A parity bit is a bit added to binary data to ensure that the number of 1s is even or odd.– Ex: for odd parity checking, 100101 is changed to

0100101, to keep the number of 1s odd.– Ex: for even parity checking, 100101 is changed to

1100101, to make the number of 1s even.

Parity Checking (continued)

• When the data is received, the receiving program checks to see if there are an even or odd number of 1s in binary information (depending on if it’s even or odd parity checking).– If not, then an error in data transmission has occurred.

• NOTE: Odd parity checking and even parity checking are the same, except for the number of 1s.

Object Oriented Program

Object Oriented Programming

• The kind of programming you’ve been doing (Java is object-oriented)

• Object Oriented Programming is the use of objects (classes) to make programming modular.

• This way, programs can be better organized, and specific parts of programs can be more easily reused.

Features of an Object

• Functions – return values• Procedures – perform a task, but don’t return

a value• Object – a combination of data and operations – Objects can be modified

• Data Items – pretty much data– Can be made private or public

Object Class

• Object is the root of the class hierarchy. – Every class has Object as a superclass.

• All objects, including arrays, implement the methods of this class.

• Object class rules everything

Encapsulation

• Basically this is the ability to hide variable information from other parts of the program

• I.E. private variables

Advantages of encapsulation

• Less variables to manage; the programmer only has to know what a method does in order to use it

• Less prone to errors• Large applications are easier to maintain since

objects may be updated and recompiled as necessary

Polymorphism

• Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon

• Examples– Overwriting– Concatenation– Overloading– Applets

Inheritance

• The ability to derive new classes from existing classes

• New methods have the same name as the superclass, but override them

• I.E. Automobile class– Subclass car– Subclass truck– Subclass motorcycle

Advantages of Objects

• Faster development• Increased quality of software, more robust• Easier maintenance• Implementation details are hidden• Enhances reusability and modification

desventajes

• Private variables can cause programmers to have difficulty debugging

• Many computer languages do not support objects, transferring them would be costly

• Simple programs take longer to make• Lack of standardized approach on how to

implement objects

Recursion

What is recursion?

• Recursion is the process of repeatedly calling a function from within the definition of the function itself.

• The function must be able to terminate (aka loop that ends)

• After the process is terminated the solutions and calculations are processed

Advantages

• Less variables• Fewer lines of code

Disadvantages

• Can take longer than other methods• Slower than iterative equivalent• Tricky to follow• Clear documentation is required• Difficult to maintain

Algorithm Evaluation

Efficiency of Algorithms

• Time required depends on processor speed and algorithm design

• Example: Linear Search– List has a size of n• Best case scenario time required is 1• Worst case scenario time required is n

• Depends a lot on how program is written

What’s Necessary to Know

• Single loops are O(N)• A nested loop is O(N2)• More than that is different, but probably not

necessary for IB tests. However…

Various Sorts and their Efficiency

Big O Notation

• Big O “lives in the land of theory and doesn’t care very much about the real world”

• Big O describes how the algorithm scales (performs) in the worst case scenario as it is is run with more input

• Big-O can also be used to describe other behavior such as memory consumption

• O(N), N = number of elements aka time required

What Big O is not

• The Big-O is not concerned with factors that do not change as the input increases.

• Big O does not care about extra RAM or anything done to the computer to make it faster.

• The only thing Big-O is concerned with is the speed relative to other algorithms (instead of actual speed).

What Big O is

• Good indicator of what algorithm to use once you take your individual circumstances into consideration

• Works great for optimization• Big-O of a given algorithm combined with the

specific problem knowledge is a great way to choose the best algorithm for your situation

Remember These

• Big-O means worst-case– Big-O gives an upper-bound

• Big-Omega means best-case– Big-Omega gives an lower-bound.

• Big-Theta means average-case

The above are oversimplifications, but it’s a good way to remember things.