Chapter 5 Methods. An overview In Add2Integers: println(“This program adds two integers.”); int n1 = readInt(“Enter n1: “); method name: println argument.

Slides:



Advertisements
Similar presentations
Methods Java 5.1 A quick overview of methods
Advertisements

Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
HST 952 Computing for Biomedical Scientists Lecture 3.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Language Issues for Inheritance CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.3 © Mitchell Wand, This work is licensed under a Creative.
Program Design and Development
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 10 Classes Continued
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
Methods Eric Roberts CS 106A January 20, Once upon a time...
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Chapter 5—Methods The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Methods C H A P T E R 5 With method and logic one can.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
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.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Chapter 5: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
1 More Control Structures if and switch (Chap. 8) do-while and forever loops (Chap. 9)
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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.
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Expressions Eric Roberts CS 106A January 13, 2016.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
Chapter 4 Statement Forms. Statement types 1.Simple statements expression; println(“The total is “ + total + “.”); (method call) 2. Compound statements.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Five-Minute Review 1.What are expression statements? Compound statements? 2.What is a scope? 3.What are conditional statements in Java? How about iterative.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
CS 106A, Lecture 4 Introduction to Java
Chapter 4 Repetition Statements (loops)
Review.
Suppose we want to print out the word MISSISSIPPI in big letters.
Chapter 4 Loops DDC 2133 Programming II.
Five-Minute Review What are expression statements? Compound statements? What is a scope? What are conditional statements in Java? How about iterative statements?
Java Programming: Guided Learning with Early Objects
C++ for Engineers and Scientists Second Edition
Five-Minute Review What are expression statements? Compound statements? What is a scope? What are conditional statements in Java? How about iterative statements?
CS 106A, Lecture 7 Parameters and Return
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.
6 Chapter Functions.
Methods Java 5.1 A quick overview of methods
Week 4 Lecture-2 Chapter 6 (Methods).
Presentation transcript:

Chapter 5 Methods

An overview In Add2Integers: println(“This program adds two integers.”); int n1 = readInt(“Enter n1: “); method name: println argument (string): “This program adds two integers.” operation: prints the argument (string) on the console return: no return value (void) When the call to println is completed, the program continues to the next statement

println(“This program adds two integers.”); int n1 = readInt(“Enter n1: “); method name: readInt argument (string): “Enter n1: “ operation: prints the argument (string) on the console, then read an integer from the user (keyboard) return: the integer entered by the user This method can be viewed as an expression. It returns a value (integer). When the call to readInt is completed, the program continues to store the value (integer) to variable n1.

Beauty of methods You don’t need to understand how readInt is implemented. Just use it as a magic box. You will use readInt very often. Most of you probably will never have to understand how readInt is implemented. Information hiding

Methods vs programs Input/output from/to the user should be part of a program, a service to a user. A program passes/gets arguments/results to/from a method, a service to a programmer. New programmers have a tendency to use input/output operations within methods when the logic of the situation calls for using arguments and results.

Method calls as expressions Method calls that return a value can be used as terms in an expression just like variables and constants. Example readInt(“n1: “) + readInt(“n2 “)

Math methods Static methods from the Math class: Math.abs(x), Math.sin(x), Math.sqrt(x) Figure 5-1, p. 137 double distance = Math.sqrt(x*x + y*y); Note you must include the class name Math when calling a Math method. use x*x instead of Math.pow(x, 2)

Method calls as messages The Math methods are static methods, they belong to the class Math. In object-oriented languages like Java, the act of calling a method is often described in terms of sending a message. Objects communicate by sending messages. One object (the sender) invokes a method that belongs to another object (the receiver). Example rect.setColor(Color.RED) sender: the current (calling) object receiver: rect object (an object of GRect)

Method calls as messages (cont.) One object (the sender) invokes a method that belongs to another object (the receiver). Example: rect.setColor(Color.RED) sender: the current (calling) object receiver: rect object (an object of GRect) Pattern: receiver.methodName(arguments)

If the receiver (target object) is this (current, calling object), the receiver can be omitted. println(value) is the same as this.println(value) Since the method println() is defined as a part of the Program class. Every subclass of Program inherits this method.

Writing your own methods visibility type name(parameters) { method body } visibility: private or public, keep a method private if possible. type: data type of the return value or void if no return value. name: use a meaningful name Example private double celsiusToFahrenheit(double c) { implementation }

Returning a value from a method Pattern return expression Example private double feetToInches(double feet) { return 12*feet; }

Example: temperature conversion /* * File: TemperatureConversionTable.java * * This program creates a table of Celsius to Fahrenheit * equivalents. Lower and high limits and step are defined * as constants */ Import acm.program.*;

