10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.

Slides:



Advertisements
Similar presentations
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Advertisements

Modular Programming With Functions
Building Java Programs
Return values.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
PAC Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
1 Lecture 4: Chapter 6 - Methods Outline Introduction Program Modules in Java Math -Class Methods Method Declarations Java API Packages Random-Number Generation.
©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.
Introduction to Computers and Programming Lecture 12: Math.random() Professor: Evan Korth New York University.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
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.
Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
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 10 / 09 / 2006 Instructor: Michael Eckmann.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2003 Prentice Hall, Inc. All rights reserved Introduction Modules –Small pieces of a problem e.g., divide and conquer –Facilitate design, implementation,
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
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.
1 Lecture 3 Part 1 Functions with math and randomness.
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.
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
MATH AND RANDOM CLASSES.  The need for random numbers occurs frequently when writing software.  The Random class, which is part of the java.util class,
CSC Programming I Lecture 5 August 30, 2002.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 4 part 3 GEORGE KOUTSOGIANNAKIS.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Math Class Part of the Java.lang package. This package is from object so you do not have to import the lang package. Static: Math Class is static.
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.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
11/2: Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()
The Math Class Methods Utilizing the Important Math Operations of Java!
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Methods Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math -Class Methods 6.4.
Introduction Modules Small pieces of a problem ▴ e.g., divide and conquer Facilitate design, implementation, operation and maintenance of large programs.
Programming Fundamentals Enumerations and Functions.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
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.
9/13: Objects & Java Applets Objects: their nature –attributes & behaviors –inheritance –information hiding –classes: blueprints for objects Java Applets.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Dr. Shady Yehia Elmashad
“Form Ever Follows Function” Louis Henri Sullivan
Methods Chapter 6.
Object Oriented Systems Lecture 03 Method
Introduction to Methods
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Applet 2019/4/23.
Lecture 03 & 04 Method and Arrays Jaeki Song.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions Math.random()

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; }

1 st 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).

Math.random, more methods About DrawLine.java modifications –allow user input –draw a curve Method definitions Math.random()

Allow User Input (1 of 2) Add in an init method for two input dialog boxes. double coeff;//note that I declared these double yInt; //two variables as global. public void init () { cText = JOptionPane.showInputDialog (“Coefficient, please.”); yText = JOptionPane.showInputDialog (“Y-intercept, please.”); coeff = Double.parseDouble ( cText ); yInt = Double.parseDouble ( yText ); }

Allow User Input (2 of 2) Use coeff & yInt in the drawDot method int drawDot ( int a ) { int b; //must cast to int below b = (int) (coeff * a + yInt) ; return b; }

Draw a Curve Change the formula in the drawDot method from: int drawDot ( int a ) { int b; b = 2 * a + 10 ; return b; } to: int drawDot ( int a ) { int b; b = 2 * ( a * a ) + 10 ; return b; }

Method Definitions in SquareInt.java (simple version below), we see a method called square: public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; }

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } the method square is called in this line. Because it is in parentheses, x is the value to use as the input.

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } x fits the requirement that it must be an integer.

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } using x as the input, the method calculates a return value.

Method Definitions public void paint ( Graphics g ) { g.drawString(square(x) + “ “,25,25); } public int square ( int y ) { return y * y ; } the returned value is what replaces the square(x). This value is of type int. x * x

Example: Maximum.java (p. 212) public void init () { (stuff left out…) double max = maximum ( num1, num2, num3 ); outputArea.setText ( “Max is “ + max ); } public double maximum ( double x, double y, double z ) { return Math.max ( x, Math.max ( y, z ) ); }

Important: Coercion of Arguments Java will automatically promote variables to larger types when necessary; EX: int to double. In Math.pow, the requested types of input are double. However, Java will create a double version of an int variable to use in that method automatically. go to Java class index for Math. pow Math. pow read p carefully

Math.random() generates a random double number in the range 0.0 <= x < 1.0 To utilize this function, we use scaling & shifting. scaling: multiplying the function by a number to get the range we want. shifting: adding a number to the function to get the beginning point of the range that we want.

Math.random() EX: Math.random () shift it by 1: Math.random() + 1 makes 1 to <2 scale it by 6: Math.random()* 6 makes 0 to <5 to simulate rolling a die, we want 6 possibilities (or a range of 6), from 1 to 6. They have to be integers. (int) ( Math.random() * );

2 nd Program of the day pg. 220 RollDie.java Modify the program to allow the user to input how many times to roll the die.