Methods and Data Passing

Slides:



Advertisements
Similar presentations
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Advertisements

©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.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
LAB 10.
Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
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.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
1 Creating Web Services Presented by Ashraf Memon Hands-on Ghulam Memon, Longjiang Ding.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
TOPIC 6 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
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.
Creating Web Services Presented by Ashraf Memon Presented by Ashraf Memon.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Classes - Intermediate
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
CS001 Introduction to Programming Day 6 Sujana Jyothi
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Looping Examples I.
Chapter 10 – Exception Handling
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
Lecture 11 B Methods and Data Passing
Starting Out with Java: From Control Structures through Objects
Computing Adjusted Quiz Total Score
Group Status Project Status.
Method Overloading in JAVA
CS2011 Introduction to Programming I Methods (II)
Chapter 6 Methods.
Chapter 5 Methods.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Methods and Data Passing
class PrintOnetoTen { public static void main(String args[]) {
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
1D Arrays and Lots of Brackets
Module 4 Loops and Repetition 4/7/2019 CSE 1321 Module 4.
Object Oriented Programming
Methods and Data Passing
Methods and Data Passing
Building Java Programs
Object-Oriented Programming and class Design
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Building Java Programs
Building Java Programs
Building Java Programs
Methods and Data Passing
MIS 222 – Lecture 12 10/9/2003.
Methods and Data Passing
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Methods and Data Passing Module 5 Methods and Data Passing 4/3/2019 CSE 1321 Module 5

Why have functions? CREATE average, userNum1, userNum2 PRINT (“Please enter the 2 numbers”) READ userNum1 READ userNum2 average = (userNum1 + userNum2) / 2 // a lot of other code // more code here, then

Header/Body Example // Header is top line public static int Sum(int num1, int num2) { // Begin the body int sum = 0; for(int i = num1; i <= num2; i++) { sum += i; } return sum; } // End the body 4/3/2019 CSE 1321 Module 4

Method Signature // Signature is Sum (int num1, int num2) public static int Sum(int num1, int num2) { int sum = 0; for(int i = num1; i <= num2; i++) sum += i; } return sum; 4/3/2019 CSE 1321 Module 4

Example (main wakes back up; average sleeps) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; double result1, result2; num1 = 5; num2 = 7; num3 = 4; result1 = average (num1, num2); // 6 result2 = average (num3, num1); // 4.5 } sleep x y Memory num1 num2 num3 double average (int x, int y) { return ( (x+y) / 2); } 5 7 4 result1 result2 6 4.5

Another Quick Example (function falls asleep; main awake) class MyClass { public static void main (String args[ ]) { int num1, num2, num3; num1 = 5; num2 = 7; num3 = 4; printNum (num1); } Output 5 myNum Memory num1 num2 num3 void printNum (int myNum) { PRINT (myNum); } 5 7 4

Psuedocode - A Bad Solution (where is the repeated code?) sum ← 0 FOR (i ← 1 i <= 10 i ← i+1) sum ← sum + i ENDFOR PRINT("Sum from 1 to 10 is “, sum) FOR (int i ← 20 i <= 30 i ← i+1) PRINT("Sum from 20 to 30 is “, sum) for (int i ← 35 i <= 45 i ← i+1) PRINT("Sum from 35 to 45 is “, sum) 4/3/2019 CSE 1321 Module 5

METHOD MAIN BEGIN CREATE result ← SUM(arguments: 1,10) PRINT("Sum from 1 to 10 is:”, result) result ← SUM(arguments: 20,30) PRINT("Sum from 20 to 30 is:”, result) result ← SUM(arguments: 35,45) PRINT("Sum from 35 to 45 is:”, result) END MAIN METHOD SUM(parameters: num1, num2) BEGIN CREATE sum ← 0 FOR (i ← num1, i <= num2, i ← i+1 ) sum ← sum + i ENDFOR RETURN sum END SUM Ps 4/3/2019 CSE 1321 Module 5

Java Example – Method Sum public static int Sum(int num1, int num2) { int sum = 0; for(int i = num1; i <= num2; i++) sum += i; } return sum; public static void main(String[] args) int result = Sum(1, 10); System.out.println("Sum from 1 to 10 is " + result); result = Sum(20, 30); System.out.println("Sum from 20 to 30 is " + result); result = Sum(35, 45); System.out.println("Sum from 35 to 45 is " + result); 4/3/2019 CSE 1321 Module 5

Psuedocode - Method Max METHOD MAIN BEGIN CREATE i ← 5 CREATE j ← 2 CREATE k ← max(i, j) PRINT("The maximum of ", i , " and ” , j ," is ", k) END MAIN METHOD MAX(parameters: num1, num2) BEGIN CREATE result if (num1 > num2) result ← num1 else result ← num2 RETURN result END MAX 10 4/3/2019 CSE 1321 Module 5

Java – Method Max public static void main(String[] args) { int i = 5, j = 2; int k = max(i, j); System.out.println("The maximum of "+i+" and "+j+" is "+k); } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } 4/3/2019 CSE 1321 Module 5

public static int math(int x, int y) { return x+y; } class Main { public static int math(int x, int y) { return x+y; } public static int math (int x) { return x*x; public static double math(double x, double y) { return x*y; public static String math (){ return "Why does this work?"; public static void main(String[] args) { System.out.println(math(7.5, 9.5)); // Prints 71.25 System.out.println (math(4)); // Prints 16 System.out.println (math()); // Prints ”Why does this work?” 4/3/2019 CSE 1321 Module 4