Static Methods 14-Nov-18.

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.
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.
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.
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.
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 and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
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.
Controlling Execution Dong Shao, Nanjing Unviersity.
ECE122 Feb. 22, Any question on Vehicle sample code?
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
4. Java language basics: Function
User-Written Functions
Chapter 7: User-Defined Functions II
C Functions -Continue…-.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Introduction to Computer Science / Procedural – 67130
Methods Chapter 6.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Functions Inputs Output
For Loops October 12, 2017.
Class Structure 16-Nov-18.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Class Structure 28-Nov-18.
Object Oriented Programming (OOP) LAB # 5
Class Structure 7-Dec-18.
Java Lesson 36 Mr. Kalmes.
Functions Pass By Value Pass by Reference
Class Structure 2-Jan-19.
Unit 3 Test: Friday.
CS2011 Introduction to Programming I Methods (I)
Testing and Repetition
Workshop for Programming And Systems Management Teachers
Class Structure 25-Feb-19.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Classes and Methods 15-Aug-19.
Presentation transcript:

Static Methods 14-Nov-18

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; } 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