 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.

Slides:



Advertisements
Similar presentations
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Advertisements

Java Syntax. Basic Output public class test1 { public static void main(String[] args) { System.out.println("Hello"); }
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Recursion.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Computer Programming Lab(4).
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Java Programming: From the Ground Up
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Output Programs These slides will present a variety of small programs. Each program has a control structure that was introduced in this chapter. Our concern.
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Introduction to programming in java
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
(Dreaded) Quiz 2 Next Monday.
CSC111 Quick Revision.
Software Development I/O and Numbers
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
TemperatureConversion
Computer Programming Methodology Input and While Loop
10.2 Implementation and Execution of Recursive Code
Repetition-Counter control Loop
TK1114 Computer Programming
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs
Java Enter your code from FRQ to Shell
Building Java Programs
Fundamental Error Handling
The Basics of Recursion
Building Java Programs
Self study.
Module 1-10: Recursion.
Building Java Programs
Java 1/31/2017 Back to Objects.
Building Java Programs
Building Java Programs
Random Numbers while loop
Building Java Programs
Computer Science Club 1st November 2019.
Presentation transcript:

 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might do with a while loop.

 A recursive definition is given below for finding how many times the constant value 2 will go evenly into another number.  The value 1 which is added in the recursive case counts how many times recursion occurs.  This count gives the result of the division.  Notice that in the recursive case n is no longer decremented by 1.  It is decremented by 2.

 The test for the base case is an inequality rather than an equality since the values that n takes on vary depending on whether it is initially odd or even.  Recursive case: n >= 2  f(n) = 1 + f(n – 2)  Base case: n < 2  f(n) = 0

 Here is a sample implementation.  The recursive method recursiveDivisionByTwo() is typed int and takes an int parameter named dividend.  public static int recursiveDivisionByTwo(int dividend)  {  if(dividend < 2)  return 0;  else  return 1 + recursiveDivisionByTwo(dividend – 2);  }

 Building on this example, it is possible to write a recursive definition for the general division function.  This is shown on the next slide. The function has two parameters.  Adding 1 in the recursive case again counts how many times the recursion occurs.  In this definition the first parameter for the recursive case is dividend – divisor.  The amount decremented each time is always the same, but divisor can be any (positive integer) value.  The second parameter is the divisor, which is passed down unchanged through all of the calls.

 Recursive case: dividend >= divisor  f(dividend, divisor) = 1 + f(dividend – divisor, divisor)   Base case: dividend < divisor  f(dividend, divisor) = 0

 Here is a complete driver program and an implementation of this function.  As usual, this is not a general implementation, but it works for appropriate input values and illustrates the key points.  Notice that a variable retVal is used in the implementation.

 import java.util.Scanner;   public class GenRecDivProg  {  public static void main(String[] args)  {  Scanner in = new Scanner(System.in);  int retVal;  int dividend, divisor;  System.out.print("Enter the dividend: ");  dividend = in.nextInt();  System.out.print("Enter the divisor: ");  divisor = in.nextInt();  retVal = GenRecDivClass.genRecDiv(dividend, divisor);  System.out.println(retVal);  }

 public class GenRecDivClass  {  public static int genRecDiv(int dividend, int divisor)  {  int retVal = 0;   if(dividend < divisor)  return retVal;   else  {  retVal = 1 + genRecDiv(dividend - divisor, divisor);  return retVal;  }

 Just as division can be defined by repeated subtraction, finding a logarithm can be defined by repeated division.  For a given number to find the log of, which will be represented by the variable name toFindLogOf, and a given base, a simple recursive definition of the logarithm function would look like this:  Recursive case: toFindLogOf >= base  f(toFindLogOf, base) = 1 + f(toFindLogOf / base, base)   Base case: toFindLogOf < base  f(toFindLogOf, base) = 0

 The following example contains two static methods, both of which find a simple logarithm by doing repeated division.  One does so with looping and the other with recursion.  For positive input consisting of a given double toFindLogOf and a given integer base, the code gives as output the largest integer value less than or equal to the logarithm for that number and base.

 import java.util.Scanner;   public class TestRecAndLoop  {  public static void main(String[] args)  {  Scanner in = new Scanner(System.in);   double toFindLogOf;  int base;  double answer;   System.out.println("What number would you like to find the log of?");  toFindLogOf = in.nextDouble();   System.out.println("What should the base of the logarithm be?");  base = in.nextInt();   answer = RecursionAndLooping.myLogLooping(toFindLogOf, base);   System.out.println("The looping answer is: " + answer);   answer = RecursionAndLooping.myLogRec(toFindLogOf, base);   System.out.println("The recursive answer is: " + answer);  }

 public class RecursionAndLooping  {  public static int myLogLooping(double toFindLogOf, int base)  {  int retVal = 0;   while(toFindLogOf >= base)  {  toFindLogOf = toFindLogOf / base;  retVal++;  }  return retVal;  }   public static int myLogRec(double toFindLogOf, int base)  {  int retVal = 0;   if(toFindLogOf < base)  return retVal;   else  {  toFindLogOf = toFindLogOf / base;  retVal = 1 + myLogRec(toFindLogOf, base);  return retVal;  }

 Note that the following version of the recursive method is shorter, but it might also be a little bit harder to understand.   public static int myLogRec(double toFindLogOf, int base)  {  int retVal = 0;  if(toFindLogOf > base)  {  toFindLogOf = toFindLogOf / base;  retVal = 1 + myLogRec(toFindLogOf, base);  }  return retVal;  }