30-Jun-15 Static Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
IT151: Introduction to Programming
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Week 9: Methods 1.  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.
Starting Classes and Methods. Objects have behaviors In old style programming, you had: –data, which was completely passive –functions, which could manipulate.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
Introduction to Programming with Java, for Beginners Scope.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
COMP More About Classes Yi Hong May 22, 2015.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Loops and Iteration for Statements, while Statements and do-while Statements.
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
C Programming Lecture 8-1 : Function (Basic). What is a Function? A small program(subroutine) that performs a particular task Input : parameter / argument.
Introduction to programming in the Java programming language.
Controlling Execution Dong Shao, Nanjing Unviersity.
ECE122 Feb. 22, Any question on Vehicle sample code?
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
24-Dec-15 Class Structure. 2 Classes A class describes a set of objects The objects are called instances of the class A class describes: Fields (instance.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Department of Computer Engineering Methods Computer Programming for International Engineers.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Functions Dilshad M. Shahid New York
Chapter 2 Wrap Up 2/18/16 & 2/22/16. Topics Review for Exam I DecimalFormat More Style Conventions Debugging using eclipse The Java API.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Methods Chapter 6.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 4 Procedural Methods.
Method Mark and Lyubo.
Static Methods 14-Nov-18.
For Loops October 12, 2017.
Class Structure 16-Nov-18.
Class Structure 28-Nov-18.
Object Oriented Programming (OOP) LAB # 5
Class Structure 7-Dec-18.
Functions Pass By Value Pass by Reference
Class Structure 2-Jan-19.
Chapter 7 Procedural Methods.
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Class Structure 25-Feb-19.
Classes, Objects and Methods
Classes and Methods 15-Aug-19.
Classes Member Qualifiers
Presentation transcript:

30-Jun-15 Static Methods

About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the method When you call the method, you can give it parameters (information, such as numbers, going into the method) A method typically has a return value (a single piece of information coming out of the method) Declarations and statements parameter return value

Why methods? Methods help you break up a complex problem into simpler subproblems, which you can solve separately Methods can use other methods, but should not depend on the “inner workings” of those other methods--just what they do, not how they do it Methods allow you to give names to parts of your computation, thus making your program more readable Readability is essential in good programming Proper choice of method names is important Verbs are usually best, since methods “do something” If you have anything the least bit complicated to compute, you should write (and debug) it in only one place This is an application of the DRY principle (“Don’t Repeat Yourself) Methods are very good for putting code in only one place

Defining a static method A static method has the syntax: static return-type method-name ( parameters ) { method-variables code } Example: static boolean isAdult(int age) { int magicAge = 21; return age >= magicAge; } Example: static double average(int a, int b) { return (a + b) / 2.0; }

Returning a result from a method If a method is to return a result, it must specify the type of the result: static boolean isAdult ( … You must use a return statement to exit the method with a result of the correct type: return age >= magicAge ;

Returning no result from a method The keyword void is used to indicate that a method doesn’t return a value The return statement must not specify a value Example: static void printAge(String name, int age) { System.out.println(name + " is " + age + " years old."); return; }  There are two ways to return from a void method:  Execute a return statement  Reach the closing brace of the method

Non- static methods This slide set only talks about static methods Most methods in a Java program will not be static However, before we can talk about what it means to be static, we have to introduce classes and methods So, more to come...

The End