Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
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,
Introduction to Methods
More Arrays Length, constants, and arrays of arrays By Greg Butler.
COMP More About Classes Yi Hong May 22, 2015.
Hello AP Computer Science!. What are some of the things that you have used computers for?
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Classes CS 21a: Introduction to Computing I First Semester,
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.
Introduction to programming in the Java programming language.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
1/28: Inputs, Variable Types, etc. Addition.java in depth Variable types & data types Input from user: how to get it Arithmetic operators.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
String and Scanner CS 21a: Introduction to Computing I First Semester,
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
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.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CIS 234: Java Methods Dr. Ralph D. Westfall April, 2010.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods and Objects.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Department of Computer Engineering Methods Computer Programming for International Engineers.
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];
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
OOP Basics Classes & Methods (c) IDMS/SQL News
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
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.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS 106 Introduction to Computer Science I 10 / 10 / 2007 Instructor: Michael Eckmann.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Chapter 7: Class Variables and Methods Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Class Methods.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
AKA the birth, life, and death of variables.
Introduction to Computer Science / Procedural – 67130
2.5 Another Java Application: Adding Integers
Object Oriented Systems Lecture 03 Method
Chapter 4 Procedural Methods.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lecture 11 C Parameters Richard Gesick.
Interface.
Group Status Project Status.
CS2011 Introduction to Programming I Methods (II)
Chapter 7 Procedural Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Scope of variables class scopeofvars {
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Methods/Functions.
Presentation transcript:

Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static methods, so that we can call them later on in other programs we write

Creating a Method A method has the following structure: modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } The part in purple above is called the method signature...a way of identifying the method and quickly seeing what it needs to work and what it returns.

modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } The modifier section contain words that describe the method, just as public and static. – public means that anyone can call or use the method. There are other things you can say besides public, but we won't use them for now. – static means that the method is a "class method", unchanging and depending only on the input parameters to do what it is supposed to do. If a method has the word static in its signature, you call a it by saying: ClassName.methodName(inputValues);

modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } The returnValueType section says what the method returns when it is called. A method can only return one value, like an int or a double. If the method doesn’t return anything, its returnValueType is void. – If a method has int as the return type, its result can be assigned to an int variable or printed out using System.out.println() – If a method has void as the return type, you CANNOT put the method inside of a System.out.println(), because it doesn’t return anything to print!

modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } The methodName is what the function is called. The rules for method names are the same as the rules for variable names and class names. – Method names can only be one word – Usually, the first letter of method names is written in lowercase

modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } The list of input parameters is a list of variable declarations that say what the method needs from the person calling it in order to work. Just like you do with any variable declaration, when writing the list of input parameters you need to list both the type and name of each variable the method takes in If a method requires more than one input, the input parameter declarations are separated with commas

modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } Inside the method, you can declare variables and use them just like we have been doing in the main method. Variables declared inside of a function (or in a function’s input parameter list) can only be seen inside of the curly braces of that function. So if you declare a variable inside a method (like the main method), it cannot be seen inside the other methods in your class. Because of this, you can have 2 variables with the same name inside of the same class, but inside different methods, and containing different values.

modifer returnValueType methodName(list of input parameters) { method variables declaration method body return value } Finally, the return value is what the function returns to whoever called it. The return value’s type must match the returnValueType listed at the beginning of the function. – You return a value by following the Java reserved word “return” with the name of the variable whose value you want to return. For example, the following returns the value of x: return x; – If the returnValueType is void, you cannot return anything, but you can still use type return; to leave the method.

Examples of writing and calling static methods public class PracticeClass { public static void main(String[] args) { int num1 = 4; int num1Cubed = PracticeClass.cube(4); System.out.println("num1 cubed is"+num1Cubed); } //finds a 3 public static int cube(int a) { int value = a*a*a; return value; }

Review You have used JOptionPane.showInputDialog() to read in a number from the user as a String, then used Double.parseDouble() to turn that String into a valid double value. Similarly, you have used JOptionPane.showInputDialog() to read in a number from the user as a String, then used Integer.parseInt() to turn that String into a valid int value. Getting ints or doubles from the user in a program is a very common occurrence in first-year computer programming classes. Because of this, it might be useful to make a static method we can call whenever we want to get a number, instead of having to use both showInputDialog and parseInt/parseDouble to do the same thing.