CMP-2123-Object Oriented Programming Lecture 1.4 By Abrar Ahmad Abrar.ahmad14@ce.ceme.edu.pk
Operators • Operator in java is a symbol that is used to perform operations. There are many types of operators in java such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator. 2Badar Waseer arbabwaseergmail.com
Operators Precedence Postfix expr++ expr-- Unary ++expr --expr +expr -expr ~ ! Multiplicative * / % Additive + - Shift << >> >>> Relational < > <= >= instanceof Equality == != Bitwise AND & Bitwise exclusive OR ^ Bitwise inclusive OR | Logical AND && Logical OR || Ternary ? : Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= 3Badar Waseer arbabwaseergmail.com
• The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java. • if statement • if-else statement • nested if statement • if-else-if ladder Java If-else Statement 4Badar Waseer arbabwaseergmail.com
Java IF Statement • The Java if statement tests the condition. It executes the if block if condition is true. • Syntax: 5Badar Waseer arbabwaseergmail.com
6Badar Waseer arbabwaseergmail.com
7Badar Waseer arbabwaseergmail.com
Java IF-else Statement • The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed. • Syntax: 8Badar Waseer arbabwaseergmail.com
9Badar Waseer arbabwaseergmail.com
10Badar Waseer arbabwaseergmail.com
Java IF-else-if ladder Statement • The if-else-if ladder statement executes one condition from multiple statements. • Syntax: 11Badar Waseer arbabwaseergmail.com
12Badar Waseer arbabwaseergmail.com
13Badar Waseer arbabwaseergmail.com
14Badar Waseer arbabwaseergmail.com
Java Switch Statement • The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. • Syntax: 15Badar Waseer arbabwaseergmail.com
16Badar Waseer arbabwaseergmail.com
17Badar Waseer arbabwaseergmail.com
Java Switch Statement is fall-through • The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases. 18Badar Waseer arbabwaseergmail.com
19Badar Waseer arbabwaseergmail.com
JAVA Methods • A Java method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. • Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design. 20Badar Waseer arbabwaseergmail.com
Creating Method 21Badar Waseer arbabwaseergmail.com
• The syntax shown includes − • modifier − It defines the access type of the method and it is optional to use. • returnType − Method may return a value. • nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list. • Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters. • method body − The method body defines what the method does with the statements. 22Badar Waseer arbabwaseergmail.com
• Here, • public static − modifier • int − return type • methodName − name of the method • a, b − formal parameters • int a, int b − list of parameters • Method definition consists of a method header and a method body. 23Badar Waseer arbabwaseergmail.com
24Badar Waseer arbabwaseergmail.com
25 • Here is the source code of the above defined method called minFunction(). This method takes two parameters n1 and n2 and returns the minimum between the two − Badar Waseer arbabwaseergmail.com
Method Calling • For using a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value). • The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when − • the return statement is executed. • it reaches the method ending closing brace. 26Badar Waseer arbabwaseergmail.com
• The methods returning void is considered as call to a statement. Lets consider an example − • The method returning value can be understood by the following example − • Following is the example to demonstrate how to define a method and how to call it − 27 Badar Waseer arbabwaseergmail.com 27
28 • This will produce the following result − Badar Waseer arbabwaseergmail.com
The void Keyword • The void keyword allows us to create methods which do not return a value. Here, in the following example we're considering a void method methodRankPoints. This method is a void method, which does not return any value. Call to a void method must be a statement i.e. methodRankPoints(255.7);. It is a Java statement which ends with a semicolon as shown in the following example. 29Badar Waseer arbabwaseergmail.com
30 • This will produce the following result − Badar Waseer arbabwaseergmail.com
Passing Parameters by Value • While working under calling process, arguments is to be passed. These should be in the same order as their respective parameters in the method specification. Parameters can be passed by value or by reference. • Passing Parameters by Value means calling a method with a parameter. Through this, the argument value is passed to the parameter. 31Badar Waseer arbabwaseergmail.com
• The following program shows an example of passing parameter by value. The values of the arguments remains the same even after the method invocation. 32Badar Waseer arbabwaseergmail.com
in 33Badar Waseer arbabwaseergmail.com
• This will produce the following result − 34Badar Waseer arbabwaseergmail.com

