[] [][] [][][]
public class Triangle
{
private int width;
public Triangle(int aWidth)
{
width = aWidth;
}
public int getArea()
{
. . .
}
}
public int getArea()
{
if (width == 1) { return 1; }
. . .
}
[]
[][]
[][][]
[][][][]
Triangle smallerTriangle = new Triangle(width - 1); int smallerArea = smallerTriangle.getArea();
public int getArea()
{
if (width == 1) { return 1; }
Triangle smallerTriangle = new Triangle(width - 1);
int smallerArea = smallerTriangle.getArea();
return smallerArea + width;
}
if (width <= 0) return 0;
1 + 2 + 3 + . . . + width
double area = 0; for (int i = 1; i <= width; i++) area = area + i;
1 + 2 + . . . + n = n × (n + 1)/2 => area = width * (width + 1) / 2
public class MysteryShape
{
private int size;
public MysteryShape(int size) { this.size = size; }
public int getArea()
{
if (size == 1) return 1;
MysteryShape smallerShape = new MysteryShape(size / 2);
int area = 4 * smallerShape.getArea();
if (size % 2 == 1) area += 2 * size - 1;
return area;
}
}
What is new MysteryShape(4).getArea()?
What does this recursive method return?
public static int mystery(int n)
{
if (n % 2 == 0) // it's even
return mystery(n / 2);
else if (n > 1)
return mystery(3 * n + 1);
else
return 1;
}
It is conjectured that the function from the preceding slide always returns, but nobody knows for sure. Some values take quite a few recursive calls:
27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Complete this program to return the number of steps until you reach 1. (Each step is either dividing by two, or multiplying by 3 and adding 1.)
What is the result of calling steps(63728127)?
public class Sentence
{
private String text;
/**
Constructs a sentence.
@param aText a string containing all characters of the sentence
*/
public Sentence(String aText)
{
text = aText;
}
/**
Tests whether this sentence is a palindrome.
@return true if this sentence is a palindrome, false otherwise
*/
public boolean isPalindrome()
{
. . .
}
}
adam, I'm Ada, is a palindrome too!
public boolean isPalindrome()
{
int length = text.length();
// Separate case for shortest strings.
if (length <= 1) { return true; }
// Get first and last characters, converted to lowercase.
char first = Character.toLowerCase(text.charAt(0));
char last = Character.toLowerCase(text.charAt(length - 1));
if (Character.isLetter(first) && Character.isLetter(last))
{
// Both are letters.
if (first == last)
{
// Remove both first and last character.
Sentence shorter = new Sentence(text.substring(1, length - 1));
return shorter.isPalindrome();
}
else
{
return false;
}
}
else if (!Character.isLetter(last))
{
// Remove last character.
Sentence shorter = new Sentence(text.substring(0, length - 1));
return shorter.isPalindrome();
}
else
{
// Remove first character.
Sentence shorter = new Sentence(text.substring(1));
return shorter.isPalindrome();
}
}
Suppose we changed
if (length <= 1) { return true; }
to
if (length <= 0) { return true; }
What would be the effect of this change?
/** Tests whether a substring of the sentence is a palindrome. @param start the index of the first character of the substring @param end the index of the last character of the substring @return true if the substring is a palindrome */ public boolean isPalindrome(int start, int end)
public boolean isPalindrome()
{
return isPalindrome(0, text.length() - 1);
}
public boolean isPalindrome(int start, int end)
{
// Separate case for substrings of length 0 and 1.
if (start >= end) { return true; }
// Get first and last characters, converted to lowercase.
char first = Character.toLowerCase(text.charAt(start));
char last = Character.toLowerCase(text.charAt(end));
if (Character.isLetter(first) && Character.isLetter(last))
{
if (first == last)
{
// Test substring that doesn't contain the matching letters.
return isPalindrome(start + 1, end - 1);
}
else
{
return false;
}
}
else if (!Character.isLetter(last))
{
// Test substring that doesn't contain the last character.
return isPalindrome(start, end - 1);
}
else
{
// Test substring that doesn't contain the first character.
return isPalindrome(start + 1, end);
}
}
Consider this recursive function:
public static int mystery(int[] values, int start, int value)
{
if (start == values.length) return value;
else return mystery(values, start + 1, Math.max(value, values[start]));
}
What does mystery(new int[] { 3, 1, 4 }, 0, Integer.MIN_VALUE ) return?
14Integer.MIN_VALUE