Methods 10.2.15. What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.

Slides:



Advertisements
Similar presentations
CSCI 160 Midterm Review Rasanjalee DM.
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.
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)
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Saravanan.G.
COMP More About Classes Yi Hong May 22, 2015.
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.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Method Examples CS 139 Algorithm Development 10/06/2008.
Modules (Methods) Functions and Procedures Parameters...Output.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
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.
Staples are our staple Building upon our solution.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Methods.
Building Java Programs
ATS Application Programming: Java Programming
Week 6 CS 302 Jim Williams, PhD.
Group Status Project Status.
Method Overloading in JAVA
CS110D Programming Language I
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Take out a piece of paper and PEN.
class PrintOnetoTen { public static void main(String args[]) {
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Anatomy of a Java Program
Scope of variables class scopeofvars {
Object Oriented Programming
while while (condition) { statements }
Lecture 11 Parameters CSE /26/2018.
Methods/Functions.
Methods (a.k.a functions)
Corresponds with Chapter 5
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Methods

What is a method?

Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying to call a non static method from a static one public class StaticExample {public static void main(String[] args) { //starting point of execution System.out.println("In main method"); method1(); } public static void method1() { System.out.println( "method 1"); }

4 Method Parameters a method may have any number of parameters each parameter listed separately void print_info(int age, String name) { System.out.println(“my name is “ + name); System.out.println(“I am “ + age + “years old); }

Method Return Statements a class may have multiple methods with the same name as long as the parameter signature is unique may not overload on return type if a method has a return value other than void it must have a return statement with a variable or expression of the proper type multiple return statements allowed, the first one encountered is executed and method ends style considerations

6 Passing Values to a method // value void add10(int x) { x += 10; } void calls() {int y = 12; add10(y); // y = ? }