Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Library Methods and Recursion Instructor: Mainak Chaudhuri

Similar presentations


Presentation on theme: "1 Library Methods and Recursion Instructor: Mainak Chaudhuri"— Presentation transcript:

1 1 Library Methods and Recursion Instructor: Mainak Chaudhuri mainakc@cse.iitk.ac.in

2 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 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 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 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 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 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 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 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 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 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 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 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 14 Fibonacci series A second order recurrence F n = F n-1 + F n-2 for n > 2; F 1 = F 2 = 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


Download ppt "1 Library Methods and Recursion Instructor: Mainak Chaudhuri"

Similar presentations


Ads by Google