Methods. Chapter 4: Methods Asserting Java Rick Mercer

Similar documents
Chapter 2: Elements of Java

java Features Version April 19, 2013 by Thorsten Kracht

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Introduction to Java Lecture Notes. Ryan Dougherty

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Computer Programming I

CS 106 Introduction to Computer Science I

Building Java Programs

Example of a Java program

Week 1: Review of Java Programming Basics

Building Java Programs

Introduction to Java

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Object-Oriented Programming in Java

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Moving from CS 61A Scheme to CS 61B Java

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Install Java Development Kit (JDK) 1.8

Class 16: Function Parameters and Polymorphism

Quick Introduction to Java

CS106A, Stanford Handout #38. Strings and Chars

Basics of Java Programming Input and the Scanner class

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CS 106 Introduction to Computer Science I

Introduction to Programming

JAVA.UTIL.SCANNER CLASS

Chapter 1 Java Program Design and Development

Iteration CHAPTER 6. Topic Summary

Comp 248 Introduction to Programming

6.1. Example: A Tip Calculator 6-1

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

High-Level Language. Building a Modern Computer From First Principles.

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Introduction to Java. CS 3: Computer Programming in Java

Programming and Data Structures with Java and JUnit. Rick Mercer

Unit Testing & JUnit

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Building Java Programs

D06 PROGRAMMING with JAVA

Programming in Java Course Technology, a part of Cengage Learning.

Java CPD (I) Frans Coenen Department of Computer Science

Variables, Constants, and Data Types

Topic 11 Scanner object, conditional execution

Lecture 5: Java Fundamentals III

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 2 Basics of Scanning and Conventional Programming in Java

Chapter 8. Living with Java. 8.1 Standard Output

Sample CSE8A midterm Multiple Choice (circle one)

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

CS 111 Classes I 1. Software Organization View to this point:

Visit us at

Introduction to Python

Chapter 3. Input and output. 3.1 The System class

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Understanding class definitions

Arrays. Introduction. Chapter 7

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

J a v a Quiz (Unit 3, Test 0 Practice)

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

csce4313 Programming Languages Scanner (pass/fail)

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

1 Stored Procedures in PL/SQL 2 PL/SQL. 2.1 Variables. 2.2 PL/SQL Program Blocks

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Building Java Programs

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

Basic Object-Oriented Programming in Java

MAT 2170: Laboratory 3

Java Interview Questions and Answers

Event-Driven Programming

Decision-making Computer Science Lesson to Prepare for UIL Computer Science Contest

Java Classes. GEEN163 Introduction to Computer Programming

Chapter 13 - Inheritance

CISC 181 Project 3 Designing Classes for Bank Accounts

CmpSci 187: Programming with Data Structures Spring 2015

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Pemrograman Dasar. Basic Elements Of Java

Primitive data types in Java

Interactive Programs and Graphics in Java

Building Java Programs

arrays C Programming Language - Arrays

For live Java EE training, please see training courses

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

Programming Fundamentals I CS 110, Central Washington University. November 2015

D06 PROGRAMMING with JAVA

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Introduction to Object-Oriented Programming

Java Basics: Data Types, Variables, and Loops

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

13 File Output and Input

Using Files as Input/Output in Java 5.0 Applications

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

UML Class Diagrams. Lesson Objectives

Transcription:

Methods Chapter 4: Methods Asserting Java Rick Mercer

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 the block access mode, return type, name, parameters 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 -orprimitive-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)