Answers to review questions from Chapter 2

Similar documents
Lecture Notes 4 More C++ and recursion CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Introduction to Programming Using Java (98-388)

Recursion. February 02, 2018 Cinda Heeren / Geoffrey Tien 1

Chapter 4 Defining Classes I

Recursive Definitions

Java Primer 1: Types, Classes and Operators

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

Tree traversals. Review: recursion Tree traversals. October 05, 2017 Cinda Heeren / Geoffrey Tien 1

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Queue ADT. January 31, 2018 Cinda Heeren / Geoffrey Tien 1

School of Informatics, University of Edinburgh

Methods. Bok, Jong Soon

Lecture 5: Methods CS2301

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

CS313D: ADVANCED PROGRAMMING LANGUAGE

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

The return Statement

CS113: Lecture 4. Topics: Functions. Function Activation Records

DM502 Programming A. Peter Schneider-Kamp.

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

EECS168 Exam 3 Review

Dynamic Programming. See p of the text

Unit #2: Recursion, Induction, and Loop Invariants

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

1 Lexical Considerations

There are some situations in which recursion can be massively inefficient. For example, the standard Fibonacci recursion Fib(n) = Fib(n-1) + Fib(n-2)

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

DM550/DM857 Introduction to Programming. Peter Schneider-Kamp

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

CS171:Introduction to Computer Science II

Language of the Machine Recursive functions

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

CS1 Recitation. Week 2

CS 211: Recursion. Chris Kauffman. Week 13-1

Anatomy of a static method, S&W, 2.1, figure page 188. Class.name or just name Scope, S&W, 2.1, figure page 189. Overloading

Programming & Data Structure

Methods (Deitel chapter 6)

The Java Type System (continued)

Methods (Deitel chapter 6)

CSCE 206: Structured Programming in C++

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CS111: PROGRAMMING LANGUAGE II

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

Chapter 3 - Functions

Object Oriented Methods : Deeper Look Lecture Three

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Subprograms, Subroutines, and Functions

Bioinformatics. Gabriella Trucco.

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

easel LANGUAGE REFERENCE MANUAL

Chapter 6 Introduction to Defining Classes

Lecture 3. Lecture

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

More About Objects and Methods

Variable Scope. The variable scope is the range of the program where the variable can be referenced.

CS302: Self Check Quiz 2

COS 126 General Computer Science Spring Written Exam 1

Inference rule for Induction

CS1150 Principles of Computer Science Methods

Data Structures And Algorithms

Comp215: More Recursion

CSC Java Programming, Fall Java Data Types and Control Constructs

i.e.: n! = n (n 1)

The University of North Carolina at Chapel Hill. Comp 411 Computer Organization Spring Problem Set #3 Solution Set

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Ch. 12: Operator Overloading

WYSE Academic Challenge Computer Science Test (State) 2015 Solution Set

Cloning Arrays. In practice, one might duplicate an array for some reason. One could attempt to use the assignment statement (=), for example,

CS 61B Discussion Quiz 1. Questions

Lexical Considerations

LLVM code generation and implementation of nested functions for the SimpliC language

Simple Java Programming Constructs 4

Solving problems by recursion

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

A Foundation for Programming

recursive algorithms 1

Chapter 17 Recursion

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

Wentworth Institute of Technology COMP1050 Computer Science II Spring 2017 Derbinsky. Recursion. Lecture 13. Recursion

Data Types The ML Type System

4. Java language basics: Function. Minhaeng Lee

Functional Fibonacci to a Fast FPGA

Programming Language Concepts: Lecture 2

Largest Online Community of VU Students

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

Transcription:

