CS 1301 * Ch 6, Handout 1

advertisement
CS 1302 – Chapter 20 Homework
Relevant material: Recursion, Ch. 20
1. Write a driver (Ch20P01_LastName.java) that tests the methods below. Write
recursive
methods (and helpers if necessary) that:
a. Prints a message a specified number of times.
b. Prints whether a string is a palindrome or not. Use substring technique.
c. Prints whether a string is a palindrome or not. Use a helper method that doesn’t use the substring,
e.g. one that uses memory efficiently.
d. Write a method that sums an array of numbers recursively.
Turn in the driver: Ch20P01_LastName.java
2. Write a driver (Ch20P02_LastName.java) that tests the methods below. Write methods (and helpers if
necessary) that solve these problems from the text:
a. Problem 20.5
b. Problem 20.13
c. Problem 20.21
Turn in the driver: Ch20P02_LastName.java
3. Consider the code on the next pages. You will write exactly three methods as indicated by the
comments: “// write this method”. There is a Person class with subclasses Customer and Employee.
The driver and test code are provided.
a. You will modify the Person class to implement the compareTo method to compare Persons based
on their last name, and then their first name. For instance, “Zack Black” would be first, followed by
“Archie Dozier”, and then followed by “Bob Dozier”.
b. You will write a binary search that finds a Person.
Turn in the driver: Ch20P03BinarySearch_LastName.java
1
import java.util.*;
public class Ch20P03BinarySearch_LastName
{
public static void main( String[] args )
{
Person p;
int index;
Person[] persons = { new
new
new
new
new
Customer("Dave", "Gibson"),
Employee("Zane", "Mathis", 38),
Employee("Kirk", "Long", 33),
Employee("Abe", "Gibson", 22),
Customer("Ralph", "Barnes") };
System.out.println( "Original list:" + Arrays.toString( persons ) );
Arrays.sort( persons );
System.out.println( "Sorted list :" + Arrays.toString( persons ) );
// Test Case 1
p = new Customer("Dave","Gibson");
index = binarySearch( persons, p );
displayResults(persons, p, index);
// Test Case 2
p = new Employee("Dirk","Jergen", 77);
index = binarySearch( persons, p );
displayResults(persons, p, index);
}
public static int binarySearch( Person[] persons, Person key )
{
// Write this method.
return 0;
}
private static int binarySearch( Person[] persons, Person key, int low, int high )
{
// Write this method.
return 0;
}
2
public static void displayResults( Person[] persons, Person p, int index )
{
System.out.println( "***Search Results:" );
System.out.println( "Searching for Person=" + p + ", binary search index=" +
index );
if( index >= 0 ) // found item
System.out.println( "--found person:" + persons[index] + " in pos: " + index
);
else // didn't find item
{
// Find index of first item bigger than key
index = -1*index - 1;
if( index < persons.length )
System.out.println( "--not found, next person after: " +
persons[index] + " in pos: " + index );
else
// If index is bigger then size of list,
// then key is bigger than largest item.
System.out.println(
"--not found, person after all items in list" );
}
System.out.println();
}
}
abstract class Person implements Comparable
{
private String firstName, lastName;
public Person( String firstName, String lastName )
{
this.firstName = firstName;
this.lastName = lastName;
}
public String getLastName() { return lastName; }
public String getFirstName() { return firstName; }
public int compareTo( Object o )
{
// Write this method
return 0;
}
public String toString()
{
return firstName + " " + lastName;
}
}
class Customer extends Person
{
public Customer( String firstName, String lastName )
{
super( firstName, lastName );
}
3
public String toString()
{
return "Customer: " + super.toString();
}
}
class Employee extends Person
{
private int years;
public Employee( String firstName, String lastName, int years )
{
super( firstName, lastName );
this.years = years;
}
public String toString()
{
return "Employee: " + super.toString() + ", years=" + years;
}
}
4
Download