Methods. Bok, Jong Soon

Similar documents
Object Orientation Fourth Story. Bok, Jong Soon

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Chapter 4 Defining Classes I

Lecture 5: Methods CS2301

Lecture Set 4: More About Methods and More About Operators

Review Ch 5 static Multiple Choice Test Creating Class Methods

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

University of Palestine. Mid Exam Total Grade: 100

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

CS313D: ADVANCED PROGRAMMING LANGUAGE

Lecture Set 4: More About Methods and More About Operators

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

C Programming for Engineers Functions

( &% class MyClass { }

1.00 Lecture 8. Using An Existing Class, cont.

Answers to review questions from Chapter 2

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

CSE 230 Intermediate Programming in C and C++ Functions

Introduction to Java. Handout-1d. cs402 - Spring

Chapter 5 - Methods Prentice Hall, Inc. All rights reserved.

CS 11 java track: lecture 1

Introduction to Programming Using Java (98-388)

Programming: Java. Chapter Objectives. Chapter Objectives (continued) Program Design Including Data Structures. Chapter 7: User-Defined Methods

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

CT 229 Arrays Continued

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

CS110: PROGRAMMING LANGUAGE I

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CT 229 Methods Continued

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

A Foundation for Programming

CS171:Introduction to Computer Science II

Methods (Deitel chapter 6)

this keyword in java javatpoint

Methods (Deitel chapter 6)

Chapter 4. Defining Classes I

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Chapter 5: Classes and Objects in Depth. Introduction to methods

Arrays. 1 Index mapping

Methods. CSE 114, Computer Science 1 Stony Brook University

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

Chapter 7 User-Defined Methods. Chapter Objectives

Computer Programming, I. Laboratory Manual. Experiment #7. Methods

Methods: A Deeper Look

Java Loop Control. Programming languages provide various control structures that allow for more complicated execution paths.

Loops. CSE 114, Computer Science 1 Stony Brook University

Methods and Functions

Java Primer 1: Types, Classes and Operators

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

1 Lexical Considerations

Preview from Notesale.co.uk Page 3 of 36

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Object-Oriented Programming

Object Oriented Modeling

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS110D: PROGRAMMING LANGUAGE I

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

STUDENT LESSON A5 Designing and Using Classes

M e t h o d s a n d P a r a m e t e r s

Building Java Programs

Lecture #6-7 Methods

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Building Java Programs

JAVA Programming Concepts

*Java has included a feature that simplifies the creation of

APCS Semester #1 Final Exam Practice Problems

TEMPLATES AND EXCEPTION HANDLING

Lexical Considerations

Loops and Expression Types

Advanced Computer Programming

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Lecture 18 Tao Wang 1

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS212 Midterm. 1. Read the following code fragments and answer the questions.

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

Recap: Assignment as an Operator CS 112 Introduction to Programming

Selected Questions from by Nageshwara Rao

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

2.8. Decision Making: Equality and Relational Operators

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Fundamental Concepts and Definitions

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

Transcription:

Methods Bok, Jong Soon javaexpert@nate.com www.javaexpert.co.kr

Methods Enable you to separate statements into code blocks. Can be called whenever appropriate. Can invoke each other. Can call themselves(recursion) All programs use methods. Applications begin with the method called main.

Why Are Methods Necessary? If you do not use methods you are not using object orientation. Methods make programs more readable and more maintainable. Methods make development and maintenance quicker. Methods are central to reusable software. Methods avoid duplication.

Methods Syntax [modifiers] return_type method_name ([parameters]) { method_body modifiers - Java keywords that can be used to modify the way methods are stored or how they run. return_type - Methods used to calculate a value or query an object for a variable. method_name Identifier that will be used to call the method. parameters - Sequences of statements that perform a task. method_body - Methods that perform a task.

Calling Methods After you define a method, you can: Call a method from within the same class Use method s name followed by a parameter list in parentheses Call a method that is in a different class You must indicate to the compiler which class contains the method to call Use nested calls Methods can call methods, which can call other methods, and so on Calling method resumes at point after it called the worker method

Using the return Statement Immediate return Return with a conditional statement static void examplemethod( ){ int su; //... System.out.println("Hello"); if (su < 10) return; System.out.println("World");

Returning Values Declare the method with non-void type Add a return statement with an expression Sets the return value Returns to caller Non-void methods must return a value static int twoplustwo( ) { int a,b; a = 2; b = 2; return a + b; int x; x = twoplustwo( ); System.out.println(x);

Receiving Return Values The return value is returned to the same place in your code where you called it from You can combine the call and the use of the return value in one line, when using the return value. int value = t.add(2, 4);

Declaring and Calling Parameters Declaring parameters Place between parentheses after method name Define type and name for each parameter Calling methods with parameters Supply a value for each parameter static void methodwithparameters(int n, String y) {... methodwithparameters(2, "Hello, world");

Call by Value (Pass by Value) Default mechanism for passing parameters: Parameter value is copied Variable can be changed inside the method Has no effect on value outside the method Parameter must be of the same type or compatible type static void addone(int x){ x++; // Increment x public static void main(string [] args){ int k = 6; addone(k); System.out.println(k); // Display the value 6, not 7

Call by Reference in Java What are reference parameters? A reference to memory location Using reference parameters Match types and variable values Changes made in the method affect the caller Assign parameter value before calling the method static void addone(test test){ test.su++; // Increment test.su public static void main(string [] args){ Test t = new Test(); addone(t); System.out.println(t.su); // Display the value 6 class Test{ int su = 5;

Varargs (Variable Arguments) This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists.

Using Recursive Methods A method can call itself Directly Indirectly Useful for solving certain problems void printtest(int n){ System.out.print(n + \t ); if(n == 3) return; else printtest(n + 1);

Using Recursive Methods (Cont.)

Method Overloading Methods that share a name in a class Distinguished by examining parameter lists class OverloadingExample { static int add(int a, int b){ return a + b; static int add(int a, int b, int c) { return a + b + c; public static void main(string [] args){ System.out.println(add(1,2) + add(1,2,3));

Method Signatures Method signatures must be unique within a class Signature definition Forms Signature Definition Name of method Parameter type No Effect on Signature Name of parameter Return type of method

static Methods Must be preceded with the name of their class and the dot operator(. ) When to use static. Example of a method: Math.random(). Why main() is static.