10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Methods Java 5.1 A quick overview of methods
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Chapter Five Functions
Building Java Programs
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Return values.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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.
Introduction to Computers and Programming Introduction to Methods in Java.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Math class methods & User defined methods Math class methods Math.sqrt(4.0) Math.random() java.lang is the library/package that provides Math class methods.
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CS 106 Introduction to Computer Science I 02 / 24 / 2010 Instructor: Michael Eckmann.
Review for Midterm 2 Nested loops & Math class methods & User defined methods.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
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.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
Mt. Rushmore, South Dakota CSE 114 – Computer Science I Static Methods andVariables.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5.3Math Library Functions Math library functions –perform.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
7/31: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
Review for Nested loops & Math class methods & User defined methods.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 7 User-Defined Methods.
Dr. Shady Yehia Elmashad
Examples of Classes & Objects
Introduction to Modular Programming
“Form Ever Follows Function” Louis Henri Sullivan
Object Oriented Systems Lecture 03 Method
Introduction to Methods
Dr. Shady Yehia Elmashad
Chapter 4 Procedural Methods.
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
Chapter 6 Methods: A Deeper Look
Classes & Objects: Examples
Chapter 7 Procedural Methods.
Using java libraries CGS3416 spring 2019.
Lecture 03 & 04 Method and Arrays Jaeki Song.
ITM 352 Functions.
Presentation transcript:

10/25: Methods & templates Return to concepts: methods Math class methods Program of the day

Methods recall: a method is an action; something to do. terminology: Java API, Java class library programmer-defined methods instance (global) versus local variables methods are invoked by calling a method. methods return a result. Methods bear a conceptual resemblance to functions in equations and to templates.

Methods: what they look like general format of methods: methodname ( argument ) methodname ( arg, arg ) Classname.methodname ( argument ) Classname.methodname ( arg, arg ) Names of methods look similar to names of variables & objects: they don’t start with capital letter or numbers. Methods can have one or more arguments. Multiple arguments are separated by commas. Methods retrieved from libraries have the name of the class that they are associated with in front separated by a period.

Method headers: what they look like general format of methods: privacy returntype methodname ( type parameter ) privacy returntype methodname (type prm, type prm) optional: public or private? Can this method be used outside this class or not? Parameters are templates for the arguments that the method will use as input. Parameter types specify the kind of arguments that the method will accept as input. Return types specify the kind of variables that the method will return (or output).

Sample Program public class DrawLine extends JApplet { public void paint ( Graphics g ) { int y; for ( int x = 0 ; x < 150 ; x++ ) { y = drawDot ( x ); g.drawString ( ".", x, y ); } int drawDot ( int a ) { int b; b = 2 * a + 10 ; return b; }

Math Class Methods Math class methods provide mathematical functions for us to use. A few: abs ( x ) absolute value of x exp ( x ) exponential of x; e x max ( x, y ) returns larger value ( x or y ) min ( x, y ) returns smaller value pow ( x, y ) x y, x to the y th power sqrt ( x ) square root of x

Sample Program public class CallMe extends JApplet { public void paint ( Graphics g ) { g.drawString ( " My phone number is ", 25, 25 ); for ( int x = 0 ; x < 8 ; x++ ) { if ( x == 3 ) { g.drawString ( " - ", 50 + x * 7, 40 ); continue; } g.drawString ( " " + randomNumber( 9 ), 50 + x*7, 40 ); } public int randomNumber ( int a ) { int b = 0; b = (int)( Math.random() * a + 1 ); return b; }

Program of the day Modify DrawLine to allow user input for the slope (coefficient of x) and the y-intercept. y = 2x + 10 Modify DrawLine to draw a curved line instead of a straight line (think of parabolas and x 2 functions).