Methods Chapter 4: Methods Asserting Java ©Rick Mercer.

Slides:



Advertisements
Similar presentations
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Advertisements

Java Programming Strings Chapter 7.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
Notes from HW3 / Lab3 Program documentation – At the top of every class: //************************************************************ // seu01.java Author:
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
Procedural programming in Java
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Asserting Java ©Rick Mercer Chapter 3 Objects and JUnit.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
28-Jun-15 String and StringBuilder Part I: String.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
COMP More About Classes Yi Hong May 22, 2015.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
EXAM 1 REVIEW. days until the AP Computer Science test.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Introduction to Java Java Translation Program Structure
CIS 270—Application Development II Chapter 18-Generics.
Using Java Class Library
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Introduction to programming in java
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Computer Programming BCT 1113
String class.
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Chapter 3: Using Methods, Classes, and Objects
Data types and variables
Java Programming: From Problem Analysis to Program Design, 4e
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.
String and StringBuilder
Chapter 7: Strings and Characters
Using Free Functions Chapter 3 Computing Fundamentals with C++
String and StringBuilder
Chapter 2: Basic Elements of Java
CMSC 202 Java Primer 2.
Defining methods and more arrays
Chapter 4 Implementing Free Functions
String and StringBuilder
Chapter 7 The Java Array Object © Rick Mercer.
String and StringBuilder
String methods 26-Apr-19.
Defining Classes and Methods
Strings in Java.
Classes, Objects and Methods
Object Oriented Programming
Presentation transcript:

Methods Chapter 4: Methods Asserting Java ©Rick Mercer

Methods Java has thousands of methods We have used some existing methods without fully understanding their implementation System.out's print, println String's length, charAt, indexOf, toUpperCase Scanner's nextDouble, nextInt BankAccount's withdraw, deposit Java has thousands of methods We often need to create our own

Methods There are two major components to a method the method heading with access mode, return type, name, parameters the block a pair of curly braces containing code that fulfills the method's responsibility Method headings specify the number and types of arguments required to use the method

Method Heading with documentation /* * Return a new string that is a substring of this string. * The substring begins at the specified beginIndex and * extends to the character at index endIndex-1. * Thus the length of the substring is endIndex-beginIndex. * * Examples: * "hamburger".substring(4, 8) returns "urge" * "smiles".substring(1, 5) returns "mile" * Parameters: * beginIndex - the beginning index, inclusive. * endIndex - the ending index, exclusive. * Returns: the specified substring. */ public String substring(int beginIndex, int endIndex)

Using JUnit to demo substring

What method headings tell us Method headings provide the information needed to use it they show us how to send messages public String substring(int beginIndex, int endIndex) 1 2 3 5 4 5 4 1 Where is the method accessible 2 What does the method evaluate to? 3 What is the method name? 4 What type arguments are required? 5 How many arguments are required?

Arguments are assigned to parameters The substring method requires two arguments in order to specify the portion of the string to return When the message is sent the 1st argument 0 is assigned to parameter beginIndex the 2nd argument 6 is assigned to parameter endIndex fullName.substring(0, 6); public String substring(int beginIndex, int endIndex) Implementation of the method is not shown here

Arguments  Parameters When a message is sent the first argument is assigned to the first parameter, second argument gets assigned to the second parameter,... If you do not supply the correct number and type of arguments, you get compiletime errors fullName.substring("wrong type"); fullName.substring(0, 6, fullName.length()); fullName.substring(); fullName.substring(0.0, 6.0); BTW: This returns the string form index to the end fullName.substring(2); // sometimes convenient

Method Heading: General Form General form of a Java method heading public return-type method-name ( parameter-1, parameter-2, parameter-n, ... ) public says a method is known where objects are constructed return-type may be any primitive type, any class, or void A void method returns nothing, therefore, a void method can not be assigned to anything a void method can not be printed with println

Method Headings Example method headings think of class as type public char charAt(int index) // class String public int indexOf(String sub) // class String public void withdraw(double amt) // class BankAccount public String getText() // class Jbutton public String setText(String str) // class Jbutton public void setSize(int x, int y) // class JFrame public int nextInt() // class Scanner public int nextDouble() // class Scanner public int next() // class Scanner public int nextLine() // class Scanner

Parameters Parameters, which are optional, specify the number and type of arguments required in the message Sometimes methods need extra information How much to deposit? substring need to know begin- and end-indexes? General form of a parameter between ( and ) in method headings class-name identifier -or- primitive-type identifier

The Block The method body is Java code enclosed within a block { } Curly braces have the same things we've seen in main methods variable declarations and initializations int creditOne = 0; objects String str = "local"; messages boolean less = str.compareTo("m") < 0; Method bodies have access to parameters Hence, methods are general enough to be reused with many different arguments

The return statement All non-void methods must return a value The type of the value is defined in the method heading Use Java's return statement return expression ; Example in the context of a method's block public double f(double x) { return 2.0 * x - 1.0; }

Code Demo Given the following documented method heading, Write a test method in ControlFunTest.java The assertions are expected to fail Write the actual method in ControlFun.java /* * Return largest of 3 integer arguments * max(1, 2, 3) returns 3 * max(1, 3, 2) returns 3 * max(-1, -2, -3) returns -1 */ public int max(int a, int b, int c) { return 0; }

Methods: A Summary Method headings provide this information on usage: is the method is available from other places in code? public methods are known in the block where constructed return-type the kind of value a message evaluates to method-name that begins a valid method call parameter-list the number and type of needed arguments documentation to describe what the method does The block is where your algorithm gets implemented The parameters are accessible within the block Methods usually return a value of the correct type Methods must be fully tested (at least in this course)