Computer Science A 7: 27/2. Methods - subroutines Chapter 4 static return-type name ( parameters ) { statements } Call a method name ();

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
PLDI’2005Page 1June 2005 Example (C code) int double(int x) { return 2 * x; } void test_me(int x, int y) { int z = double(x); if (z==y) { if (y == x+10)
Building Java Programs
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
1 11/27/06CS150 Introduction to Computer Science 1 Searching Arrays.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh 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,
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Return function Random function Recursion function Function in C 1.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Methods We write methods in our programs for many reasons:
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
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?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Creating and Using Class Methods. Definition Class Object.
Expressions Methods if else Statements Loops Potpourri.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Lecture 17: 4/4/2003CS148 Spring CS148 Introduction to Programming II Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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.
Sum of natural numbers class SumOfNaturalNumbers {
FIGURE 4-10 Function Return Statements
Chapter 6 More Conditionals and Loops
Midterm Review Problems
Chapter 4 Procedural Methods.
Method Mark and Lyubo.
FIGURE 4-10 Function Return Statements
March 29th Odds & Ends CS 239.
The for-loop and Nested loops
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
TO COMPLETE THE FOLLOWING:
An Introduction to Java – Part I, language basics
Java so far Week 7.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS150 Introduction to Computer Science 1
More on Classes and Objects
Java Lesson 36 Mr. Kalmes.
FIGURE 4-10 Function Return Statements
Chapter 7 Procedural Methods.
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
Java Programming: Chapter 9: Recursion Second Edition
Scope of variables class scopeofvars {
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Question 1a) What is printed by the following Java program? int s;
FIGURE 4-10 Function Return Statements
Just Enough Java 17-May-19.
First Semester Review.
Presentation transcript:

Computer Science A 7: 27/2

Methods - subroutines Chapter 4 static return-type name ( parameters ) { statements } Call a method name ();

Methods Return type: void or some type If void call method as a statement If not void call it in expressions If not void the method should return a value Methods can declare local variables – can have same name as variales in other methods – they disappear when you leave Methods can have parameters

Examples static void printBlanks(int n){ for(int i=0;i<n;i++) System.out.print(” ”); } … for(int i=n/2;i>=0;i++){ printBlanks(i); System.out.print(”x”); printBlanks(n-2*i); System.out.println(”x”); }

Examples static String spaces(int n){ String s=””; for(int i=0;i<n;i++)s+=” ”; return s; } … for(int i=n/2;i>=0;i++){ System.out.println(spaces(i)+ ”x”+spaces(n-2*i)+”x”); }

Parameters Methods can have zero one or more parameters They work as local variables that are initialized when the method is called static void zero(int x){x=0;} int n=1; zero(n); // but n is still 1

Overloading You can have several methods with the same name if they have different parameter types static void check(int n){ System.out.println(”an integer”);} static void check(double d){ System.out.println(”a real number”);} static void check(String s){ System.out.println(”a string”);}

Static variables A class can have variables that can be used in all methods public class A{ static int count=0; static void main(..){..} static void method(){ count++;} } Do not such variables to return values from methods

Method calls Methods can call other methods – they can even call themselves static int fib(int n){ if(n<=2) return 1; else return fib(n-1)+fib(n-2); }

Exercises Write a capitalize function – that change the first letter in each word into uppercase Write a random function random(int n,int m) that generates a random integer between n and m (inclusive).