[] [][] [][][]
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()?
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