warm-up: april 21 write a loop (type of your choosing) that prints every 3 rd number between 10 and...

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

Upload: alvin-hoover

Post on 24-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Warm-Up: April 21

• Write a loop (type of your choosing) that prints every 3rd number between 10 and 50.

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

User-Defined MethodsPre-AP Computer Science, Cycle 6

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

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 20-30 is “ + sum);

sum=0;for (int i=35; i <= 45; i++)

sum = sum + i;System.out.println(“Sum from 35-45 is “ + sum);

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

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 20-30 is “ + sum);

sum=0;for (int i=35; i <= 45; i++)

sum = sum + i;System.out.println(“Sum from 35-45 is “ + sum);

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

Case Study – Using Methodspublic 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 20-30 is “ +

sum(20,30));System.out.println(“Sum from 35-45 is “ +

sum(35,45));}

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

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()

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

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

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

Why use methods?

• Like loops, they help make code • simpler, • shorter, • more efficient, • and more organized

• Modularization!

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

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;

}

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

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)

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

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)

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

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)

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

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)

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

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

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

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”

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

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)

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

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

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

Calling a Methodpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

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)?

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

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

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

Type I MethodsPre-AP Computer Science, Cycle 6

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

Methods - Reviewpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

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

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

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

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

Example – Type I Method• Method that outputs a name

public static void name() {System.out.println(“Rachel Alexander”);

}

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

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!”);

}

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

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));}

}

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

Warm-Up: April 24

• Write a method called warmup() that prints the word “warmup” 3 times

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

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!”);

}

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

Type II and III MethodsPre-AP Computer Science, Week 6

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

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

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

Example – Type I Method• Method that outputs a name

public static void name() {System.out.println(“Rachel Alexander”);

}

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

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

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

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);

}

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

Type II Method – Example Bpublic 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);

}}

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

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

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

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);

}

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

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);

}

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

Warm-Up: April 25

• When calling a method that returns a value, why must we set the method call equal to a variable?

Whyint biggest = max(a,b);

and notmax(a,b);

??

*When finished, turn in your warm-ups page

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

Announcements

• Test next Friday• Loops• Methods

• Last test of the school year (besides the final)• Will have one more quiz

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

Overloaded MethodsPre-AP Computer Science, Cycle 6

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

Methods - Reviewpublic static int max(int a, int b) {

if (a >= b)return a;

elsereturn b;

}

public static void main(String[] args) {int num1=45;int num2=32;int biggest = max(num1, num2);System.out.println(“Biggest was “ + biggest);

}

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

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

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

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

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

Differing Parameter Numberspublic 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);

}

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

Differing Data Typespublic 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);}

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

Template for Multiple Methodspublic class NAME{

public static void method1() {}

public static void method2(){}

public static void main(String[] args){}

}