1 / 14

Library Methods and Recursion

Library Methods and Recursion. Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in. Announcements. Lab tests next week You will get 90 minutes to solve two problems In the remaining 90 minutes your work will be graded with the tutors’ inputs Syllabus same as mid-term I. Math library.

deepak
Download Presentation

Library Methods and Recursion

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. Library MethodsandRecursion Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

  2. Announcements • Lab tests next week • You will get 90 minutes to solve two problems • In the remaining 90 minutes your work will be graded with the tutors’ inputs • Syllabus same as mid-term I

  3. Math library • Math.sqrt (x) • Takes double, returns double • Math.pow (x, n) • Takes two doubles, returns double • Math.sin (x) • Takes double, returns double • Math.cos (x) • Takes double, returns double • Check out online Math library and other libraries

  4. Roots of a quadratic class QuadraticSolver { public static void main (String arg[]) { double a=1.0, b=-2.0, c=1.0; double r1, r2; r1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a); r2 = (-b-Math.sqrt(b*b-4*a*c))/(2*a); System.out.println(“Roots are: ” + r1 + “, ” + r2); } }

  5. Velocity on inclined plane class velocityExample{ public static void main(String arg[]){ double theta=Math.PI/3.0; double g=9.81; double x=10; double velocity; velocity = Math.sqrt(2*g*Math.sin(theta)*x); System.out.println(“Final velocity: ” + velocity + “ m/s”); } }

  6. Error in eπ class errorE{ public static void main(String arg[]){ double x = Math.PI; double approx, error; approx = 1 + x + x*x/2 + Math.pow(x, 3)/6 + Math.pow(x, 4)/24; error = Math.exp(x) – approx; System.out.println(“Error up to fourth power: ” + error); } }

  7. Sum of vectors class vectorSum{ public static void main(String arg[]){ double v1 = 10.0; double v2 = 21.2; double angle = 120; // In degrees double resultant; resultant = Math.sqrt(v1*v1 + v2*v2 - 2*v1*v2*Math.cos(angle*Math.PI/180.0)); System.out.println(“Resultant of ” + v1 + “ and ” + v2 + “ at ” + angle + “ degrees is ” + resultant); } }

  8. Which one is bigger? class whichOneIsBigger{ public static void main(String arg[]){ double x = Math.PI; double y = Math.E; double difference; difference = Math.exp(x) – Math.pow(x, y); System.out.println(“Difference: ” + difference); } }

  9. Adiabatic expansion class Adiabatic{ public static void main(String arg[]){ double P1 = 10; double P2 = 15.6; double V1 = 100; double V2; double gamma = 1.66; V2 = V1*Math.pow((P1/P2), 1.0/gamma); System.out.println(“New volume: ” + V2); } }

  10. Recursion • Recursion is a process of calling the same method from the method body: self-reference class recurExample { public static void main (String arg[]) { f (10); } public static void f (int n) { System.out.println(n); if (n > 0) { f(n-1); } } }

  11. Recursion class AnotherExample { public static void main (String arg[]) { f (10); } public static void f (int n) { System.out.println(n); if (n > 0) { f(n-1); } System.out.println(n); } }

  12. Recurrence relations • Recursive methods can be used to compute recurrence relations easily • Sum of the first n natural numbers satisfies the following recurrence S(n) = S(n-1) + n for n > 1; S(1) = 1

  13. Sum of first n numbers class SumOfNumbers { public static void main (String arg[]) { int n = 10; System.out.println(“Sum of the first ” + n + “ natural numbers is ” + Sum(n)); } public static int Sum(int n) { if (n == 1) return 1; // initial condition return (Sum(n-1) + n); } }

  14. Fibonacci series • A second order recurrence Fn = Fn-1 + Fn-2 for n > 2; F1 = F2 = 1 class Fibonacci { public static void main (String arg[]) { int n = 10; System.out.println(n + “th Fibonacci number is ” + Fibonacci(n)); } public static int Fibonacci (int n) { if ((1==n) || (2==n)) return 1; return (Fibonacci(n-1) + Fibonacci(n-2)); } // Try to compute the total number of } // method calls

More Related