Lec 1.4 Object Oriented Programming

  • 1.
  • 2.
    Operators • Operator injava is a symbol that is used to perform operations. There are many types of operators in java such as unary operator, arithmetic operator, relational operator, shift operator, bitwise operator, ternary operator and assignment operator. 2Badar Waseer arbabwaseergmail.com
  • 3.
    Operators Precedence Postfix expr++expr-- Unary ++expr --expr +expr -expr ~ ! Multiplicative * / % Additive + - Shift << >> >>> Relational < > <= >= instanceof Equality == != Bitwise AND & Bitwise exclusive OR ^ Bitwise inclusive OR | Logical AND && Logical OR || Ternary ? : Assignment = += -= *= /= %= &= ^= |= <<= >>= >>>= 3Badar Waseer arbabwaseergmail.com
  • 4.
    • The Javaif statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java. • if statement • if-else statement • nested if statement • if-else-if ladder Java If-else Statement 4Badar Waseer arbabwaseergmail.com
  • 5.
    Java IF Statement •The Java if statement tests the condition. It executes the if block if condition is true. • Syntax: 5Badar Waseer arbabwaseergmail.com
  • 6.
  • 7.
  • 8.
    Java IF-else Statement •The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed. • Syntax: 8Badar Waseer arbabwaseergmail.com
  • 9.
  • 10.
  • 11.
    Java IF-else-if ladderStatement • The if-else-if ladder statement executes one condition from multiple statements. • Syntax: 11Badar Waseer arbabwaseergmail.com
  • 12.
  • 13.
  • 14.
  • 15.
    Java Switch Statement •The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. • Syntax: 15Badar Waseer arbabwaseergmail.com
  • 16.
  • 17.
  • 18.
    Java Switch Statementis fall-through • The java switch statement is fall-through. It means it executes all statement after first match if break statement is not used with switch cases. 18Badar Waseer arbabwaseergmail.com
  • 19.
  • 20.
    JAVA Methods • AJava method is a collection of statements that are grouped together to perform an operation. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. • Now you will learn how to create your own methods with or without return values, invoke a method with or without parameters, and apply method abstraction in the program design. 20Badar Waseer arbabwaseergmail.com
  • 21.
    Creating Method 21Badar Waseerarbabwaseergmail.com
  • 22.
    • The syntaxshown includes − • modifier − It defines the access type of the method and it is optional to use. • returnType − Method may return a value. • nameOfMethod − This is the method name. The method signature consists of the method name and the parameter list. • Parameter List − The list of parameters, it is the type, order, and number of parameters of a method. These are optional, method may contain zero parameters. • method body − The method body defines what the method does with the statements. 22Badar Waseer arbabwaseergmail.com
  • 23.
    • Here, • publicstatic − modifier • int − return type • methodName − name of the method • a, b − formal parameters • int a, int b − list of parameters • Method definition consists of a method header and a method body. 23Badar Waseer arbabwaseergmail.com
  • 24.
  • 25.
    25 • Here isthe source code of the above defined method called minFunction(). This method takes two parameters n1 and n2 and returns the minimum between the two − Badar Waseer arbabwaseergmail.com
  • 26.
    Method Calling • Forusing a method, it should be called. There are two ways in which a method is called i.e., method returns a value or returning nothing (no return value). • The process of method calling is simple. When a program invokes a method, the program control gets transferred to the called method. This called method then returns control to the caller in two conditions, when − • the return statement is executed. • it reaches the method ending closing brace. 26Badar Waseer arbabwaseergmail.com
  • 27.
    • The methodsreturning void is considered as call to a statement. Lets consider an example − • The method returning value can be understood by the following example − • Following is the example to demonstrate how to define a method and how to call it − 27 Badar Waseer arbabwaseergmail.com 27
  • 28.
    28 • This willproduce the following result − Badar Waseer arbabwaseergmail.com
  • 29.
    The void Keyword •The void keyword allows us to create methods which do not return a value. Here, in the following example we're considering a void method methodRankPoints. This method is a void method, which does not return any value. Call to a void method must be a statement i.e. methodRankPoints(255.7);. It is a Java statement which ends with a semicolon as shown in the following example. 29Badar Waseer arbabwaseergmail.com
  • 30.
    30 • This will producethe following result − Badar Waseer arbabwaseergmail.com
  • 31.
    Passing Parameters byValue • While working under calling process, arguments is to be passed. These should be in the same order as their respective parameters in the method specification. Parameters can be passed by value or by reference. • Passing Parameters by Value means calling a method with a parameter. Through this, the argument value is passed to the parameter. 31Badar Waseer arbabwaseergmail.com
  • 32.
    • The followingprogram shows an example of passing parameter by value. The values of the arguments remains the same even after the method invocation. 32Badar Waseer arbabwaseergmail.com
  • 33.
  • 34.
    • This willproduce the following result − 34Badar Waseer arbabwaseergmail.com