Method Examples CS 139 Algorithm Development 10/06/2008.

Slides:



Advertisements
Similar presentations
Programming Methodology (1). Implementing Methods main.
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
Introduction to Methods
Chapter 6: Functions.
CS1101: Programming Methodology Aaron Tan.
Preventing and Correcting Errors
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Chapter 5, Methods Java How to Program, Late Objects Version, 10/e
A Revamping Of our skills so far. Our Tools so Far Variable - a placeholder for a value Method - a set of code which accomplishes a task Class - a mold.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Methods Chapter Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
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.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Chapter 5 : Methods. Why Write Methods?  Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
A High Flying Overview CS139 – Fall 2006 How far we have come.
Classes - Intermediate
Methods.
Exam Review 10/01/2014 Happy October. The Exam  Will be in Canvas  Two parts  Part A is recall – closed book, closed notes ◦Quizzes, in class activity.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
CS001 Introduction to Programming Day 6 Sujana Jyothi
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
A High Flying Overview CS139 – Fall 2010 How far we have come.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Chapter 5 Methods. 2 Contents 1. Introduction to Methods 2. Passing Arguments to a Method 3. More about Local Variables 4. Returning a Value from a Method.
by Tony Gaddis and Godfrey Muganda
Methods.
if-else-if Statements
CS139 – Fall 2010 How far we have come
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Java: From Control Structures through Objects
Starting Out with Java: From Control Structures through Objects
Group Status Project Status.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Week 4 Lecture-2 Chapter 6 (Methods).
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Lecture 5- Classes, Objects and Methods
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Starting Out with Java: From Control Structures through Objects
Happy October Exam Review 10/01/2014.
Methods/Functions.
Corresponds with Chapter 5
Methods and Data Passing
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Method Examples CS 139 Algorithm Development 10/06/2008

SimpleMethod.java System.out.println("Hello from the main method."); displayMessage(); System.out.println("Back in the main method."); System.out.println("Hello from the displayMessage method."); main (String args []) displayMessage() At the point of calling the method, execution suspends and the method carries out its task. println(String output)

Two part compilation – SimpleMethodDriver and SimpleContainer System.out.println("Hello from the main method."); SimpleContainer.displayMessage(); System.out.println("Back in the main method."); System.out.println("Hello from the displayMessage method."); main (String args []) displayMessage() SimpleMethodDriver SimpleContainer

DeeperAndDeeper.java System.out.println("I am starting in main."); deep(); System.out.println("Now I am back in main."); System.out.println("I am now in deep."); deeper(); System.out.println("Now I am back in deep."); System.out.println("I am now in deeper."); main() deep() deeper()

Stack trace. We can’t control Scanner, but we can find the problem in our code or where that problem occurred. ----jGRASP exec: java DeepAndDeeper I am starting in main. I am now in deep. I am now in deeper. Type something: a Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:840) at java.util.Scanner.next(Scanner.java:1461) at java.util.Scanner.nextInt(Scanner.java:2091) at java.util.Scanner.nextInt(Scanner.java:2050) at DeepAndDeeper.deeper(DeepAndDeeper.java:40) at DeepAndDeeper.deep(DeepAndDeeper.java:23) at DeepAndDeeper.main(DeepAndDeeper.java:11) ----jGRASP wedge2: exit code for process is jGRASP: operation complete.

LocalVars.java Illustration of block {} scope in Java. Birds are two different variables and are visible only within their respective methods. texas(); california(); int birds; birds = 5000; System.out.println("In Texas there are birds + " birds."); int birds; birds = 3500; System.out.println("In California there are birds + " birds."); main() texas() california() As we leave texas, birds is destroyed As we leave california, birds is destroyed LocalVars.java

PassArg.java demo of different kinds of values that may be passed to a method public class PassArg { public static void main(String[] args) { int x; x = 10; System.out.println("I am passing values to displayValue."); displayValue(5); // Pass 5 (literal) displayValue(x); // Pass 10 (variable) displayValue(x * 4); // Pass 40 (expression) displayValue(Integer.parseInt("700")); // Pass 700 (method call) System.out.println("Now I am back in main."); } /** The displayValue method displays the value of its integer parameter. */ public static void displayValue(int num) { System.out.println("The value is " + num); } } Any value can be passed as long as the type of the value matches the type of the parameter.

5-8 Defining a Value-Returning Method public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } Return type This expression must be of the same data type as the return type The return statement causes the method to end execution and it returns a value back to the statement that called the method.

ValueReturn.java public class ValueReturn { public static void main(String[] args) { int total; int value1; int value2; value1 = 20; value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } public static int sum(int num1, int num2) { int result; // result is a local variable result = num1 + num2; return result; } }

ValueReturn.java public class ValueReturn { public static void main(String[] args) { int total; int value1; int value2; value1 = 20; value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } public static int sum(int num1, int num2) { int result; // result is a local variable result = num1 + num2; return result; // The integer value is returned and “replaces” the method call. } }

Stubs – A “stub” is a working method which is holding the place while you work on other parts of the program. This program will compile. public class ValueReturn { public static void main(String[] args) { int total; int value1; int value2; value1 = 20; value2 = 40; total = sum(value1, value2); System.out.println("The sum of " + value1 + " and " + value2 + " is " + total); } // documentation goes here public static int sum(int num1, int num2) { return ; } }