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

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

View Full Document Right Arrow Icon
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter 4 Loops for while do-while Last Week
Background image of page 2
Chapter 5 Methods Input arguments Output Overloading Code reusability Scope of variables Objectives
Background image of page 3

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

View Full DocumentRight Arrow Icon
Methods
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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);
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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); }
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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; } }
Background image of page 11

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

View Full DocumentRight Arrow Icon
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; } }
Background image of page 12
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…
Background image of page 13

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

View Full DocumentRight Arrow Icon
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
Background image of page 14
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
Background image of page 15

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

View Full DocumentRight Arrow Icon
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 .
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/15/2012 for the course COP 3502 taught by Professor Staff during the Spring '12 term at University of Florida.

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 Right Arrow Icon
Ask a homework question - tutors are online