 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.

Slides:



Advertisements
Similar presentations
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Advertisements

 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Arrays. Last time on 4CS001 Anatomy of a Method int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to.
Mock test review Revision of Activity Diagrams for Loops,
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.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Introduction to Methods
Unit 2: Java Introduction to Programming 2.1 Initial Example.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
COMP More About Classes Yi Hong May 22, 2015.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
Introduction to Java Thanks to Dan Lunney (SHS). Java Basics File names The “main” method Output to screen Escape Sequence – Special Characters format()
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Chapter 6: User-Defined Functions
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Procedural programming in Java Methods, parameters and return values.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
ME 142 Engineering Computation I Using Subroutines Effectively.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Classes - Intermediate
Method Examples CS 139 Algorithm Development 10/06/2008.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Chapter 9 Repetition.
Chapter 2 Clarifications
Examples of Classes & Objects
Department of Computer Science
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter 5 Repetition.
CSC 113 Tutorial QUIZ I.
Chapter 9 Control Structures.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
CS110D Programming Language I
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Scope of variables class scopeofvars {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
CISC101 Reminders Assignment 3 due today.
Methods/Functions.
Presentation transcript:

 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods ◦ To be able to write Methods for use in your programs.

 Consider the following code: public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } ‘main’ program: the entry point of our code methods that the ‘main’ method will call

public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } Call to sayHelloEnters the method, executes all the code, and then returns to the main program. public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); } The code which is actually run so far...

public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = square(2); System.out.println(result); } The code which is actually run so far...

public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } Call to squareEnters the method with a parameter value 2, executes all the code, and then returns to the main program with the answer 4. public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } The code which is actually run so far...

public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } The code which is actually run so far

public class MethodDemo { public static void main (String args[]) { sayHello(); int result = square(2); System.out.println(result); } public static void sayHello() { System.out.println("Hello! "); } public static int square(int x) { return x * x; } public class MethodDemo { public static void main (String args[]) { System.out.println("Hello! "); int result = 2 * 2; System.out.println(result); } The total code which is actually run.

our method does not require any parameters  The sayHello() method in more detail: public static void sayHello() { System.out.println("Hello! "); } nothing is returned name of method empty parameter list ‘()’

 The square() method in more detail: public static int square(int x) { return x * x; } name of method this method requires an integer an integer is returned... … and the value of that integer is given by this expression

 In our main method, we have the following code: ◦ The program will:  output the word Hello  calculate the result of 2 * 2  output the result public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); }

 In our main method, we now have the following code:  The program will: ◦ output the word Hello ◦ calculate the result of 2 * 2 ◦ output the result ◦ calculate the result of 5 * 5 ◦ output the result ◦ calculate the result of 34 * 34 ◦ output the result ◦ calculate the result of 2000 * 2000 ◦ output the result public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); result = square(5); System.out.println(result); result = square(34); System.out.println(result); result = square(2000); System.out.println(result); }

public static void main(String args[]) { sayHello(); int result = square(2); System.out.println(result); result = square(5); System.out.println(result); result = square(34); System.out.println(result); result = square(2000); System.out.println(result); } public static int square(int x) { return x * x; } , ,000 4,000,000

int calculateArea(int w, int h) { int area = w * h; return area; } Parameters Used for passing data to a method Return type Defines the type of data to be passed from the method. Keyword void is used here if method returns no data. Return statement Used for passing data from the method. Omitted for void methods.

int calculateArea(int w, int h) { int area = w * h; return area; } int area; area = calculateArea(2, 5); System.out.println( "Area = " + area); Parameter values Numbers 2 and 5 are transferred to the method from main. Return value Result 10 is passed from the method into variable area. 2 & 5 10

 Consider these two methods:  Both calculate the square of the number that is supplied by the input parameter.  The first simply prints out the computed value. ◦ i.e. the value is lost  The second returns the computed value ◦ i.e. the value can be used by the program that called the method public void square1 (int y) { System.out.println(y * y); } public int square2 (int y) { return (y * y); }

Four Types of Method exist: 1. Those which ‘do something’ but don’t require any data from the main program and which don’t return any data to the main program (no data in or out). 2. Those which ‘do something’ where the ‘something’ depends on data supplied by the main program but don’t return any data to the main program (data in). 3. Those which ‘do something’ and then do return resulting data to the main program (data out). 4. A combination of the last 2 – Require data from the main program and return data back to the main program (data in and out).

 A Method may require more than one Input parameter ◦ or none at all  A Method can only return a single Output return value ◦ or none at all  Some Methods may have Optional input parameters ◦ these will take Default values if not supplied

 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Thursday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm.  Not necessarily lectures in the afternoons. ◦ Some modules have lectures during the morning and workshops/tutorials in the afternoons.  Module classes are mostly on the same day (but not always).  It depends on which modules that you are taking.