1 / 23

Warm up!!

Take attendance in 420. Warm up!!. public class MoreFunctionFun { public static void main(String[] args) { int a=2, b=3, c=4; c = fun(a, b); b = fun(a, c); System.out.println( "a=" +a); System.out.println( "b=" +b); System.out.println( "c=" +c); }

ting
Download Presentation

Warm up!!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Take attendance in 420 Warm up!! publicclassMoreFunctionFun{publicstaticvoidmain(String[]args){inta=2,b=3,c=4; c=fun(a,b); b=fun(a,c); System.out.println("a="+a); System.out.println("b="+b); System.out.println("c="+c); } publicstaticintfun(intn1,intn2){ n1++; n2+=n1;returnn1*n2; }} a=2 b=63 c=18

  2. More Fun With Functions! AP Computer Science

  3. Lesson 1: functions are dumb • Functions only know what main “tells” them. • Main passes information to a function through parameters. • The function ONLY knows what the parameters tell it.

  4. The tax function doesn’t know TAX_RATE!!It ONLY knows amount Try this… publicstaticvoidmain(String[]args){finaldoubleTAX_RATE=0.06;KeyboardReaderreader=new KeyboardReader();doublesubtotal,total; subtotal=reader.readDouble("Enter Purchase Amount:"); total=subtotal+tax(subtotal);} publicstaticdoubletax(doubleamount){returnamount*TAX_RATE;}

  5. publicstaticvoidmain(String[]args){finaldoubleTAX_RATE=0.06;KeyboardReaderreader=new KeyboardReader();doublesubtotal,total; subtotal=reader.readDouble("Enter Purchase Amount:"); total=subtotal+tax(subtotal, TAX_RATE);} publicstaticdoubletax(doubleamount, double tr){returnamount*tr;}

  6. Lesson 2:functions are really dumb • Functions assume that they have been passed parameters in a certain order. • This order is defined by the function header. • Even though variable names may make it seem sensible, the function ONLY cares about the order.

  7. publicstaticvoidmain(String[]args){intleg1=3,leg2=4,hyp=5;if(pythag(hyp,leg1,leg2))System.out.println(“Right Triangle!!");elseSystem.out.println("NOT right triangle"); if(pythag(leg1,leg2,hyp))System.out.println(“Right Triangle!!");elseSystem.out.println("NOT right triangle");} publicstaticbooleanpythag(inta,intb,intc){return((a*a+b*b)==c*c);} Output:NOT right triangleRight Triangle!!

  8. Lesson 3:main doesn’t trust functions • When a function is called, it only gets a copy of the parameters’ values. • The function CANNOT change any of main’s variables. • The only change possible is if main assigns the function’s returned value into a variable.

  9. a b c 5 15 25 publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);}

  10. publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);} n2 n1 15 5 a b c 5 15 25

  11. publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);} 14 n2 n1 5 15 500 515 a b c 5 15 25

  12. publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);} n2 n1 5 15 a b c 5 15 25

  13. publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);} 14 n2 n2 n1 15 15 500 5 515 a b c 5 15 25

  14. publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);} n2 n1 14 5 a b c 5 14 25

  15. publicstaticvoid main(String[] args){int a=5, b=15, c=25; change(a, b); b=change(a, b); c = change(a, b);} publicstaticint change(int n1, int n2){ n1 *= 100; n1 += n2;return (n2-1);} 13 n2 n2 n1 500 5 514 14 14 a b c 5 14 25

  16. Function Overloading:Same name, different parameters • Every function has a namepublic static int area(int l, int w) • Your program can have 2 (or more) functions with the same name if they have a different number and/ortype of parameters.

  17. publicclass Overloading {publicstaticvoid main(String[] args) {System.out.print ("3x5 Rectangle has area: ");System.out.println(area(3,5));System.out.print ("Circle w/radius 5 has area: ");} publicstaticdouble area(int l, int w){return l*w; } }//end class Now write an “area” function for circles and call it from the main.

  18. publicclass Overloading {publicstaticvoid main(String[] args) {System.out.print ("3x5 Rectangle has area: ");System.out.println(area(3,5));System.out.print ("Circle w/radius 5 has area: ");System.out.println(area(5));} publicstaticdouble area(int l, int w){return l*w; }publicstaticdouble area(int r){returnMath.PI*r*r; }}//end class Now write an “area” function for squares and call it from the main. Output:3x5 Rectangle has area: 15.0Circle w/radius 5 has area: 78.53981633974

  19. The computer won’t be able to tell which one you are calling!! Don’t Confuse the Computer! • Overloaded functions MUST have a different number and/or type of parameters. • Having a different parameter name is not enough!! publicstaticdoublearea(intr){returnMath.PI*r*r;} publicstaticdoublearea(ints){returns*s;}

  20. Preconditions & Postconditions • When you write functions, they should be accompanied by special comments: • Preconditions: What needs to be true when the function is called. • What do the parameters represent? • What values are legal for the parameters? • Postconditions: What will be true when the function returns. • What type of value is returned? • What does it represent?

  21. Preconditions & Postconditions • These comments provide a guarantee: • If the preconditions are met, then the postconditions will be true. • If the preconditions are not met, then there is no guarantee the function will work.

  22. Example: /* * Preconditions:l & w are nonnegative integers * representing the length & width of a rectangle * Postconditions:the area of the rectangle is returned */ publicstaticdoublearea(intl,intw){returnl*w;}/* * Preconditions:r is a nonnegative integer * representing the radius of a circle * Postconditions:the area of the circle is returned */publicstaticdouble area(int r){returnMath.PI*r*r;}

  23. Pizza per Inch:Some Practice • Now write a program that helps someone make pizza purchasing decisions. • The program should: • Ask the user for the price and diameter of a large pizza • Ask the user for the price and diameter of a medium pizza • Ask the user for the price and diameter of a small pizza • Use a function to calculate the unit price (price per square inch) for each. • Inform the user of the best deal.

More Related