* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Presentation on Operators and Notation
Survey
Document related concepts
Transcript
5. Abstract Data Structures & Algorithms 5.1 Data Structure Fundamentals 1 5.1.1 Operators and Notation Operator • An operator is a mathematical instruction that manipulates data, ‣ e.g. + means ‘add’, * means ‘multiply’, /, %. • They are the equivalent of verbs in language. 3 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?] 4 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). 5 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. 6 Expression • An expression is a combination of operators and operands, ‣ e.g. 3x + y = 29. • The equivalent of a sentence in language. 7 Recap • Operator (unary and binary) • Operand • Expression 8 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’. 9 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. 10 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. 11 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. 12 What gets passed? • Primitives (int, double, etc.) are passed-byvalue 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. 13 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. 14 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’. 15 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’. 16 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 17 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. 18