Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.

Slides:



Advertisements
Similar presentations
CS110 Programming Language I
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?
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,
Horstmann chapter 8 continued. Sorting arrays The telephone book is easy to use, because the entries are sorted Sorting is a common task, and many many.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 7: User-Defined Methods
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
Instructions To use this template: –for each slide write the correct answer on the orange bar first –choose which option (A,B,C or D) and make sure you.
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.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
Procedural programming in Java Methods, parameters and return values.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
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.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
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.
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 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Classes - Intermediate
CSCI S-1 Section 8. Coming Soon Problem Set Four (72 + 5/15 points) – Tuesday, July 21, 17:00 EST.
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.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods Dr. Musab Zghoul.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Methods.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 07 Title: Methods, part 1 Reference: MalikFarrell, chap 1, Liang Ch 5.
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 5: Control Structures II
Chapter 6: Methods CS1: Java Programming Colorado State University
Chapter 6 Methods 1.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
TK1114 Computer Programming
Control Statement Examples
Group Status Project Status.
CS 200 Loops Jim Williams, PhD.
Chapter 6 Methods.
Chapter 5 Methods.
Methods and Data Passing
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Building Java Programs
Factoring if/else code
Methods and Data Passing
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Warm-Up: April 21 Write a loop (type of your choosing) that prints every 3 rd number between 10 and 50.

User-Defined Methods Pre-AP Computer Science, Cycle 6

Case Study: Sum the integers between 1 and 10, 20 and 30, and 35 to 45. int sum=0; for (int i=1; i <= 10; i++) sum = sum + i; System.out.println(“Sum from 1-10 is “ + sum); sum=0; for (int i=20; i <= 30; i++) sum = sum + i; System.out.println(“Sum from is “ + sum); sum=0; for (int i=35; i <= 45; i++) sum = sum + i; System.out.println(“Sum from is “ + sum);

Case Study: Sum the integers between 1 and 10, 20 and 30, and 35 to 45. int sum=0; for (int i=1; i <= 10; i++) sum = sum + i; System.out.println(“Sum from 1-10 is “ + sum); sum=0; for (int i=20; i <= 30; i++) sum = sum + i; System.out.println(“Sum from is “ + sum); sum=0; for (int i=35; i <= 45; i++) sum = sum + i; System.out.println(“Sum from is “ + sum);

Case Study – Using Methods public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } public static void main(String[] args) { System.out.println(“Sum from 1-10 is “ + sum(1,10)); System.out.println(“Sum from is “ + sum(20,30)); System.out.println(“Sum from is “ + sum(35,45)); }

Methods Associated with classes Define what objects are capable of doing Actions Essentially any command that ends with a pair of parentheses (empty or otherwise) System.out.print() Math.max() console.nextInt()

User-Defined Methods The methods we’ve used so far have come pre- written, stored in other files called packages Usually have to import those files into our programs in order to use those pre-written methods (java.util.* to use Scanner) However, we can write our own methods! User-defined methods Examples of possibly useful methods to write Sum of integers between two numbers isPrime isEven or isOdd

Why use methods? Like loops, they help make code simpler, shorter, more efficient, and more organized Modularization!

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; }

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } Modifier – states whether the method is public or private (for our class, ALWAYS PUBLIC STATIC)

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } Return type – data type of the information the method will return (int, double, String, void)

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } Name of the method – this is the name that will be used when we call the method later (use it)

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } Parameters – list of variables that we will pass to the method for it to use in the body code (may or may not be parameters)

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } Method body – statements associated with the method to make it perform the necessary task

Dissecting a Method public static int sum(int start, int end) { int sum=0; for (int i = start; i <= end; i++) sum = sum + i; return sum; } Method return – variable or value the method should return as its “answer”

4 Types of Methods Methods may or may not return values Methods may or may not accept parameters 4 type of methods No parameters OR returns (Type I) No parameters WITH returns (Type II) Parameters with NO returns (Type III) Parameters AND returns (Type IV)

Calling a Method Defining a method does not mean it is automatically used You must call the method within the main method in order to use it The method executes, then returns to the same spot in the main method where it was called

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Calling a Method public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Warm-Up: April 22 If you were to write a method called “isOdd”, which determines whether a number is odd or not, What would be your parameter(s) (if any)? What would be your return (if any)?

Mastery Level: Awesome Passes ALL LEFT-OVER ML:A PASSES NOT USED FOR AN EXEMPTION FOR AN IN-CLASS OR FINAL EXAM MAY BE REDEEMED FOR EXTRA CREDIT ON ANY EXAM AT THE FOLLOWING EXCHANGE RATE: 1 PASS = 2 E.C. POINTS *Note: Exam scores may not exceed 100

Type I Methods Pre-AP Computer Science, Cycle 6

