11 Methods1June 151 11 Methods CE00858-1: Fundamental Programming Techniques.

Slides:



Advertisements
Similar presentations
04 WeightLoss program1May WeightLoss program CE : Fundamental Programming Techniques.
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
08 Deterministic iteration1May Deterministic iteration CE : Fundamental Programming Techniques.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
03 Data types1June Data types CE : Fundamental Programming Techniques.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
15 Sorting1June Sorting CE : Fundamental Programming Techniques.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
01 Introduction1June Introduction CE : Fundamental Programming Techniques.
18 File handling1June File handling CE : Fundamental Programming Techniques.
05 Simple selection1June Simple selection CE : Fundamental Programming Techniques.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Procedural programming in Java Methods, parameters and return values.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
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.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
14 BirthMonth1February BirthMonth CE : Fundamental Programming Techniques.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
13 Arrays CE : Fundamental Programming Techniques June 161.
Functions + Overloading + Scope
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Chapter 6 More Conditionals and Loops
Building Java Programs
Chapter 5: Control Structures II
Object Oriented Systems Lecture 03 Method
Introduction to Methods in java
Repetition-Counter control Loop
Methods.
Chapter 5: Control Structures II
Starting Out with Java: From Control Structures through Objects
While Statement.
Chapter 4 Writing Classes.
Defining methods and more arrays
Building Java Programs
Week 4 Lecture-2 Chapter 6 (Methods).
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 03 & 04 Method and Arrays Jaeki Song.
Building Java Programs
Building Java Programs
Presentation transcript:

11 Methods1June Methods CE : Fundamental Programming Techniques

11 Methods2June 152 Objectives In this session, we will: introduce methods and look at the main() method implement methods and call them pass values into methods return values from methods

11 Methods3June 15 Methods named block of code that can be invoked one or more times in an application performs a well defined task has no side effects is easily understood advantages: can focus on what a block of code does, not how can call method as many times as needed reduces length of code simplifies testing

11 Methods4June 15 Methods in Java method header: returnType: type of data that is returned by method void if method doesn’t return anything methodName parameters enclosed by brackets: information needed by method brackets must be included even if no parameters needed method body: enclosed by braces 1 or more statements to be executed returnType methodName (parameters) { statements; } method header method body

