Starting Out with Java: From Control Structures through Objects

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

Introduction to C Programming
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Starting Out with Java: From Control Structures through Objects Fourth.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 Fall 2009ACS-1903 Methods – Ch 5 A design technique referred to as stepwise refinement (or divide and conquer, or functional decomposition) is used to.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Introduction to Methods
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 5, Methods Java How to Program, Late Objects Version, 10/e
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Procedural programming in Java Methods, parameters and return values.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
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.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
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.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Department of Computer Engineering Methods Computer Programming for International Engineers.
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.
Method Examples CS 139 Algorithm Development 10/06/2008.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 5.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
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.
Functions + Overloading + Scope
Chapter 2 Clarifications
Chapter 7 User-Defined Methods.
Methods Chapter 6.
by Tony Gaddis and Godfrey Muganda
Object Oriented Systems Lecture 03 Method
Methods.
if-else-if Statements
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.
Chapter Topics Chapter 5 discusses the following main topics:
Starting Out with Java: From Control Structures through Objects
Group Status Project Status.
CHAPTER 6 GENERAL-PURPOSE METHODS
IFS410 Advanced Analysis and Design
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 6 Methods.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
Week 4 Lecture-2 Chapter 6 (Methods).
Lecture 5- Classes, Objects and Methods
Fundamental Programming
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
Methods/Functions.
ITM 352 Functions.
Corresponds with Chapter 5
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Starting Out with Java: From Control Structures through Objects Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis

Reading Quiz In Java a named block of code that accomplishes a specific task is called a: a) Function b) Subroutine c) Loop d) Method A _______ method is one that does not return a value when finished. a) boolean b) String c) void d) class

Chapter Topics Chapter 5 discusses the following main topics: Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods

What is a Method? A named group of Java commands.

A Shakespearean Insult Generator Using Methods to Build A Shakespearean Insult Generator Insult me! How it works

PseudoCode for Insult Generator Clear the screen Display “Shakespearean Insult Generator” Begin the insult with Thou Add adjectives Add noun Add insult ending

clearScreen() Method call Method header Method body Method declaration

The Method Call Call public static void main(String[] args) { clearScreen(); }

Two Parts of Method Declaration Header public static void clearScreen() { System.out.println(“\f"); } Body

What is the output of this program? public class Checkpoint { public static void main(String[] args) method1(); method2(); } public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); What is the output? Circle method headers. Box method calls

What is the output of this program? public class Checkpoint { public static void main(String[] args) method1(); method2(); } public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); circle method headers box method calls output: abra cadabra

Let’s write some methods beginInsult() adjective() - use if-else-if loop in main() for many insults 2 adjectives noun() - use switch endInsult()

Why Write Methods? Methods are commonly used to break a problem down into small manageable pieces. This is called divide and conquer. Methods simplify programs. If a specific task is performed in several places in the program, a method can be written once to perform that task, and then be executed anytime it is needed. This is known as code reuse.

Better and Deeper generateInsults() generateInsults(500) Arguments and parameters Data types must match Can pass many arguments like Math.pow() Pass by reference; scope

Checkpoint

Checkpoint argument is given to method call parameter receives argument in method body

99 1.5 0 0

User Input and Passing Data Ask user for # of insults. getNumInsults() Returns an int

Methods we know already. Do they receive and/or return data? int i = keyboard.nextInt(); System.out.println(“Hello World”); String upper = name.toUpperCase(); clearScreen(); System.out.println( Math.sqrt(49) );

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

Calling a Value-Returning Method total = sum(value1, value2); public static int sum(int num1, int num2) { int result; result = num1 + num2; return result; } 40 20 60

Checkpoint is it void or value-returning? 5.3b Is the following line of code a method header or method call? void or value-returning?

Checkpoint is it void or value-returning? a void method has no return value when invoked it appears on a line by itself a value returning method comes back with a value when complete must appear in an assignment statement or output line. is it void or value-returning? 5.3b Is the following line of code a method header or method call? void or value-returning?

What data types can be passed or returned? Any: int, double, boolean, String, … Any primitive or object Parameter type must match Argument type. Return type must match the use in the call.

Returning a Reference to a String Object customerName = fullName("John", "Martin"); public static String fullName(String first, String last) { String name; name = first + " " + last; return name; } See example: ReturnString.java address Local variable name holds the reference to the object. The return statement sends a copy of the reference back to the call statement and it is stored in customerName. “John Martin”

String Returning Method Example public class ReturnString { public static void main(String[] args) String customerName; customerName = fullName("John", "Martin"); System.out.println(customerName); } public static String fullName(String first, String last) String name; name = first + " " + last; return name;

Checkpoint

Checkpoint public static int days( int years, int months, int weeks) public static double distance(double rate, double time)