Methods - Review public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Parameters and Returns Parameters Information passed to the method from the main method What goes inside the parenthesis max(a,b)  a,b are parameters Returns The answer the method gives back max(a,b)  the largest would be the return isPrime(num)  true or false

Type I Methods No parameters OR returns Return type will ALWAYS be void Parenthesis will ALWAYS be empty NO return statement Used exclusively to carry out an action Output a message Perform a uniform calculation AND output the answer Uniform  numbers never change

Example – Type I Method Method that outputs a name public static void name() { System.out.println(“Rachel Alexander”); }

Example – Calling name() public static void name() { System.out.println(“Rachel Alexander”); } public static void main(String[] args) { System.out.print(“Hello “); name(); System.out.println(“Nice to meet you!”); }

Example 2 – Powers of 2 Write a method that prints the first 10 powers of 2 public static void powersOf2( ) { for (int i=1; i<=10; i++) { System.out.println(pow(2,i)); }

Warm-Up: April 24 Write a method called warmup() that prints the word “warmup” 3 times

Warm-Up: April 24 Write a method called warmup() that prints the word “warmup” 3 times public static void warmup() { System.out.println(“Warmup!”); }

Type II and III Methods Pre-AP Computer Science, Week 6

Type I Methods - Review No parameters OR returns Return type will ALWAYS be void Parenthesis will ALWAYS be empty NO return statement Used exclusively to carry out an action Output a message Perform a uniform calculation AND output the answer Uniform  numbers never change

Example – Type I Method Method that outputs a name public static void name() { System.out.println(“Rachel Alexander”); }

Type II Methods No parameters WITH returns Parenthesis will ALWAYS be empty Will ALWAYS have a return statements Return data type will match that of the return Used for uniform calculations where the answer is returned Method call MUST be set equal to a variable

Type II Method – Example A public static int getRandom() { int random = Math.random()*100; return random; } public static void main(String[] args) { int newNum = getRandom(); System.out.print(“Your new number is: “); System.out.println(newNum); }

Type II Method – Example B public static char letter() { int random = Math.random()*10; if (random < 5) return ‘a’; else return ‘b’; } public static void main(String[] args) { for (int i=0; i<50; i++) { char randLetter = letter(); System.out.println(randLetter); }

Type III Methods Parameters with NO returns Parenthesis will NOT be empty Will NOT have a return statements Return data type will ALWAYS be VOID Used for non-uniform calculations where the answer is immediately outputted Method call is NOT set equal to a variable

Type III Method – Example A public static void isOdd(int num) { if (num % 2 == 1) System.out.println(num + “ is odd.”); else System.out.println(num + “ is even.”); } public static void main(String[] args) { int num = console.nextInt(); isOdd(num); }

Type III Method – Example B public static void largest(int a, int b) { if (a >= b) System.out.println(a + “ is largest.”); else System.out.println(b + “ is largest.”); } public static void main(String[] args) { int num = console.nextInt(); int num2 = console.nextInt(); largest(num, num2); }

Warm-Up: April 25 When calling a method that returns a value, why must we set the method call equal to a variable? Why int biggest = max(a,b); and not max(a,b); ?? *When finished, turn in your warm-ups page

Announcements Test next Friday Loops Methods Last test of the school year (besides the final) Will have one more quiz

Overloaded Methods Pre-AP Computer Science, Cycle 6

Methods - Review public static int max(int a, int b) { if (a >= b) return a; else return b; } public static void main(String[] args) { int num1=45; int num2=32; int biggest = max(num1, num2); System.out.println(“Biggest was “ + biggest); }

Parameters and Returns Parameters Information passed to the method from the main method What goes inside the parenthesis max(a,b)  a,b are parameters Returns The answer the method gives back max(a,b)  the largest would be the return isPrime(num)  true or false

Overloaded Methods Occurs when you write multiple methods within the same class with the exact same name, but with different parameters Useful for situations where you may be sending different numbers of parameters, or parameters of different data types

Differing Parameter Numbers public static int average(int a, int b, int c) { return (a+b+c)/3; } public static int average(int a, int b, int c, int d) { return (a+b+c+d)/4; } public static void main(String[] args) { int a = average(14, 26, 24); int b = average(56, 63, 39, 24); System.out.println(“A: “ + a + “\nB: “ + b); }

Differing Data Types public static int max(int a, int b) { if (a >= b) return a; return b; } public static double max(double a, double b) { if (a >= b) return a; return b; } public static void main(String[] args) { int a = max(15, 78); double b = max(34.56, 23.43); System.out.println(“A: “ + a + “\nB: “ + b); }

Template for Multiple Methods public class NAME { public static void method1() { } public static void method2() { } public static void main(String[] args) { }