Courses‎ > ‎AP Computer Science 2‎ > ‎Konstantinovich‎ > ‎

2017-02-06 HW01

posted Feb 6, 2017, 7:07 AM by Samuel Konstantinovich   [ updated Feb 7, 2017, 5:54 AM ]
Goal: Basic Recursion + Getting stuff done!

Lab + Homework:
Deadline is Wed Feb 8th 8am. I will clone everyone's repo by then.
I will grade and post results. 
No late submissions will be graded without prior permission/approval.

Discussion and working on Recursion2 problems.

How do you compare floating point values for equality? Why is this different than integers?

1.1 You are writing a class called Recursion for grading purposes this must be exact.
IMPORTANT NOTE: The methods will be static.   

You are solving 1 problem recursively:


public static double sqrt(double n);
    -throws an IllegalArgumentException when n < 0. 

Now to think a little:
There is a formula to calculate square root
Guess any number for the sqrt of n. (like n/2, or even 1)

n = 100
guess = 1

Make a better_guess this way: 
better_guess =  ( n / original_guess + original_guess) / 2

guess = 50.5

(then do it again, then do it again)
guess = 26.24009900990099
guess = 15.025530119986813
guess = 10.840434673026925
guess = 10.032578510960604
guess = 10.000052895642693
Notice how fast this converges onto the correct value (10).

** You MUST make a helper function because you need a function with two parameters (n, and the guess) even though the sqrt function only takes 1 parameter.

What is a good base case? 
How do you know you found the square root?

1.2 Make your class matches the following:
public class Recursion{
public static String name(){...};
public static double sqrt(double n){...};

Note: name():  return your name: format: "Last,First" as it appears on your school documentation without spaces, or special symbols.

2. Need to study longer? (You are doing it wrong)
Let me end with a link to a video about how to study for less time and get more out of it. Even if you only watch the first 5-10 minutes you will be much smarter.
You can watch as little or as much of this video as you want, I won't ever check... but you won't increase your GPA either.  If this video helps you do better please let me know!
This pairs nicely with every article about sleeping more to remember things better...