Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Procedural programming in Java
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Chapter 41 Defining Classes and Methods Chapter 4.
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
20-Jun-15 Methods. Complexity The programmer's biggest adversary is complexity The programmer's biggest adversary is complexity Primitive types and the.
CS 106 Introduction to Computer Science I 02 / 25 / 2008 Instructor: Michael Eckmann.
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.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements You execute those declarations and statements by calling the.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
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.
Introduction to Methods
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
COMP More About Classes Yi Hong May 22, 2015.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Comments are for people Header comments supply basic information about the artifact.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 6: User-Defined Functions
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.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Lecture 5 Methods. Sometimes we want to perform the same sequence of operations multiple times in a program. While loops allow us to do this, they are.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Classes and Objects in Java
Methods We write methods in our programs for many reasons:
Defining Classes I Part A. Class definitions OOP is the dominant programming methodology in use today. OOP is the dominant programming methodology in.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Methods. Overview Class/Library/static vs. Message/non-static return and void Parameters and arguments.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
A High Flying Overview CS139 – Fall 2006 How far we have come.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
A High Flying Overview CS139 – Fall 2010 How far we have come.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
CMSC201 Computer Science I for Majors Lecture 13 – Functions
4. Java language basics: Function
Java Programming: Guided Learning with Early Objects
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Methods.
Method Mark and Lyubo.
CS139 – Fall 2010 How far we have come
Chapter 6 Methods: A Deeper Look
The Basics of Recursion
CS139 October 11, 2004.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Methods in Java Methods & Parameters.
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Classes, Objects and Methods
Methods/Functions.
Basic Exception Handling
Presentation transcript:

Methods OR HOW TO MAKE A BIG PROGRAM SEEM SMALLER.

Recall from last time If you have a big program, you can divide it up and develop it in pieces, testing each piece as you go along. Sometimes, we want to put the code into separate modules (in this case we will call them methods).

Why?

Some standard reasons Smaller is better (7 +- 2). Reuse Quality (which comes from reuse)

Method terminology Void method Code that does something public static void doSomething( String text) doSomething(“hello”); This is the method call or invocation. This is the method definition. Examples we’ve seen: System.out.println(); Argument or actual parameter Corresponding formal parameter (or parameter)

Method terminology Value returning method Code that does something return size; Code that does something return size; public static int doSomething( String text) int length; length = doSomething(“hello”); This is the method call or invocation. This is the method definition. Examples we’ve seen: formatter.format( ); Argument or actual parameter Corresponding formal parameter (or parameter)

Method terminology Value returning method return size; public static int doSomething( String text) int length; length = doSomething(“hello”); This is the method call or invocation. This is the method definition. Examples we’ve seen: formatter.format( ); return type return statement

What’s with the dots? System.out.println() keyboard.nextInt() Math.pow(5, 5) The name of the method always goes before the parameter list(parentheses). The names before the method name itself is the qualifier for the name.

Qualified names Two kinds Name of a class, like Math or System. Name of an object of a class, like keyboard or out. A qualified name identifies where we can find the corresponding method.

Static / non-static Methods that require an object (a specific instance) of a class are termed non-static. The Scanner methods are non-static and require a particular Scanner on which to operate. Methods that can be called by just the class name are called static (there is just one of them and they don’t change). All of the Math class methods are static.

Activity today

Exercise 3 You will write “void” methods to sing Old MacDonald Had a Farm. The goal is to write the song with as few printlns as possible. But…

There are RULES Each team member must write at least one method (including main). Each line of text (including blank lines) must have its own System.out.println(). You may NOT join multiple lines of text using the \n character. You may NOT use loops, decisions, or other Java syntax we haven't yet covered in class. To simplify writing code by hand, you MAY abbreviate System.out.println as SOP.

PA2 – Is posted It’s all about methods. Think about your PA1 and the repetition in it. We will build methods to simplify the code. Part A – Stub out your program. Part B – Write the code. Part C – Reflection.