Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

CS 106 Introduction to Computer Science I 11 / 09 / 2007 Instructor: Michael Eckmann.
Methods in Java CSC 1401: Introduction to Programming with Java Week 7 – Lecture 2 Wanda M. Kunkle.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
Display a 12-Month Calendar CS-2301 D-term Programming Assignment #2 12-Month Calendar CS-2301 System Programming C-term 2009 (Slides include materials.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
Parameters, Arguments, Local Variables, and Scope CSC 1401: Introduction to Programming with Java Week 8 – Lecture 1 Wanda M. Kunkle.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
The printf Method The printf method is another way to format output. It is based on the printf function of the C language. System.out.printf(,,,..., );
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
1 CSC103: Introduction to Computer and Programming Lecture No 30.
Fundamentals of Python: From First Programs Through Data Structures
CS0007: Introduction to Computer Programming File IO and Recursion.
Fundamentals of Python: First Programs
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 2 2 Introduction to Java Applications Introduction Java application programming –Display messages –Obtain information from the user –Arithmetic.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Recursion.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
Introduction to algorithm design and recursion CS125 Spring 2007 Arthur Kantor.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
CS 1704 Introduction to Data Structures and Software Engineering.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C Formatted Input/Output
User-Written Functions
Chapter 15 Recursion.
Objectives You should be able to describe: Interactive Keyboard Input
Chapter 15 Recursion.
TMF1414 Introduction to Programming
Introduction to Computer Science - Alice
System.out.println for console output
Introduction to Primitive Data types
Programming Fundamental
Introduction to Primitive Data types
Presentation transcript:

Recursion Formatting Decimal Numbers CSC 1401: Introduction to Programming with Java Week 9 – Lectures 1 & 2 Wanda M. Kunkle

2 Recursion A recursive method is a method that calls itself. A recursive method is a method that calls itself. A recursive method is called to solve a problem that it divides into two (usually) smaller problems: A recursive method is called to solve a problem that it divides into two (usually) smaller problems: –A problem that it knows how to solve, the simplest, or base case –A problem that it doesn't know how to solve that is similar to but simpler than the original problem, the recursive case

3 Recursion A recursive method generally has two parts: A recursive method generally has two parts: –A termination part that stops the recursion, in which the method solves the simplest case and returns the result –A recursive call in which the method calls itself to go to work solving the simpler version of the original problem

4 Recursion The recursive method terminates when it is finally called to solve the base case, at which point all previous return values can be computed and the final result returned to the calling method. The recursive method terminates when it is finally called to solve the base case, at which point all previous return values can be computed and the final result returned to the calling method.

5 Example of Recursion An implementation of the factorial function can be used to illustrate recursion. An implementation of the factorial function can be used to illustrate recursion. Factorial can be formally defined as follows: Factorial can be formally defined as follows: For example, 5! can be rewritten as 5 * 4!, which can be rewritten as 5 * (4 * 3!), which can be rewritten as 5 * (4 * (3 * 2!)), and so on. For example, 5! can be rewritten as 5 * 4!, which can be rewritten as 5 * (4 * 3!), which can be rewritten as 5 * (4 * (3 * 2!)), and so on.

6 Sample Programs Let’s look at two different sample programs that implement the factorial function: Let’s look at two different sample programs that implement the factorial function: –iterativeFactorial.java iterativeFactorial.java –recursiveFactorial.java recursiveFactorial.java

7 Another Example of Recursion An implementation of the Fibonacci function can be used to further illustrate recursion. An implementation of the Fibonacci function can be used to further illustrate recursion. Would anyone care to define the Fibonacci numbers … Would anyone care to define the Fibonacci numbers … –Either formally or by example? –i.e., It’s your turn!

8 Another Example of Recursion The Fibonacci numbers are a sequence of numbers defined by the following recurrence relation*: The Fibonacci numbers are a sequence of numbers defined by the following recurrence relation*: (s) According to the Wikipedia, “a recurrence relation is an equation that defines a sequence recursively: each term of the sequence is defined as a function of the preceding terms.”Wikipediarecurrence relation

9 Another Example of Recursion In other words, each number after the two starting values is obtained by adding the two numbers that precede it. In other words, each number after the two starting values is obtained by adding the two numbers that precede it. The first few terms in the Fibonacci sequence are: The first few terms in the Fibonacci sequence are: –0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, … –Where F 0 = 0 and F 8 = 21

10 The Fibonacci Numbers in Nature The Fibonacci numbers occur everywhere in nature. Visit the following links to learn more about this phenomenon: The Fibonacci numbers occur everywhere in nature. Visit the following links to learn more about this phenomenon: – – ibonacci/fibnat.html ibonacci/fibnat.htmlhttp:// ibonacci/fibnat.html – Fibonacci_numbers_in_nature Fibonacci_numbers_in_naturehttp://en.wikipedia.org/wiki/Fibonacci_number# Fibonacci_numbers_in_nature

11 Sample Programs Let’s look at two different sample programs that implement the Fibonacci function: Let’s look at two different sample programs that implement the Fibonacci function: –iterativeFibonacci.java iterativeFibonacci.java –recursiveFibonacci.java recursiveFibonacci.java

12 Formatting Decimal Numbers Decimal numbers can be formatted using the printf method. Decimal numbers can be formatted using the printf method. Java 5.0 borrowed this method from the C programming language. Java 5.0 borrowed this method from the C programming language. A call to printf requires that it be provided with: A call to printf requires that it be provided with: –A format string –Optional arguments

13 Formatting Decimal Numbers Values passed to printf : Values passed to printf : –Format string Describes the format of the output Describes the format of the output May consist of: May consist of: –Fixed text –Format specifiers A format specifier is a placeholder for a value of a specific type A format specifier is a placeholder for a value of a specific type A format specifier may be used to specify precision (number of decimal places to be displayed), field width, justification, etc. A format specifier may be used to specify precision (number of decimal places to be displayed), field width, justification, etc. An example of a format specifier is %f, a placeholder for a float or double value An example of a format specifier is %f, a placeholder for a float or double value

14 Formatting Decimal Numbers Values passed to printf : Values passed to printf : –Optional arguments Arguments referenced by the format specifiers in the format string Arguments referenced by the format specifiers in the format string Example: System.out.printf("\n\nA circle of radius %f has a circumference of %f", radius, circumference); radius is referenced by the first %f, whereas circumference is referenced by the second %f Example: System.out.printf("\n\nA circle of radius %f has a circumference of %f", radius, circumference); radius is referenced by the first %f, whereas circumference is referenced by the second %f

15 Sample Programs Let’s look at two different sample programs that demonstrate formatting decimal numbers: Let’s look at two different sample programs that demonstrate formatting decimal numbers: –circumferenceAndArea.java circumferenceAndArea.java –volumeAndSurfaceAreaDemo.java volumeAndSurfaceAreaDemo.java