0% found this document useful (0 votes)
16 views

Lesson 4. Java Methods

The document discusses object oriented programming and Java methods. It explains that methods allow code to be reused by defining it once and calling it multiple times. Methods can take parameters as input and may or may not return a value. The document provides examples of how to define methods with parameters, call methods, and pass arguments to methods when calling them.

Uploaded by

Silhig Lanot
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lesson 4. Java Methods

The document discusses object oriented programming and Java methods. It explains that methods allow code to be reused by defining it once and calling it multiple times. Methods can take parameters as input and may or may not return a value. The document provides examples of how to define methods with parameters, call methods, and pass arguments to methods when calling them.

Uploaded by

Silhig Lanot
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

1

Topic #4

z
OBJECT ORIENTED
PROGRAMMING
02/15/2024
2

Java
z
is a full-fledged and powerful language
that can be used in many ways

■ Java Standard Edition (Java SE) to develop client-side applications. The


applications can run standalone or as applets running from a Web browser.

■ Java Enterprise Edition (Java EE) to develop server-side applications,


such as Java servlets, JavaServer Pages (JSP), and JavaServer Faces
(JSF).

■ Java Micro Edition (Java ME) to develop applications for mobile


devices, such as cell phones.
02/15/2024
3

TIPS AND TRICKS


02/15/2024
4

z
A single space should be added on both
sides of a binary operator

System.out.println(3+4*4); Bad style

System.out.println(3 + 4 * 4); Good style


02/15/2024
5
Block zStyles - A block is a group of statements
surrounded by braces. There are two popular
styles, next-line style and end-of-line style
02/15/2024
6

Object Oriented Programming

PROGRAMMING
ERRORS
02/15/2024
7

z
Syntax Error

 Errors that are detected by the compiler are called syntax errors
or compile errors.
 Syntax errors result from errors in code construction, such as
mistyping a keyword, omitting some necessary punctuation, or
using an opening brace without a corresponding closing brace.
 These errors are usually easy to detect because the compiler
tells you where they are and what caused them.
02/15/2024
8

z
Runtime Error
 Runtime errors are errors that cause a program to terminate
abnormally. They occur while a program is running if the
environment detects an operation that is impossible to carry out.

 Input mistakes typically cause runtime errors. An input error


occurs when the program is waiting for the user to enter a value,
but the user enters a value that the program cannot handle.

 Another example of runtime errors is division by zero.


02/15/2024
9

z
Logic Error

 Logic errors occur when a program does


not perform the way it was intended to.
Errors of this kind occur for many different
reasons.
02/15/2024
10

z
Common Error

Common Error 1: Missing Braces


Common Error 2: Missing Semicolons
Common Error 3: Missing Quotation Marks
Common Error 4: Misspelling Names
02/15/2024
11

Object Oriented Programming

TRACING A
PROGRAM
02/15/2024
12
1 public class ComputeArea {
z
2 public static void main(String[] args) {
3 double radius; // Declare radius
4 double area; // Declare area
5
6 // Assign a radius
7 radius = 20; // radius is now 20
8
9 // Compute area
10 area = radius * radius * 3.14159;
11
12 // Display results
13 System.out.println("The area for the circle of radius " +
14 radius + " is " + area);
15 }
02/15/2024

16 }
13

Object Oriented Programming

JAVA
METHODS
02/15/2024
14

z
METHOD
A method is a block of code which only runs when it is
called. You can pass data, known as parameters, into a
method.

Methods are used to divide and sort functionalities within


a class so that the code will be readable even if its long.

Why use methods?


To reuse code: define the code once, and use it many
times.
02/15/2024
15

A method must be declared within a class.


It is defined with the name of the method,
followed by parentheses ().

Java provides some pre-defined methods,


such as System.out.println(),
02/15/2024
16

z
Syntax

modifier returntype methodName (){


// Do anything here
}
02/15/2024
17

z
Sample Code

public class Main {


static void myMethod() {
// code to be executed
}
}
02/15/2024
18

z Definition of Terms

 myMethod() is the name of the method

 static means that the method belongs to the


Main class and not an object of the Main
class.

 void means that this method does not have a


return value.
02/15/2024
19

z
Call a Method

 Write the method's name followed by two


parentheses () and a semicolon ;

Example: myMethod() is used to print a text


(the action), when it is called:
02/15/2024
20

z Sample Code
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod(); Calling method
}
}
// Outputs: "I just got executed!"
02/15/2024
21

public
z class Main {
static void myMethod() {
System.out.println("I just got executed!");
}
public static void main(String[] args) {
myMethod();
myMethod();
myMethod();
Output: // I just got executed!
} // I just got

} executed!
02/15/2024

/ / I just got
22

Object Oriented Programming

z
PARAMETERS
ARGUMENTS
02/15/2024
23

z
Information can be passed to methods as parameter.
Parameters act as variables inside the method.

Parameters are specified after the method name, inside the


parentheses. You can add as many parameters as you want,
just separate them with a comma.

The following example has a method that takes a String called


fname as parameter. When the method is called, we pass along
a first name, which is used inside the method to print the full
name:
02/15/2024
24 public class Main {
z
static void myMethod(String fname) { PARAMETER
System.out.println(fname + " Saija");
}
public static void main(String[] args) {
myMethod("Liam");
myMethod("Jenny");
Note: has a method that takes a
myMethod("Anja");
String called fname as parameter.
} When the method is called, we pass
} along a first name, which is used

// Liam Saija inside the method to print the full


name:
// Jenny Saija
02/15/2024

// Anja Saija
25

z
Key Take Aways

When a parameter is passed to the method,


it is called an argument. So, from the
example:

fname is a parameter
Liam, Jenny and Anja are arguments
02/15/2024
26

Object Oriented Programming

z
MULTIPLE
PARAMETERS
02/15/2024
27 public class Main {
static
z void myMethod(String fname, int
age) {
System.out.println(fname + " is " +
age);
}
public static void main(String[] args) {
myMethod("Liam", 5);
myMethod("Jenny", 8);
myMethod("Anja", 31);
Ouptut: // Liam is 5
} // Jenny
} is 8
02/15/2024

// Anja is
28

Object Oriented Programming

z
RETURN
VALUES
02/15/2024
29

The void keyword, is used to indicate that the method


should not return a value. If you want the method to
return a value, you can use a primitive data type (such
as int, char, etc.) instead of void, and use the return
keyword inside the method:
02/15/2024
30
public
z class Main {
static int myMethod(int x) {
return 5 + x;
}

public static void main(String[] args) {


System.out.println(myMethod(3));
}
}
// Outputs 8 (5 + 3)
02/15/2024
31

z
Activity 1
Create a java program and a flowchart for this problem.

Score Grade
90 A
80 B
70 C
60 D
59-below FAILED
02/15/2024

You might also like