530 likes | 594 Views
Learn how to sum integers with user-defined methods and loops. Explore methods, parameters, and more in pre-AP computer science.
E N D
Warm-Up: April 21 • Write a loop (type of your choosing) that prints every 3rd 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 (inti=1; i <= 10; i++) sum = sum + i; System.out.println(“Sum from 1-10 is “ + sum); sum=0; for (inti=20; i <= 30; i++) sum = sum + i; System.out.println(“Sum from 20-30 is “ + sum); sum=0; for (inti=35; i <= 45; i++) sum = sum + i; System.out.println(“Sum from 35-45 is “ + sum);
Case Study: Sum the integers between 1 and 10, 20 and 30, and 35 to 45. int sum=0; for (inti=1; i <= 10; i++) sum = sum + i; System.out.println(“Sum from 1-10 is “ + sum); sum=0; for (inti=20;i <= 30; i++) sum = sum + i; System.out.println(“Sum from 20-30 is “ + sum); sum=0; for (inti=35; i <= 45; i++) sum = sum + i; System.out.println(“Sum from 35-45 is “ + sum);
Case Study – Using Methods public static int sum(int start, int end) { int sum=0; for (inti = 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 20-30 is “ + sum(20,30)); System.out.println(“Sum from 35-45 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 (inti = start; i <= end; i++) sum = sum + i; return sum; }
Dissecting a Method public static int sum(int start, int end) { int sum=0; for (inti = 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 (inti = 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 intsum(int start, int end) { int sum=0; for (inti = 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 (inti = 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 (inti = 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 (inti = 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 (inti=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!”); System.out.println(“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 intgetRandom() { int random = Math.random()*100; return random; } public static void main(String[] args) { intnewNum = 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 (inti=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(intnum) { if (num % 2 == 1) System.out.println(num + “ is odd.”); else System.out.println(num + “is even.”); } public static void main(String[] args) { intnum = 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) { intnum = 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