11 Methods5June 15 main() method all Java applications must have a main method automatically invoked when program run can include calls to other methods public class MyClass { public static void main (String [] args) { //method body //calls to other methods }

11 Methods6June 15 Structure of a class public class MyClass { public static void main (String [] args) { //method body for main } public static void myMethod1 () { //method body for myMethod1 } public static void myMethod2() { //method body for myMethod2 } class may have many methods

Method implementation if a program is to be implemented using methods, need to: select meaningful name for method consider values needed by method to perform its task consider values returned by method analyse operations performed inside method body 11 Methods7June 15

11 Methods8June 15 Method example – greet() problem: a method is required to output a greeting to the user on two separate lines method: method name: greet method parameters: none method returns: nothing analysis for method body: 2 output statements to greet user public static void greet () { System.out.println("Hello and welcome"); System.out.println("How are you?"); }

11 Methods9June 15 Calling methods example – GreetUser once a method has been written it can be called as many times as required by specifying its name and any parameters required public class GreetUser { public static void main (String [] args) { greet (); } public static void greet () { System.out.println("Hello and welcome"); System.out.println("How are you?"); }

11 Methods10June 15 greet() method with parameters problem: the greet() method is to be modified so that a name is passed as a parameter to be output method: method name: greet method parameters: name – String to be output method returns: nothing analysis for method body: 2 output statements to greet user public static void greet (String name) { System.out.println("Hello " + name + " and welcome"); System.out.println("How are you?"); } parameter

11 Methods11June 15 Calling methods with parameters if the method has parameters, these must be given a value when the method is called import java.util.*; public class GreetUser { public static void main (String [] args) { Scanner kybd = new Scanner(System.in); String myName = kybd.next(); greet (myName); String yourName = kybd.next(); greet (yourName); } public static void greet (String name) { System.out.println("Hello " + name + " and welcome"); System.out.println("How are you?"); } value for parameter

11 Methods12June 15 Call by value when a value is passed into a method, a copy of it is taken and used within the method after the method finishes, changes to the value are not kept

11 Methods public class Swap { public static void main (String [] args) { int x = 3; int y = 4; System.out.println("x is: " + x + ", y is: " + y); swap(x, y); System.out.println("x is: " + x + ", y is: " + y); } public static void swap (int a, int b) { int temp = a; a = b; b = temp; } June 1513 Swap.java what is output?

11 Methods14June 15 greet() method with return value problem: the greet() method is to be modified so that a name is read inside the method, the greeting is output and the name is returned to the calling program method: method name: greet method parameters: none method returns: String name analysis for method body: create Scanner prompt user for name input name 2 output statements to greet user return name

11 Methods15June 15 public static String greet () { Scanner kybd = new Scanner(System.in); System.out.print("Enter name: "); String name = kybd.next(); System.out.println("Hello " + name + " and welcome"); System.out.println("How are you?"); return name; } return type return statement

11 Methods16June 15 Calling methods with return values if the method returns a value the calling program should do something with that value: output it store it use it in a comparison

11 Methods17June 15 import java.util.*; public class GreetUser { public static void main (String [] args) { String aName = greet (); if (aName.equals("Elizabeth")) { System.out.println("Are you the queen?"); } public static String greet () { Scanner kybd = new Scanner(System.in); System.out.print("Enter name: "); String name = kybd.next(); System.out.println("Hello " + name + " and welcome"); System.out.println("How are you?"); return name; } GreetUser.java

11 Methods18June 15 Analysis and methods when faced with a specification the designer does not consider how the solution will be implemented the use of methods is an implementation decision which should be taken after analysis

11 Methods19June 15 Method example – Underline analysis: output a row of 10 * output a row of implementation as same actions will be done, but with different values, this is a candidate for implementation using methods and parameters method name: underline() method parameters: ch – character to be output times – integer number of times character is to be output method doesn't return a value problem: a program is required to output a row of 10 stars, followed by a row of symbols

11 Methods20June 15 underline() method analysis analysis for method body: what data is used? ch: character, passed as parameter times: integer, passed as parameter what operations are performed? iteration needed as ch is output several times what operations are done once before the loop? none how many times is loop repeated? times = 1 to times what operations are done inside the loop? output ch what operations are done after the loop? output new line

11 Methods21June 15 //passing parameters to methods public class Underline { public static void main (String [] args) { underline('*', 10); 15); } public static void underline (char ch, int times) { for (int i = 0; i < times; i++) { System.out.print(ch); } System.out.println(); } Underline.java

11 Methods22June 15 Method example – Largest4 analysis what data is used? num1, num2, num3 and num4: integers input by user what operations are performed? determine larger of num1 and num2 determine larger of num3 and num4 determine larger of two previous results output largest number problem: output the largest of 4 numbers input by the user

11 Methods23June 15 larger() method analysis implementation as same actions will be done, but with different values, this is a candidate for implementation using methods and return values method name: larger() method parameters: a, b – integers to compare method returns: larger integer

11 Methods24June 15 larger() method analysis cont. method body analysis what data is used? a: integer passed as parameter b: integer passed as parameter what operations are performed? selection needed as a larger dealt with differently to b larger what operations are done before the selection? none what operations are done if a larger? store a what operations are done if b larger? store what operations are done after the selection? return larger

11 Methods25June 15 //program to show return values import java.util.*; public class Largest4 { public static void main (String [] args) { //input data Scanner kybd = new Scanner(System.in); System.out.print("Enter 4 numbers: "); int num1 = kybd.nextInt(); int num2 = kybd.nextInt(); int num3 = kybd.nextInt(); int num4 = kybd.nextInt(); //determine larger of num1 and num2 //determine larger of num3 and num4 //determine larger of two previous results //output largest System.out.println("Largest is: " + larger(larger(num1, num2), larger(num3, num4))); } June 1525 Largest4.java

11 Methods26June 15 public static int larger(int a, int b) { int largerNum; if (a > b) { largerNum = a; } else { largerNum = b; } return largerNum; } June 1526

11 Methods27June 1527June 15 Summary In this session we have: looked at implementing methods in Java seen how methods are called from within the main() method passed parameters into methods returned results from methods In the next session we will: look at making methods even more flexible