Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © 2006 Recursive GCD Demo public class.

Slides:



Advertisements
Similar presentations
Solve problems with Java code Algorithms with Java.
Advertisements

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.
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Lecture 4 More on Java® Data Types, Control Structures.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Chapter 8: Arrays.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
LEVEL II, TERM II CSE – 243 MD. MONJUR-UL-HASAN LECTURER DEPT OF CSE, CUET Recursive & Dynamic Programming.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
2.3 Recursion do automated demo of towers of hanoi before class
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Graohics CSC 171 FALL 2001 LECTURE 16. History: COBOL Conference on Data System Languages (CODASYL) - led by Joe Wegstein of NBS developed the.
Question from the last class What happens if we cast “too large” float/double to an int? int has range float a=1e10f; int b=(int)
1 Calling within Static method /* We can call a non static method from a static method but by only through an object of that class. */ class Test1{ public.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
CS110 Programming Language I
Divisor máximo de dois inteiros. unsigned int gcd(unsigned int A, unsigned int B) { if (B > A) return gcd(B,A); else if (B==0) return A; else return gcd(B,A%B);}
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Using Classes to Store Data Computer Science 2 Gerb.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Building Java Programs
Computer Programming Lab 8.
Övning 4. Repetition göra egna klasser class Rektangel { private double längd; private double bredd; public Rektangel(double l, double b) { this.längd.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Recursion!. Can a method call another method? YES.
Computer Science - I Course Introduction Computer Science Department Boston College Hao Jiang.
Scoping and Recursion CSC 171 FALL 2001 LECTURE 8.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
Recursive. 2 Recursive Definitions In a recursive definition, an object is defined in terms of itself. We can recursively define sequences, functions.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Review of Recursion For those of you that have slept since.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
1 Examples of Recursion Instructor: Mainak Chaudhuri
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
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.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
AKA the birth, life, and death of variables.
התוכנית: using System; using System.Collections.Generic;
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Functions Used to write code only once Can use parameters.
Computing Adjusted Quiz Total Score
TO COMPLETE THE FOLLOWING:
Exam 2 Review 1.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Java Lesson 36 Mr. Kalmes.
Recursive GCD Demo public class Euclid {
AKA the birth, life, and death of variables.
class PrintOnetoTen { public static void main(String args[]) {
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Java Programming: Chapter 9: Recursion Second Edition
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Recursion Method calling itself (circular definition)
Names of variables, functions, classes
2.3 Recursion Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2010 · 2/17/11.
Presentation transcript:

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Copyright © Recursive GCD Demo public class Euclid { public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } public static void main(String[] args) { int p = Integer.parseInt(args[0]); int q = Integer.parseInt(args[1]); System.out.println(gcd(p, q)); }

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) p = 216, q = 192 environment p = 1272, q = 216 environment 1272 = 216 

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) p = 216, q = 192 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) p = 216, q = 192 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(24, 0) p = 24, q = 0 environment p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(24, 0) p = 24, q = 0 environment p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(24, 0) p = 24, q = 0 environment p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment 24

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) environment 24 p = 192, q = 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(192, 24) p = 192, q = 24 environment 24 p = 216, q = 192 environment p = 1272, q = 216 environment 24

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) 24 p = 216, q = 192 environment p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(216, 192) p = 216, q = 192 environment 24 p = 1272, q = 216 environment 24

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) 24 p = 1272, q = 216 environment

static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } gcd(1272, 216) p = 1272, q = 216 environment 24 public class Euclid { public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } public static void main(String[] args) { int p = Integer.parseInt(args[0]); int q = Integer.parseInt(args[1]); System.out.println(gcd(p, q)); } 24 % java Euclid