Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.

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

Programming Methodology (1). Implementing Methods main.
Importing methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Computer Programming Lab 8.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
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.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
Introduction to Computers and Programming Introduction to Methods in Java.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Enhancing classes Visibility modifiers and encapsulation revisited
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
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.
Writing Classes (Chapter 4)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
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.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
CSC 142 Computer Science II Zhen Jiang West Chester University
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
1 Week 8 l Methods l Parameter passing Methods. 2 Using Methods l Methods are actions that an object can perform. l To use a method you invoke or call.
Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Creating and Using Class Methods. Definition Class Object.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Classes - Intermediate
Methods.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
AKA the birth, life, and death of variables.
Object Oriented Systems Lecture 03 Method
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes, Encapsulation, Methods and Constructors (Continued)
CS2011 Introduction to Programming I Methods (II)
AKA the birth, life, and death of variables.
Take out a piece of paper and PEN.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Introduction to Java Brief history of Java Sample Java Program
Scope of variables class scopeofvars {
References Revisted (Ch 5)
Lecture 9 Using Objects.
Presentation transcript:

Methods

Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments

Dot and Parenthesis x.y means “the y inside x” – Similar to: ’s System.out.println means “The println inside the out that is inside System” x() means “x is a method and I want to run it”

What is a class? A class can be (any mix of) 3 things: – A program If and only if it has “public static void main( String[] )” – A library of methods and constants Examples: Math, JOptionPane – A description of a kind of object … We’ll get into this in several weeks Examples: Scanner, Random

static and non-static Consider x.y() If x is a class, y must be static If x is an Object, y should not be static Math.cos(): static because Math is a class stdin.nextInt(): non-static because stdin is an object (in particular, a Scanner).

Method Definition and Invocation A method is a named piece of code Defining a method = writing a how-to Invoking a method = following that how-to Pressing “run” just invokes main()

Parameters and Arguments A formal parameter is what a method writer assumes an invoker will bring to the table An actual argument is the thing the method invoker brings … people are sloppy with these names …

Parameters and Arguments How to fry an egg – Crack the egg into a pan – Add heat Parameters: what I call things I assume will exist when defining a method Fry this ostrich egg Fry this chicken egg Arguments: the values I use for the parameters when invoking a method

Parameters and Arguments public class Example { public static void f( int x ) { System.out.println( x + 2 ); } public class ExampleUser { p … id main( String[] args ) { Example.f( 3 ); int y = 17; Example.f( y - 2 ); }

Return When I ask you “what’s sqrt(4),” you return “2” to me return is how methods give back values return is also a way to halt a method

Carbon Dating Archeologists can date artifacts by the relative concentration of carbon-14 in a sample compared to concentration in the air The formula is age = ln( relative concentration ) × −8260

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); }

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Run invokes method main Dater.main() String args

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Create a variable x in main Dater.main() String args int x

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke carbonDate with argument 0.05 Dater.main() String args int x

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke carbonDate with argument 0.05 Dater.main() String args int x Dater.carbonDate() double concentration 0.05

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Create a variable age in carbonDate Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.05 Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age Math.log() double x 0.05

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.05 Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age Math.log() …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.05 Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age Math.log() …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.05 Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Compute the value ( …) and store it in age Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Compute the return value (24744) Dater.main() String args int x Dater.carbonDate() double concentration 0.05 double age

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } return and store the result Dater.main() String args int x Dater.carbonDate()

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } return and store the result Dater.main() String args Dater.carbonDate() int x

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } return and store the result Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } After returning, carbonDate’s variables disappear Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Compute the string to print: "0.05 means years old" Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke System.out’s println method Dater.main() String args int x PrintWriter.println() String thingToPrint "0.05 means years old"

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke carbonDate with argument 0.10 Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke carbonDate with argument 0.10 Dater.main() String args int x Dater.carbonDate() double concentration 0.10

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Create a variable age in carbonDate Dater.main() String args int x Dater.carbonDate() double concentration 0.10 double age

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.10 Dater.main() String args int x Dater.carbonDate() double concentration 0.10 double age Math.log() double x 0.10

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.10 Dater.main() String args int x Dater.carbonDate() double concentration 0.10 double age Math.log() …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke Math’s log method with argument 0.10 Dater.main() String args int x Dater.carbonDate() double concentration 0.10 double age …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Compute the value ( …) and store it in age Dater.main() String args int x Dater.carbonDate() double concentration 0.10 double age …

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Compute the return value (19019) Dater.main() String args int x Dater.carbonDate() double concentration 0.10 double age

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } return and use the result Dater.main() String args int x Dater.carbonDate()

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } return and use the result Dater.main() String args int x Dater.carbonDate()

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } return and use the result Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } After returning, carbonDate’s variables disappear Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Compute the string to print: "0.10 means years old" Dater.main() String args int x 24744

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } Invoke System.out’s println method Dater.main() String args int x PrintWriter.println() String thingToPrint "0.10 means years old"

public class Dater { public static void main( String[] args ) { int x = Dater.carbonDate( 0.05 ); System.out.println( "0.05 means " + x + " years old" ); System.out.println( "0.10 means " + Dater.carbonDate( 0.10 ) + " years old" ); } public static int carbonDate( double concentration ) { double age = Math.log( concentration ) * -8260; return (int)(age); } After main ends, its variables disappear