Answers to review questions from Chapter 2 1. Explain in your own words the difference between a method and a program. A method computes a value or performs some operation on behalf of the code for a program. A program executes computation on behalf of a user. A program typically includes many methods. 2. Define the following terms as they apply to methods: call, argument, return. In programming terminology, the process of invoking a method is referred to as calling that method. In the process of making a call, the caller can provide data to the method in the form of arguments, which are a set of local variables that are initialized from the values written inside the parentheses that designate the call. When the method completes its work, it returns to its caller, often passing back a value as a result. 3. Can there be more than one return statement in the body of a method? Yes. A method can include any number of return statements. 4. What is a static method? A static method is a method associated with a class rather than with a specific object. Static methods are called by including the class name and a dot before the method name. 5. How would you calculate the trigonometric sine of 45 using the Math class? Math.sin(Math.toRadians(45)) 6. What is a predicate method? A predicate method is a method that returns a Boolean value. 7. Describe the difference in role between an implementer and a client. The implementer is responsible for writing the actual code that carries out an operation. The client invokes that code to perform the operation but need not understand the details for doing so. 8. What is meant by the term overloading? How does the Java compiler use signatures to implement overloading? In Java, the term overloading refers to the fact that you can define several different methods with the same name, as long as each method has a different signature, which indicates the number and types of the arguments. When the Java compiler sees a call to an overloaded method with a particular name, it examines the argument values to see which version of that method is required. 9. What is a stack frame? A stack frame is the region of memory used to hold the values of local variables during a method call. The frame is created when the method is called and deleted when the method returns.

10. Describe the process by which arguments are associated with parameters. Arguments and parameters are matched by their order in the argument list. The names of the parameters have no bearing on the association process. 11. Variables declared within a method are said to be local variables. What is the significance of the word local in this context? Local variables can be used only within that method. Neither the caller nor any method called from inside a method has access to those local variables. 12. What is the difference between iteration and recursion? Iteration refers to the process of performing repeated calculations by using a loop to execute the same code. 13. What is meant by the phrase recursive leap of faith? Why is this concept important to you as a programmer? The recursive leap of faith refers to the idea that, in writing a recursive solution, you can assume that smaller instances of the problem work and then reassemble those solutions to solve the original problem. If you fail to adopt the recursive leap of faith, you are forced to trace the operation of the program all the way down to the simple cases, which often makes the process too complex to follow. 14. In the section entitled Tracing the recursive process, the text goes through a long analysis of what happens internally when fact(4) is called. Using this section as a model, trace the execution of fib(3), sketching out each stack frame created in the process. Step 1: Step 2: Step 3:

Step 4: Step 5: Step 6 Step 7: Step 8:

Step 9: Step 10: Step 11: Step 12: Step 13: Step 14:

15. What is a recurrence relation? A recurrence relation is an expression defining each new element of a sequence in terms of earlier elements. 16. Modify Fibonacci s rabbit problem by introducing the additional rule that rabbit pairs stop reproducing after giving birth to three litters. How does this assumption change the recurrence relation? What changes do you need to make in the simple cases? The new recurrence relation must subtract out the rabbits that are too old to reproduce, which are all of those born more than four months ago. The recurrence relation therefore looks like this: t n = t n-1 + t n-2 t n-5 The simple case must now take account of the fact that the value of n may now range into negative territory, when there were no rabbits. The code for the revised rabbit problem looks like this: private int rabbits(int n) { if (n <= 0) { return 0; else if (n == 1) { return 1; else { return rabbits(n - 1) + rabbits(n - 2) - rabbits(n - 5); 17. How many times is fib(1) called when fib(n) is calculated using the recursive implementation given in Figure 2-5? The method fib(1) is called fib(n) times. 18. What is a wrapper method? Why are wrapper methods often useful in writing recursive methods? A wrapper method is a method whose only role is to set things up for a call to a more general recursive method that does all the work. In many situations, the recursive process requires additional state information that the wrapper method can provide. 19. What would happen if you eliminated the if (n == 1) check from the method additivesequence, so that the implementation looked like this: int additivesequence(int n, int t0, int t1) { if (n == 0) return t0; return additivesequence(n - 1, t1, t0 + t1); Would the method still work? Why, or why not? The method would still work. In the absence of the check, the method calls itself recursively with 0 as the first argument and the value of t1 as the second. Since n will then be 0 in the recursive call, the method will return the original value of t1, just as before.