1 Methods Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Advertisements

Graohics CSC 171 FALL 2001 LECTURE 16. History: COBOL Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the.
CS110 Programming Language I
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
1 More About Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 3 Wanda M. Kunkle.
Java Syntax Primitive data types Operators Control statements.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
CS Nov 2006 C-strings.
1 Library Methods and Recursion Instructor: Mainak Chaudhuri
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 4.3, 7.6.
1 Please switch off your mobile phones. 2 Data Representation Instructor: Mainak Chaudhuri
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
1 Text Processing. 2 Type char char : A primitive type representing single characters. –A String is stored internally as an array of char String s = "Ali.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
1 Conditionals Instructor: Mainak Chaudhuri
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Operators and Expressions Instructor: Mainak Chaudhuri
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Characters. Character Data char data type – Represents one character – char literals indicated with ' '
CS177 Week2: Recitation Primitive data types and Strings with code examples.
1 Announcements B7 tutorial today in CS103 –In CSE department –Ask at entry desk for direction to CS103.
1 CS 177 Week 3 Recitation Slides Basic Math Operations, Booleans, and Character Operations.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.1, 4.4 self-checks: #1-9.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
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.
Methods: functions & procedures. Top-down programming Divide-and-conquer technique Divide-and-conquer technique Problem is broken down into a series of.
Converting string to integer class StringToInt { public static void main (String arg[]) { // Assume arg[0] is the input string int result = 0, pos; int.
Chapter One Lesson Three DATA TYPES ©
A: A: double “4” A: “34” 4.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
WELCOME To ESC101N: Fundamentals of Computing Instructor: Ajai Jain
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Printing characters : Revisited class printcharacter{ public static void main(String arg[]){ char grade=‘A’; System.out.println(grade);// prints A System.out.println((int)grade);
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Conditionals Instructor: Mainak Chaudhuri
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Checking character case class characterCase { public static void main (String arg[]) { char c = ‘A’; if (isUpperCase(c)) { System.out.println(c + “ is.
Review by Mr. Maasz, Summary of Chapter 2: Starting Out with Java.
COM S 207 Type and Variable Instructor: Ying Cai Department of Computer Science Iowa State University
(Dreaded) Quiz 2 Next Monday.
CS1010 Discussion Group 11 Week 5 – Functions, Selection, Repetition.
Basic concepts of C++ Presented by Prof. Satyajit De
AKA the birth, life, and death of variables.
Multiple variables can be created in one declaration
Examples of class: Using System.in.read ()
Review Operation Bingo
CS 177 Week 3 Recitation Slides
Computers & Programming Languages
An Introduction to Java – Part I, language basics
Lecture 7-3: Arrays for Tallying; Text Processing
Chapter 2: Java Fundamentals
Recap Week 2 and 3.
AKA the birth, life, and death of variables.
CS 200 Primitives and Expressions
Lecture Notes – Week 4 Chapter 5 (Loops).
Building Java Programs
Scope of variables class scopeofvars {
Building Java Programs
Building Java Programs
Instructor: Mainak Chaudhuri
Presentation transcript:

1 Methods Instructor: Mainak Chaudhuri

2 Announcements Use meaningful variable names –Instead of a, b, c, … use a name that conveys the meaning of the variable: numMoonsOfJupiter, dateOfToday, … Use proper indentation in programs Batch B1 will go for tutorial today –In CS103, after this class (0900 to 1000) Mid-term I is on 5 th September Lab tests in the week of 11 th September No labs next week

3 Methods in problem solving Often we think of solving a large problem in parts –Computing the number of digits in n! involves two major steps: computing k=n! and computing the number of digits in k –So I can have two “procedures”, one computes n! and the other computes the number of digits in it –Such procedures are called methods These are just like functions –A method may or may not produce a value –The type of the produced value determines the “return type” of a method

4 Return and parameter type Can be int, float, String, double, char, or void We have seen one method so far: main It does not produce any value: void public static void PrintMyName () { System.out.println(“Tintin”); } Every method must be a part of a class

5 PrintMyName class anExample { public static void main (String arg[]) { PrintMyName();// Method call } public static void PrintMyName () { // Method declaration System.out.println(“Tintin”); }

6 Method parameters Methods can have parameters also –Just like functions class anExample { public static void main (String arg[]) { String myName = “Tintin”; PrintMyName(myName); } public static void PrintMyName (String s) { System.out.println(s); }

7 Method parameters class moreExample { public static void main (String arg[]) { int n = 5; double x = 4.6; ComputeExponent(x, n); } public static void ComputeExponent (double base, int index) { double y = 1.0; System.out.println(“Base: ” + base + “, index: ” + index); // continued in next slide

8 Method parameters if (index < 0) { base = 1/base; index = -index; } while (index > 0) { y *= base; index--; } System.out.println(“Required answer: ” + y); }

9 Producing values and using them Methods can return values to the calling method –The calling method can use these values to compute further Suppose we want to check if 2 x + 2 2x is a perfect square –Involves two major procedures: computing 2 x + 2 2x and checking if it is a square

10 Example of method class exampleOfReturnValue { public static void main (String arg[]) { int x = 4, y = 1, z; // assume x is positive boolean isSquare; z = ComputePowerOfTwo(x); y += z; z = ComputePowerOfTwo(2*x+1); // Reuse y += z; isSquare = CheckSquare(y); if (isSquare) { System.out.println(“Expression is square for x = ” + x); }

11 Example of method public static int ComputePowerOfTwo (int index) { int y = 1;// This is a different y while (index > 0) { y *= 2; index--; } return y; }

12 Example of method public static boolean CheckSquare (int x) { int y;// This is another y for (y=0; y*y <= x; y++) { if (y*y == x) { return true; } return false; } }// end class

13 More examples class Trigonometry { public static void main (String arg[]) { double error, x = 0.785;// quarter pi error = cos(x)*cos(x) + sin(x)*sin(x) – 1; System.out.println(“Error: ” + error); } // continued in next slide

14 More examples public static double cos (double theta) { return (1 – theta*theta/2 + ComputeExponent(theta, 4)/factorial(4)); } public static double sin (double theta) { return (theta – ComputeExponent(theta, 3)/factorial(3) + ComputeExponent(theta, 5)/ factorial(5)); }

15 More examples public static double ComputeExponent (double a, int n) { double y = 1.0; if (n < 0) { a = 1/a; n = -n; } while (n > 0) { y *= a; n--; } return y; }

16 More examples public static int factorial (int n) { int y = 1; while (n > 1) { y *= n; n--; } return y; } } // end class Trigonometry

17 Checking character case class characterCase { public static void main (String arg[]) { char c = ‘A’; if (isUpperCase(c)) { System.out.println(c + “ is upper case.”); } else if (isLowerCase(c)) { System.out.println(c + “ is lower case.”); } else { System.out.println(c + “ is not in English alphabet.”); } }// continued in next slide

18 Checking character case public static boolean isUpperCase (char c) { return (isBetween(‘A’, c, ‘Z’)); } public static boolean isLowerCase (char c) { return (isBetween(‘a’, c, ‘z’)); } // continued in next slide

19 Checking character case public static boolean isBetween (char a, char b, char c) { return ((a <= b) && (b <= c)); } } // end class

20 Case conversion class convertCase { public static void main (String arg[]) { char c = ‘M’; System.out.println(“Original: ” + c); if (isBetween(‘A’, c, ‘Z’)) { System.out.println(“After conversion: ” + toLower(c)); } else if (isBetween(‘a’, c, ‘z’)) { System.out.println(“After conversion: ” + toUpper(c)); } else { System.out.println(“Cannot convert case!”); } } // continued in the next slide

21 Case conversion public static boolean isBetween (char a, char b, char c) { return ((a <= b) && (b <= c)); } public static char toLower (char c) { return (c – ‘A’ + ‘a’); } public static char toUpper (char c) { return (c – ‘a’ + ‘A’); } } // end class