1 Library Methods and Recursion Instructor: Mainak Chaudhuri

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Java Syntax. Basic Output public class test1 { public static void main(String[] args) { System.out.println("Hello"); }
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
1 Selection Sort and Quick Sort Instructor: Mainak Chaudhuri
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return; double ; System.out.printf reading: 3.2, 3.5, 4.4 videos: Ch.
Topic 10 return values, Math methods Based on slides bu Marty Stepp and Stuart Reges from " Thinking like a computer.
LAB 10.
Computer Programming Lab(4).
1 Methods Instructor: Mainak Chaudhuri
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
Copyright © Curt Hill Mathematics Functions An example of static methods.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Java Program Components Java Keywords - Java has special keywords that have meaning in Java. - You have already seen a fair amount of keywords. Examples.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 7: Return values, Math, and double reading: 3.2,
Solving Quadratic Equations by Factoring. Solution by factoring Example 1 Find the roots of each quadratic by factoring. factoring a) x² − 3x + 2 b) x².
1 Conditionals Instructor: Mainak Chaudhuri
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Variables, Arithmetic, etc.)
1 Operators and Expressions Instructor: Mainak Chaudhuri
Building Java Programs Chapter 3 Parameters and Objects Copyright (c) Pearson All rights reserved.
Today’s Material Recursive (Divide & Conquer) Algorithms Design
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
1 Announcements B7 tutorial today in CS103 –In CSE department –Ask at entry desk for direction to CS103.
Problem Set 5: Problem 2 22C:021 Computer Science Data Structures.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
1 Examples of Recursion Instructor: Mainak Chaudhuri
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
Take out a piece of paper and PEN. The quiz starts ONE minute after the tardy bell rings. You will have 45 – 90 seconds per question. Determine the output.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
RECURSION, CONTINUED Lecture 8 CS2110 – Fall 2015.
1 Examples of class Instructor: Mainak Chaudhuri
1 Arrays: Matrix Renamed Instructor: Mainak Chaudhuri
1 Conditionals Instructor: Mainak Chaudhuri
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Copyright 2011 by Pearson Education Building Java Programs Chapter 3 Lecture 3-2: Return values, Math, and double reading: 3.2,
CSE 110: Programming Language I Afroza Sultana UB 1230.
Chapter 2 Clarifications
Department of Computer Science
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Recursion notes Chapter 8.
Building Java Programs
Building Java Programs
Math Methods that return values
Building Java Programs
Something about Java Introduction to Problem Solving and Programming 1.
Starting Out with Java: From Control Structures through Objects
Computing Adjusted Quiz Total Score
Exam 2 Review 1.
Java Variables, Types, and Math Getting Started
Code Animation Examples
Recursive GCD Demo public class Euclid {
Chapter 5 Methods.
Take out a piece of paper and PEN.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Methods and Data Passing
Scope of variables class scopeofvars {
Methods and Data Passing
Building Java Programs
Announcement The fourth test will be 75 minutes, will consist of two parts and will take place next week. The programming part will be about Chapter 2-6,
6.2 for Loops Example: for ( int i = 1; i
Instructor: Mainak Chaudhuri
Presentation transcript:

1 Library Methods and Recursion Instructor: Mainak Chaudhuri

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