
Recursion and Inheritance
Supplement
CMPU-101: Problem-Solving and
Abstraction
Jennifer Walter
Vassar College
Spring 2008
/** determines whether the given word is a palindrome */
public boolean isWordPal(String word) {
if (word.length() <= 1) {
return true;
} else if (word.charAt(0) == word.charAt(word.length()-1)) {
return this.isWordPal(word.substring(1, word.length()-1));
} else {
return false;
}
}
i. Reuse all the existing code in isWordPal(), adding two additional recursive cases,
described below.
ii. Case 1: if the string starts with a non-letter, then it is a palindrome if the sentence
minus the first character is a palindrome.
iii.Case 2: if the string ends with a non-letter, then it is a palindrome if the sentence
minus the last character is a palindrome.
iv.Finally, you shouldn't compare the first and last characters of the string until
you're sure they're both letters.
Write a recursive method named isLinePal(String line) that returns a boolean
value indicating whether line is a palindrome. Use the above method as a
starting point (add 2 more else-ifs).
Review of Generative Recursion - Lab 11
Classes as Templates for Objects
• All classes are templates for objects, aggregations of data and
behavior. The next four slides walk through the definition of a
class called Student, which is used to keep track of the
following information about a student:
– The name of the student
– The student’s six-digit identification number
– The number of credits the student has earned (which may include a
decimal fraction to account for half- and quarter-credit courses)
• Each of these values is stored in an instance variable of the
appropriate type.
• Instance variables are declared as private. All access to
these values is therefore mediated by public methods
exported by the Student class.
/**
* The Student class keeps track of the following pieces of data
* about a student: the student's name, ID number, and the number
* of credits the student has earned toward graduation.
* All of this information is entirely private to the class.
* Clients can obtain this information only by using the various
* methods defined by the class.
*/
public class Student { // Note the lack of "extends" clause
/** 2-parameter constructor
* Creates a new Student object with the specified name and ID.
* name: The student's name as a String
* id: The student's ID number as an int
*/
public Student(String name, int id) {
this.studentName = name;
this.studentID = id;
}
The Student Class
This comment describes the class as a whole.
The class header defines Student as a direct subclass of Object.
This comment describes the constructor.
The constructor sets the instance variables.
skip code
/**
* The Student class keeps track of the following pieces of data
* about a student: the student's name, ID number, the number of
* credits the student has earned toward graduation, and whether
* the student is paid up with respect to university bills.
* All of this information is entirely private to the class.
* Clients can obtain this information only by using the various
* methods defined by the class.
*/
public class Student {
/**
* Creates a new Student object with the specified name and ID.
* @param name The student's name as a String
* @param id The student's ID number as an int
*/
public Student(String name, int id) {
studentName = name;
studentID = id;
}
/**
* Returns the name of this student
*/
public String getName() {
return this.studentName;
}
/**
* Returns the ID number of this student.
*
*/
public int getID() {
return this.studentID;
}
/**
* Sets the number of credits earned.
* credits is the new number of credits earned
*/
public void setCredits(double credits) {
this.creditsEarned = credits;
}
The Student Class
These methods retrieve the value of
an instance variable and are called
getters. Because the student name
and ID number are fixed, there are
no corresponding setters.
This method cha nges the value of
an instance variable and is called a
setter.
skip code
/**
* Gets the number of credits earned.
* @return The number of credits this student has earned
*/
public double getCredits() {
return creditsEarned;
}
/**
* Sets whether the student is paid up.
* @param flag The value true or false indicating paid-up status
*/
public void setPaidUp(boolean flag) {
paidUp = flag;
}
/**
* Returns whether the student is paid up.
* @return Whether the student is paid up
*/
public boolean isPaidUp() {
return paidUp;
}
/**
* Creates a string identifying this student.
* Return the string used to display this student
*/
public String toString() {
return this.studentName +
" (#" + this.studentID + ")"; }
/* Public constants */
/** The number of credits required for graduation */
public static final double CREDITS_TO_GRADUATE = 34.0;
/* Private instance variables */
private String studentName; /* The student's name */
private int studentID; /* The student's ID number */
private double creditsEarned; /* The number of credits earned */
}
The Student Class
The toString method
tells Java how to display
a value of this class. All
of your classes should
override toString.
Classes often export named constants.
These declarations define the instance
variables that maintain the internal
state of the class. All instance variables
used in the text are private.
skip code