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

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

Chapter 3: Modules. Starting Out with Programming Logic & Design. Second Edition. by Tony Gaddis

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Computer Science II (20082) Week 1: Review and Inheritance

Lecture 5: Methods CS2301

CS110: PROGRAMMING LANGUAGE I

AP CS Unit 3: Control Structures Notes

Chapter 9: A Second Look at Classes and Objects

6 Functions. 6.1 Focus on Software Engineering: Modular Programming TOPICS. CONCEPT: A program may be broken up into manageable functions.

CS111: PROGRAMMING LANGUAGE II

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Lecture 05: Methods. AITI Nigeria Summer 2012 University of Lagos.

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Introduction to Computer Science Unit 2. Notes

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

PROGRAMMING FUNDAMENTALS

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

CS171:Introduction to Computer Science II

CS1150 Principles of Computer Science Methods

Computer Science II (20073) Week 1: Review and Inheritance

CS110D: PROGRAMMING LANGUAGE I

Introduction to Computer Science Unit 2. Notes

CS Week 2. Jim Williams, PhD

More types, Methods, Conditionals. ARCS Lab.

( &% class MyClass { }

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Methods. CSE 114, Computer Science 1 Stony Brook University

Chapter 2: Input, Processing, and Output

Introduction to Programming (Java) 4/12

Tutorials. Tutorial every Friday at 11:30 AM in Toldo 204 * discuss the next lab assignment

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

A Foundation for Programming

CT 229 Java Syntax Continued

Static methods. Not actually a valid Java static method. Fundamentals of Computer Science Keith Vertanen

CS 200 Using Objects. Jim Williams, PhD

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Advanced Computer Programming

AP Computer Science Unit 1. Programs

Chapter 5 Methods / Functions

CT 229 Methods Continued

Research Group. 2: More types, Methods, Conditionals

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

LECTURE 06 FUNCTIONS

Data Structure and Programming Languages

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

CS111: PROGRAMMING LANGUAGE II

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Anatomy of a Class Encapsulation Anatomy of a Method

Methods. Every Java application must have a main method.

4. Java Project Design, Input Methods

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Object Oriented Programming. What is this Object? Using the Object s Slots

CS-201 Introduction to Programming with Java

Chapter 6 Methods. Dr. Hikmat Jaber

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Chapter 2: Programming Concepts

Midterm Examination (MTA)

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

LECTURE 2 (Gaya College of Engineering)

Chapter 5 Methods. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Lec 7. for loops and methods

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Lecture #6-7 Methods

Visual Programming. Lecture 2: More types, Methods, Conditionals

Methods Summer 2010 Margaret Reid-Miller

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

CS115 Principles of Computer Science

Defining Classes and Methods

Data Structure and Programming Languages

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Selected Questions from by Nageshwara Rao

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CS313D: ADVANCED PROGRAMMING LANGUAGE

Java Identifiers, Data Types & Variables

Building Java Programs

Chapter 6 Lab Classes and Objects

M e t h o d s a n d P a r a m e t e r s

Last Name: Circle One: OCW Non-OCW

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Chapter 11: Inheritance

Chapter 1: Introduction to Computers and Java

8/23/2014. Chapter Topics. Introduction. Java History. Why Program? Java Applications and Applets. Chapter 1: Introduction to Computers and Java

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

St. Edmund Preparatory High School Brooklyn, NY

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

CS-201 Introduction to Programming with Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Transcription:

Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved.

Reading Quiz 1. In Java a named block of code that accomplishes a specific task is called a: a) Function b) Subroutine c) Loop d) Method 2. 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 5-3

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

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

PseudoCode for Insult Generator 1. Clear the screen 2. Display Shakespearean Insult Generator 3. Begin the insult with Thou 4. Add adjectives 5. Add noun 6. Add insult ending

clearscreen() Method call Method header Method body Method declaration

The Method Call Call public static void main(string[] args) clearscreen(); 5-8

Two Parts of Method Declaration Header public static void clearscreen() System.out.println( \f"); Body 5-9

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

What is the output of this program? public class Checkpoint public static void main(string[] args) method1(); method1(); method2(); circle method headers box method calls public static void method1() System.out.println("abra"); public static void method2() System.out.print("cad"); method1(); output: abra 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. 5-13

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 99 1.5 0 0 99 1.5

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; This expression must be of the same data type as the return type Return type The return statement causes the method to end execution and it returns a value back to the statement that called the method. 5-21

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

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 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"); address public static String fullname(string first, String last) John Martin See example: String name; name = first + " " + last; return name; ReturnString.java 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. 5-26

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)