{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# Slide 5 - COP3502 Programming Fundamentals for CIS Majors 1...

This preview shows pages 1–17. Sign up to view the full content.

COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 4 Loops for while do-while Last Week
Chapter 5 Methods Input arguments Output Overloading Code reusability Scope of variables Objectives

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Methods
Suppose we want to write a program to find the sum of integers from 1 to 10 from 20 to 30 from 35 to 45 Motivation

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Obvious solution Naïve Solution int sum = 0; for ( int i = 1; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for ( int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for ( int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum);
What about some refactoring? Refactor int sum = 0; for ( int i = 1 ; i <= 10; i++) sum += i; System.out.println("Sum from 1 to 10 is " + sum); sum = 0; for ( int i = 20; i <= 30; i++) sum += i; System.out.println("Sum from 20 to 30 is " + sum); sum = 0; for ( int i = 35; i <= 45; i++) sum += i; System.out.println("Sum from 35 to 45 is " + sum); x x x y y y x y x y x y

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A better approach is to use a method Solution public static int sum( int x, int y) { int sum = 0; for ( int i = x; i <= y; i++) sum += i; return sum; } modifier output name input Method body
First, a method should be defined Then we can use the method i.e. calling or invoking a method Invoking a Method public static void main(String[] args) { int total1 = sum(1, 10); int total2= sum(20, 30); int total3 = sum(35, 45); int total4 = sum(35,1000); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
When calling a method within the same class, we directly call the method Invoking a Method public class TestClass{ public static void main(String[] args) { int total1 = sum(1, 10); } //---------------------------------------------- public static int sum( int x, int y) { int sum = 0; for ( int i = x; i <= y; i++) sum += i; return sum; } } calling directly
When calling a method from another class, use class name if a static method Invoking a Method public class TestClass{ public static void main(String[] args) { int total1 = AnotherClass .sum(1, 10); } } Class name public class AnotherClass{ public static int sum( int x, int y) { int sum = 0; for ( int i = x; i <= y; i++) sum += i; return sum; } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
When calling a method from another class, use class name if a static method Invoking a Method public class TestClass{ public static void main(String[] args) { AnotherClass a = new AnotherClass(); int total1 = a.sum(1, 10); } } Instance name public class AnotherClass{ public int sum( int x, int y) { int sum = 0; for ( int i = x; i <= y; i++) sum += i; return sum; } }
Method is A collection of statements grouped together to perform an operation To use a method We invoke the method E.g. int result = sum(1,10); So…

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Method signature Combination of the method name and the parameter list Method Signature public static int sum( int x, int y) { int sum = 0; for ( int i = x; i <= y; i++) sum += i; return sum; } signature Method header
Parameters Parameters public static int sum( int x, int y) { int sum = 0; for ( int i = x; i <= y; i++) sum += i; return sum; } Formal parameter public static void main(String[] args) { int total1 = sum(1, 10); } Actual parameter

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Formal parameters: Variables defined in the method header Actual parameters: When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 80

Slide 5 - COP3502 Programming Fundamentals for CIS Majors 1...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online