Download presentation
Presentation is loading. Please wait.
Published byJune Riley Modified over 8 years ago
1
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost
2
2 Lecture Outline Boolean Expressions Methods Naming Parameter Lists Return Types Invoking / Calling Control Flow Lab 1 Overview
3
3 Boolean Expressions An expression is a combination of one or more operators and operands Last lecture we learned about arithmetic expressions – expressions dealing with numeric types as operands and the operators +, -, *, / and % Boolean expressions compute logical results and make use of the boolean operators: Name: Java Operator AND&& OR| | NOT! XOR^
4
4 Boolean Expressions AND (&&) – Binary operator that evaluates to true if both operands are true OR ( | | ) – Binary operator that evaluates to true if either of the operands are true XOR ( ^ ) – Binary operator that evaluates to true if one and only one of the operands are true NOT ( ! ) – Unary operator that negates the value of an operator boolean one, two; boolean allOrNone = one && two; boolean eitherOne = one || two; boolean onlyOne = one ^ two; boolean negated = !one;
5
5 Boolean Expressions Truth Tables A truth table is a tool we can use to verify the results of a boolean expression for all input values [ Truth Tables on Board ] p q p && q T T T T F F F T F F F F
6
6 Boolean Operators #1NOT (!) #2AND (&&) #3XOR (^) #4OR (||) Precedence Like arithmetic expressions, boolean expressions can be complex Like arithmetic expressions, parenthesis can be used to override precedence (done || found) && !interupted done || found && !interupted
7
7 Advanced Boolean Operations NAND !(a && b) (!a || !b) NOR !(a || b) (!a && !b)
8
8 Broadening Our Programming Context Up till now, we’ve learned: How to declare primitive variables ints, doubles, booleans, etc. How to combine variables, constants, and operators to form complex expressions Arithmetic Expressions Boolean Expressions We can use these to change the value of variables with assignment operators
9
9 Review Basic Structure of a Java Program public class Calculator { } /* Calculator.java Nick Leidenfrost Example program */ public static void main (String[] args) { } // this method starts my program // statements to be executed here...
10
10 Methods Introduction Many of us are familiar with the concept of a function from mathematics: We put in some input We get some defined output In general, a method in Java is pretty much the same thing: f(x) = 2x + 5 The value resulting from this calculation is our output public int calc (int x) { return 2*x + 5; } Here is our Input Here is the expression that uses our input to calculate our output
11
11 Methods Anatomy Method declarations are composed of 4 main parts: Method Name Parameter List Method Body Return Type You may see methods defined with words like “public” and “private” in front of them These are called access modifiers You can remain blissfully ignorant of them for another week public int calc (int x) { return 2*x + 5; } The method name can be any identifier we want The parameter list contains local variable declarations for the input passed into the method. (The name and type of each parameter) The method body is enclosed in curly braces ({ and }) and contains an arbitrary number of statements The return type defines the type of the output that the method produces. This can be any primitive or object type. Collectively, the name, parameter list, return type and access modifier make up the method signature
12
12 Methods Detail: Parameters (Input) When declaring a method, we can specify any number of parameters that we decide we need: The parameters can be of any type, and can be named with any legal identifier public int calcY (int x) { return 2*x + 5; } public int calcY (int slope, int x, int yOffset) { return slope*x + yOffset; }
13
13 Methods Detail: Naming & Identifiers Can two methods have the same name? Yes, actually. This is known as overloading the method. The methods can have the same name, but cannot have the same method signature Why is this useful? Accepting different types of input to perform a calculation Specifying default values public int calcY (int x) { return calcY(2, x, 5); }
14
14 Methods Detail: Return Type The return type of a method specifies what the type of the output will be, e.g. int, double, boolean, etc. If a method has no output, it has a return type of void void doSomething () { }
15
15 Methods Invoking / Calling Methods We can execute a method by invoking or calling the method. To invoke a method, we use the method’s name, and any required input arguments surrounded by parenthesis: Notice that the formal and actual parameters have the same type Also, notice how I am using the method as the Right- Hand Side of an assignment operation This stores the output of the method into the local variable y public int calcY (int x) { return 2*x + 5; } int myXVal = 2; int y = calcY(myXVal); The parameters in the method declaration are referred to as the formal parameters The arguments passed in when the method is invoked are referred to as the actual parameters
16
16 Methods Invoking / Calling Methods If a method requires no parameters, we invoke it with just the method name and empty parenthesis int y = generateRandom();
17
17 Methods The return Statement: Output The return statement specifies what the output of the method will be The return statement must evaluate to a type that is assignable (convertible by assignment) to the return type The Right-Hand Value of the method Must be the last statement to execute in the method public int calcY (int slope, int x, int yOffset) { int y = slope*x + yOffset; return y; }
18
18 Methods The return Statement: Output (or lack thereof) What if a method has a void return type – can it still have a return statement? Yes, but the return statement cannot include any variable or expression Can a method with a void return type be used as the Right-Hand side of an assignment? No. public void calcY (int slope, int x, int yOffset) { int y = slope*x + yOffset; System.out.println(y); return; }
19
19 Method Control Flow Invoking Methods & Control Flow Control flow (or flow of control) is a term that refers to the order of executions of statements within a program We will learn about many ways of manipulating control flow Conditional Statements Loops When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined
20
20 Methods Invoking Methods & Control Flow When a method is invoked: The actual parameters are copied into the formal parameters Control flow (execution) is transferred to inside the method’s body The statements inside the method body are executed The method ends and returns control flow to the caller The method body ends (the method is a void method) A return statement is reached The value returned is copied and passed to the caller All of the method’s local variables are destroyed
21
21 Sample Program public class CubeANumber { public static void main (String[] args) { int cube; cube = cube(5); System.out.println(cube); } public static int cube (int toCube) { int squared = square(toCube); return toCube*squared; } public static int square (int toSquare) { return toSquare*toSquare; }
22
22 int cube (int num) { int square (int num) { square(5); cube(5); void main () { Method Control Flow } } }
23
23 Methods in OOP Methods as Class Members In Java, all methods are defined within a class definition Methods define the behavior of the objects of that class In OOP, methods can be categorized generally as: Accessors Retrieve an object’s state (return the value of fields) Mutators Manipulate an object’s internal state Subroutines Used by other methods in the program – “Helper methods” // You will have 1 of these in Lab 1 // You will have 2 of these in Lab 1 // The rest of the Lab 1 methods
24
24 Methods Benefits: Abstraction Methods can help support abstraction The process of hiding detail Methods specify what inputs it needs (parameters) Methods specify what outputs it produces (return values) How it translates the inputs to outputs is hidden from the programmer Square Root Method aSqrt(a)
25
25 Methods Benefits: Code Reuse Methods promote code reuse and maintainability A problem can be solved generally by the method for any input If we define functionality in one place (inside the method) and need to make a change, we only need to make that change in one place
26
26 Lab 1 Overview public class Calculator { public int plus (int a, int b) { return a+b; } // Method Skeleton public double plus (double a, double b) { return 0.0; }
27
27 Lab 1 Overview Real Programming -> Errors A program can have three types of errors Compile-time errors Syntax and logical errors that can be identified by the compiler. Prior to execution Run-time Errors A problem occurring during program execution, which causes a program to terminate abnormally Semantic Errors The program runs, but produces incorrect / undesired results
28
28 Lab 1 Overview Eclipse IDE We will be using the IDE Eclipse for the rest of our labs this semester You can use another IDE, if you prefer But I probably won’t know how to use it well…
29
29 Lab 1 Overview Memory Operations Some methods you are asked to implement, the “memory” operations will require interaction with an instance variable Instance variables are variables that are defined inside a class body rather than inside a method body We will learn about these on Tuesday When we take our next step towards awesomeness: Classes and Objects
30
30 Conclusion Questions? Lab 1 Assigned On the website Due Next Thursday I will be in Lab now.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.