Methods.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Java: How to Program Methods Summary Yingcai Xiao.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
Encapsulation by Subprograms and Type Definitions
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Methods in Java Selim Aksoy Bilkent University Department of Computer Engineering
Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Object-Oriented Programming
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
ECE122 Feb. 22, Any question on Vehicle sample code?
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
C# Programming Methods.
Classes - Intermediate
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Topic: Classes and Objects
Suppose we want to print out the word MISSISSIPPI in big letters.
Methods.
Chapter 4 Procedural Methods.
Writing Methods.
Computing Adjusted Quiz Total Score
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
Method Overloading in JAVA
CS2011 Introduction to Programming I Methods (II)
JAVA Constructors.
Chapter 6 Methods.
Chapter 7 Procedural Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Introduction to Object-Oriented Programming
Lecture 11 Parameters CSE /26/2018.
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Methods

A Java Program A Java Program A Java program consists of one or more classes A Java class consists of one or more methods A Java method consists of one or more statements Java classes Java Methods

… - What is a method … public class class-name { method1 method 2 method n }

Example of a Java Program Class name Main method Class body Instruction

Exercise Write a program that computes: factorial (n!) of 6 factorial of 3, then factorial of 10.

using method Method invocation Return type Method parameter Method definition Method parameter Return instruction

Method Structure { statement … } Method name If method doesn’t return value public or private<static> <void or typeReturned>myMethod(<parameters>) { statement … } Type of the return value Variable list Method body

Invoking a Methods The statements inside a method body are executed when the corresponding method is called from another method. Calling a method is also called invoking a method Each time the method is invoked, its corresponding body is executed

- return Statements … The body of a method that returns a value must also contain one or more return statements A return statement specifies the value returned and ends the method invocation.

return Statements A void method need not contain a return statement, Example : write a method that prints all the numbers between 10 and 30

  public class TestSum {   public static void main)String[] args){        float  i =5;        float  j =2;        float  k =Sum(i ,j);       System.out.println)”The sum =“ + k); } Public static float Sum( float x , float y ) { float z ; z= x + y ; return z; } }

- Method Parameters: Array Parameters A parameter of type array

- Method Overloading … In java the same class can have methods with the same name. Such methods are called overloaded methods. Overloaded methods must differ in the number or type of their arguments. The compiler treats overloaded methods as completely different methods..

These 3 methods have the same name but different signatures - Method Overloading … These 3 methods have the same name but different signatures