Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals.

Similar presentations


Presentation on theme: "1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals."— Presentation transcript:

1 1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals

2 5.1.1 Operators and Notation

3 3 Operator An operator is a mathematical instruction that manipulates data, ‣ e.g. + means ‘add’, * means ‘multiply’, /, %. They are the equivalent of verbs in language.

4 4 Unary operator Unary operators act on a single data item, ‣ e.g. the negation operator used to represent negative numbers e.g. -4, ‣ e.g. the factorial operator, 8!, ‣ e.g. the percentage operator. [Are these not just shorthand for two dat items?]

5 5 Binary operators Binary operators work on two items of data, ‣ e.g. the addition operator (+), 3 + 4 = 7 combines 3 and 4 by addition ‣ e.g. - (minus), * (multiply), / (divide), ^ (to the power of), div (whole number division), mod (divide and use remainder).

6 6 Operand An operand is a data item that the operators act on, ‣ e.g. in the expression y = x + 1, y, x and 1 are the operands.

7 7 Expression An expression is a combination of operators and operands, ‣ e.g. 3x + y = 29. The equivalent of a sentence in language.

8 8 Recap Operator (unary and binary) Operand Expression

9 9 Identifier An identifier is a label chosen by the programmer to represent an item in the program e.g. a variable, method, object or class. They should always have a meaningful name e.g. totalSalary, dayOfWeek, findAverage(), Student. You have seen the Java error message ‘identifier expected’.

10 10 Arguments An argument is a value passed to a method for it to act upon e.g. drawLine(2,2,10,50) has 4 arguments. setPosn(x,y) has 2 arguments, whatever the values of x and y happen to be at the time of the call. Sometimes called an actual parameter, but best to use the term argument.

11 11 Parameters A (formal) parameter is the variable name and type in the method definition e.g. for public int calculateAverage (int x, int y, int z) the parameters are int x, int y and int z. When the code is run, each parameter is replaced by the value of the variable, and becomes the argument of the method.

12 12 Parameters Note that in calling a method, no type is needed e.g. answer = average (numbers); However, in defining a method the type is needed (i.e. formal parameters) e.g. public static double average (int no1, int no2, int no3) {... etc.

13 13 What gets passed? Primitives (int, double, etc.) are passed-by- value i.e. the value of the argument is copied before being sent to the method and any changes to it do not affect the original variable (two different memory locations). Objects are passed-by-reference i.e. a pointer at its memory location is passed, not a copy, and any changes the called method makes change the original object.

14 14 Infix notation Infix notation is the usual way that we write algebraic expressions, i.e. the operator is written between the operands, e.g. ‘the sum of x and y multiplied by z is written ( x + y ) * z. It is also the usual word order in English: ‘the dog bites the man’ = noun - verb - noun.

15 15 Postfix notation In postfix notation, the operator is written after the operands to which it is applied, e.g. x y + z * means ‘x plus y multiplied by z’. It is a more logical order for a processor to work in and requires no brackets. It is sometimes known as reverse Polish notation (RPN). In Latin, the verb comes at the end of the sentence: ‘canis hominem mordet’.

16 16 Prefix notation In prefix notation, the operator comes before the operands ‣ e.g. + x y * z. It is also known as Polish notation. The processor has warning of the operator to be executed before the operands arrive. It also requires no brackets Commands in English can be prefix e.g. ‘add together x and y’.

17 17 Converting between notations If asked to convert a complex expression between notations, put in all implied brackets first, e.g. converting 4 * ( 1 + 2 ) – 3 (which is infix) into postfix, insert all implied brackets: ((4 * ( 1 + 2 )) – 3), convert each element to prefix: (- (* 4 (+ 1 2)) 3), remove the brackets: - * 4 - 1 2 3

18 18 Static vs. dynamic data structures A static data structure has its size and nature determined before a program is executed e.g. an array A dynamic data structure can vary in size to suit the data it has to contain - they are faster and more efficient to use but harder to program.


Download ppt "1 5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals."

Similar presentations


Ads by Google