Methods and Parameters in Java. Methods in Java Also known as  functions, procedures, subroutines For example  System.out.println()  Math.sqrt() Provide.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Procedures and Control Flow CS351 – Programming Paradigms.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
J AVA A SSIGNMENT 1. O VERVIEW Tic Tac Toe How it should work Using the supplied methods What you need to do How we will test your code.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
CMT Programming Software Applications
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Looping Yong Choi School of Business CSU, Bakersfield.
Introduction to Methods
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
COMP More About Classes Yi Hong May 22, 2015.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 CSC103: Introduction to Computer and Programming Lecture No 13.
Imperative Programming
CS 331 Modules Chapter 6. Overview Decomposition and abstraction Program organization Abstract vs. concrete User-defined types Example discussion.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Methods We write methods in our programs for many reasons:
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java and C++ Transitioning. A simple example public class HelloWorldApp { public static void main(String[] args) { //Display the string. System.out.println("Hello.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
CS Class 15 Today  More practice with arrays  Introduction to Multi-dimensional arrays Announcements  Programming project #4 due 10/23 by midnight.
Problem Decomposition and Abstraction. Problem Solving Which one is easier:  Solving one big problem, or  Solving a number of small problems?
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Computer Science 112 Fundamentals of Programming II.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
THINKING PROCEDURALLY OR CONCURRENT (SIMULTANEOUSLY OR SIDE BY SIDE OR IN LOGICAL ORDER A- IDENTIFY THE COMPONENTS OF A PROBLEM B-IDENTIFY THE COMPONENTS.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CS Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define.
C allows programmer to define their own function according to their requirement. These types of functions are known as user-defined functions. Suppose,
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
Functions Venkatesh Ramamoorthy 21-March Examples #include double y ; … y = sin(45*3.1416/180) ; std::cout
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Chapter 9 Introduction to Arrays Fundamentals of Java.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Information and Computer Sciences University of Hawaii, Manoa
Functions + Overloading + Scope
Paul Ammann & Jeff Offutt
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
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.
Starting Out with Java: From Control Structures through Objects
Chapter 7 Procedural Methods.
In C Programming Language
Functions Reasons Concepts Passing arguments to a function
CPS125.
Presentation transcript:

Methods and Parameters in Java

Methods in Java Also known as  functions, procedures, subroutines For example  System.out.println()  Math.sqrt() Provide a name for a sequence of instructions  Provide abstraction and hide the underlying details Can be used by many programs One of the ways to help implement decomposition

User-defined methods Defined by Java  System.out.println()  Math.sqrt() Defined by users/programmers

Basic anatomy of a method 1. Return type 2. Name 3. Parameters (if any) 4. Instructions … int findMax(int[] scores) { // instructions }

Basic syntax of methods … returnType name ( parameters ) { // instructions } For simplicity for this workshop, … is public static

Return Type of a Method Any data type  int, double, boolean, char, …  The method returns a value of that type  ie, a function No return value  void  ie, a procedure

Name of a Method Same rules as naming a variable…

Parameters of a Method Type and name  E.g. int findMax(int[] scores) More than one parameter  E.g. int findMin(int x, int y)

Instructions (Body) of a Method Instructions as before in the main method You have been mostly writing instructions for the main method

Using/Calling a Method Once a method is defined  It can be used or “called” For example,  You have been calling the method System.out.println()

Using/Calling a Method—Parameter Passing If a method has parameters  The parameter types have to match when the method is called Definition  … int findMin(int x, int y) Call  int a, b. m;  m = findMin(a, b);  m = findMin(a, 10);  m = findMin(a + 10, b);

Summary Defining a method  Return type  Name  Parameters  Instructions (body) Calling a method  Parameter types need to match

Problem decomposition for Tic-tac-toe 1. Display the board 2. Make a move a. Ask for a move b. Update the board 3. Decide winner a. Check 3 rows b. Check 3 columns c. Check 2 diagonals Tasks 1 to 3 can be implemented as methods Subtasks 2a-2b, 3a-2c can be as well

Defining Methods in Tic-tac-toe 1. Display the Board  Return type ?  Parameters ? 2. Make a move  Return type ?  Parameters ? 3. Decide Winner  Return type ?  Parameters ?

One Possibility 1. Display the Board  Return type -- void  Parameters -- board 2. Make a move  Return type -- void  Parameters -- board, player 3. Decide Winner  Return type -- char ( X, O, space--no winner)  Parameters – board

Main method—first level in decomposition … main(…) { char[][] board = new char[3][3]; char player = ’X’, winner = ’ ’; // initialize the board … displayBoard(board); makeAMove(board, player); winner = decideWinner(board); }

Main method—Refining it … main(…) { char[][] board = new char[3][3]; char player = ’X’, winner = ’ ’; // initialize the board … do { displayBoard(board); makeAMove(board, player); player = switchPlayer(player); winner = decideWinner(board); } while (…); // no winner and board is not full }

Main method (high-level & readable) … main(…) { char[][] board = new char[3][3]; char player = ’X’, winner = ’ ’; // initialize the board … do { displayBoard(board); makeAMove(board, player); player = switchPlayer(player); winner = decideWinner(board); } while (winner == ’ ’ && !isFull(board)); // no winner and board is not full }

Two more methods 1. switchPlayer(player) 2. isFull(board) Note that: We don’t have to know exactly how to implement the methods  we just need to know we need them  we can further decompose them to solve them It’s ok to add methods  we might not cover everything the first try