©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.

Slides:



Advertisements
Similar presentations
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
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.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods 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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Introduction to Computers and Programming Introduction to Methods in Java.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
©Brooks/Cole, 2001 Chapter 11 Strings. ©Brooks/Cole, 2001 Figure 11-1.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
©2004 Brooks/Cole Chapter 6 Methods and Scope. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method Can Be Viewed as a Black Box To use a.
Introduction to Methods
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
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.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Writing Classes (Chapter 4)
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
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.
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
CiS 260: App Dev I. 2 Introduction to Methods n A method (or ________) is a segment of code that performs a specific task. n Advantages of modular program.
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 Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
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.
Functions + Overloading + Scope
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.
Methods Chapter 6.
Yanal Alahmad Java Workshop Yanal Alahmad
Object Oriented Systems Lecture 03 Method
Methods.
Starting Out with Java: From Control Structures through Objects
CHAPTER 6 GENERAL-PURPOSE METHODS
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Class.
The Lifecycle of an Object
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
Presentation transcript:

©2004 Brooks/Cole Chapter 6 Methods

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods y = Math.sqrt( x); df.format( price); System.out.println( "Output"); Many methods need to be given some data –method call must include the right number and types of data

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Calling and Passing Data to a Method

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Methods Every method is defined in a class Two kinds of method –instance methods associated with an object ( df.format ) called with objectName.methodName() instance methods may use object data –static methods belong to the class ( Math.sqrt ) call with ClassName.methodName() class methods do not have access to instance data

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 General Format of a Method Method definition has two parts –header (signature) - specifies access, return value, name and parameters –body - block of code that is executed when the method runs

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Structure of a static Method’s Header public is a visibility modifier which allows the method to be used anywhere static means the method is called with the name of the class in front of it returnType specifies the type of data that will be returned parameterList is a comma-separated list of type name pairs public static returnType methodName( parameterList)

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of main method public static void main( String[] args) –public is visibility modifier –static means class method –void means the method returns nothing –main is the name of the method –There is one parameter called args whose type is String[]

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of pow method in Math class public static double pow( double a, double b) –public is visibility modifier –static means class method –double means the method returns a double value –pow is the name of the method –There are two parameter called a and b whose type is double

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of println method in PrintStream class public void print(String s) –public is visibility modifier –void means the method returns a String –print is the name of the method –There is one parameter called s whose type is String

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Signature of format method in DecimalFormat class public String format(double number) –public is visibility modifier –String means the method returns a String –format is the name of the method –There is one parameter called number whose type is double

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Structure of a Method Body Variables declared in the method body are local to the method Parameters are variables that can be used in the body of the method return statement is used to return a value from a method

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Defining findMaximum public static void findMaximum( double x, double y) { double max; if (x > y) max = x; else max = y; System.out.println( "The maximum is " + max); }

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Methods that return a value Think about how you might want to use a method like findMaximum –It really ought to return the maximum value so that it can be used to do something We define a method that returns a value by –using a return type other than void –putting a return statement at the end of the method body

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Defining findMaximum public static double findMaximum( double x, double y) { double max; if (x > y) max = x; else max = y; return max; }

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Method can only return one value Method can return a primitive value an object reference A method whose return type is a class returns the location of an object Objects can contain multiple pieces of data

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Passing a Reference Value

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Exercise 9.a from Section 6.2 Write a method named distance that accepts the coordinates of two points, (x 1, y 1 ) and (x 2, y 2 ), and calculates the distance between them.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Exercise 11 from Section 6.2 Write a method to calculate the monthly payment for a loan