Public class TemperatureConversionTable extends ConsoleProgram { public void run() { println(“Celsius to Fahrenheit table.”); for (int c = LOWER_LIMIT; c <= UPPER_LIMIT; c += STEP_SIZE) { int f = (int) celsiuToFahrenheit(c); println(c + “C = “ f + “F”); } /* Returns the Fahrenheit equivalent of the Celsius temperature c. */ private double celsiusToFahrenheit(double c) { return (9.0 / 5.0) * c ; /* Private constants */ private static final int LOWER_LIMIT = 0; private static final int UPPER_LIMIT = 100; Private static final int STEP_SIZE = 5; }

Example (cont.) Note method celsiusToFahrenheit belongs to this class, thus this (the receiver) can omitted. Method println is defined as a part of Program, which is the superclass of this class, thus this (the receiver) can be omitted.

Methods involving control statements private int max(int m, int n) { if (m > n) { return m; } else { return n; } same as return ((m > n)? m : n); return can be used at any points in a method and may appear more than once.

Nonnumerical methods private String weekdayName(int day) { switch (day) { case 0: return "Sunday"; case 1: return "Monday"; case 2: return "Tuesday"; case 3: return "Wednesday"; case 4: return "Thursday"; case 5: return "Friday"; case 6: return "Saturday"; default: return "Illegal weekday"; }

Methods returning graphical object /* A circle of radius r centered at (x,y) filled with color */ private GOval createFilledCircle(double x, double y, double r, Color color) { GOval circle = new GOval(x - r, y - r, 2 * r, 2 * r); circle.setFilled(true); circle.setColor(color); return circle; } Useful when drawing multiple filled circles.

Predicate methods Methods that return Boolean values. private boolean isDivisibleBy(int x, int y) { return x % y == 0; } Example for (int i = 1; i <= 100; i++) { if (isDivisibleBy(i, 7)) { println(i); }

Testing power of 2 private boolean isPowerOfTwo(int n) { if (n < 1) return false while (n > 1) { if (n % 2 == 1) return false; n /= 2; } return true; }

Method-calling mechanics Understand what happens during a method-call. Example main program public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } Method private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; }

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT 0 10

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT 010 0

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT (par)return pointreturn value R R

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 0 (par)return pointreturn value R R private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } n (par) 0 result 1i1

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 0 (par)return pointreturn value R R private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } n (par) 0 result 1i1 1

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 0 (par)return pointreturn value R R private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } 1 0! = 1

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 1 private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } 0! = 1

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 1 (par)return pointreturn value R R private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } n (par) 1 result 1i1 1 0! = 1

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 1 (par)return pointreturn value R R private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } 1 0! = 1 1! = 1

public void run() { for (int i = LOWER_LIMIT; i <= UPPER_LIMIT; i++) { println(i + “! = “ + factorial(i)); } run i LOWER_LIMITUPPER_LIMIT010 2 private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } 0! = 1 1! = 1

Decomposition Decompose a large task into more manageable smaller tasks. Even further decompose some subtasks into still smaller subtasks. Decomposition strategy Follow the structure if the real-world problem Each subtask should perform a function that is easy to name and describe Each level should take responsibility for certain details and avoid having those details percolate up to higher level

Example: Drawing a train Pseudocode public void run() { draw the engine draw the boxcar draw the caboose }

Arguments vs named constants In graphical problems, it is about the information needed to draw the right picture, such as sizes and locations. Two ways: You can use named constants to define the parameters of the picture You can pass this information as arguments to each method

Arguments vs named constants Using named constants is easy but inflexible, e.g., the location of a boxcar may change Using arguments is more cumbersome but easy to change Guidelines Use argument when caller will want to supply different values Use named constants when caller will be satisfied with a single value

DrawTrain program Assumptions The caller will always want to supply the location of each car. All train cars are the same size and have the same basic structure. Engines are always black. Boxcars come in many colors, which means the caller must supply it. Cabooses are always red.

DrawTrain program Headers private void drawEngine(double x, double y) private void drawBoxcar(double x, double y, Color color) private void drawCaboose(double x, double y)

Looking for common features Another useful strategy in choosing a decomposition is to look for features that are shared among several different parts of a program. Such common features can be implemented by a single method. Common structure in DrawTrain the frame for the car, the wheels on which it runs, a connector to link it to its neighbor.

The engine is black and adds a smokestack, cab, and cowcatcher. The boxcar is colored as specified by the caller and adds doors. The caboose is red and adds a cupola. You can use a single drawCarFrame method to draw the common parts of each car, as described in the text.

Algorithmic methods Greatest common divisor problem: int gcd(int x, int y) x, y: positive integers gcd(49,35) = 7 gcd(6,18) = 6 gcd(32,33) = 1

A brute force approach public int gcd(int x, int y) { int guess = Math.min(x,y); while (((x % guess) != 0) || ((y % guess) != 0)) { guess--; }

Correctness: while loop terminates if !(((x % guess) != 0) || ((y % guess) != 0)) From De Morgan’s law, it is equivalent to ((x % guess) == 0) && ((y % guess) 0= 0) Thus the final guess is a common divisor. The program counts downward, the final guess is the first common divisor, so the greatest. Termination: initial guess is a positive integer, then decremented in the program. Eventually, it will reach 1, which is always a common divisor. The loop terminates.

Euclid’s algorithm int gcd(int x, int y) { int r = x % y; while (r != 0) { x = y; y = r; r = x % y; } return y; }

Correctness and termination: beyond the scope of this course. Efficiency: gcd( , ) brute force: – 8 % operations Euclid’s: 2 % operations gcd( , ) Euclid’s: 3 % operations