CS 1301 * Ch 6, Handout 1

advertisement
CS 1302 – Homework 9
These problems cover material from Chapters 20.
Homework Submission
There are 5 problems. The first 2 problems are worth 85 points. The last 3 are challenging and are worth 20 points
total. Make a project in Eclipse named: hw9_lastNameFirstInitial (e.g. hw9_gibsond). You can put all problems in the
same package (or default package).
1. Export your project in Eclipse as an archive file with the name: hw9_lastNameFirstInitial.zip.
2. Submit on D2L by the 1:45pm on the due date indicated on the Schedule.
Homework Problems
1.
2.
(60 points, 10 points each) Write a class called Problem1. Write the following methods (static is fine). And write a main to
test. Name the methods: method_a, method_b, etc.
a.
Find the value of 𝑝𝑤𝑟(𝑥, 𝑛) = 𝑥 0 + 𝑥 2 + 𝑥 4 + ⋯ + 𝑥 𝑚 , where m is the largest even power that is less than or equal to
n. For example: 𝑝𝑤𝑟(3,5) = 30 + 32 + 34 . Note, this would be the same as 𝑝𝑤𝑟(3,4).
b.
Find the sum of an array of integers.
c.
An integer is input. Print the integer in reverse order (without using any String or Character methods). For example,
4295 would be displayed: 5924.
d.
An integer is input. Write a method that returns a string of the digits of the integer in reverse order?
e.
Return the count of the character key in the string str. For example: countChars(“Enzeme Dentene Orf”, ‘e’) returns 5.
f.
A string is input. Return a palindrome of that string. For example, makePalindrome( “abc” ) returns “abccba”.
(25 points) 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.
Copy all the code into a class named: Problem2.
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.
1
import java.util.*;
public class Problem2
{
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;
}
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 );
2
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 );
}
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;
}
3
public String toString()
{
return "Employee: " + super.toString() + ", years=" + years;
}
}
3.
(5 points) Write a class, Problem3. Write a recursive method named printCombinations which takes an array of symbols
(strings) and an array of counts (integers) of each symbol and displays all combinations of all symbols at each integer level
up to the count. For instance, if we have symbols=(A,B) and counts=(2,3), then we want to generate all combinations of A1
and A2 with B1, B2, and B3. Some examples:
char[] symbols = new char[] {'A', 'B'};
int[] counts = new int[] { 2, 3 };
printCombinations( symbols, counts );
char[] symbols = new char[] { 'A', 'B', 'C' };
int[] counts = new int[] { 2, 3, 2 };
printCombinations( symbols, counts );
A1
B1
B2
B3
A2
B1
B2
B3
A1
B1
C1
C2
B2
C1
C2
B3
C1
C2
Output
A2
B1
C1
C2
B2
C1
C2
B3
C1
C2
4.
(10 points) Write a class named Problem4. Similar to the last problem, write a recursive method named
printCombinationsFull which takes an array of symbols (strings) and an array of counts (integers) of each symbol and
displays all combinations of all symbols at each integer level up to the count as shown below.
char[] symbols = new char[] {'A', 'B'};
int[] counts = new int[] { 2, 3 };
printCombinationsFull( symbols, counts );
char[] symbols = new char[] { 'A', 'B', 'C' };
int[] counts = new int[] { 2, 3, 2 };
printCombinationsFull( symbols, counts );
[A1,
[A1,
[A1,
[A2,
[A2,
[A2,
Ï[A1,
Ï[A1,
Ï[A1,
Ï[A1,
Ï[A1,
Ï[A1,
Ï[A2,
Ï[A2,
Ï[A2,
Ï[A2,
Ï[A2,
Ï[A2,
B1]
B2]
B3]
B1]
B2]
B3]
Output
4
B1,
B1,
B2,
B2,
B3,
B3,
B1,
B1,
B2,
B2,
B3,
B3,
C1]
C2]
C1]
C2]
C1]
C2]
C1]
C2]
C1]
C2]
C1]
C2]
5.
(5 points) Write a driver named Problem5, and a Blob class (and any others you may want). Blobs have an integer value.
Each Blob has a twin (just another Blob with a potentially different value). Blobs (and their twins are stored in an array. For
example:
Index
B(8)
0
Twin
B(2)
1
B(4)
2
Twin
B(1)
3
B(6)
4
Twin
B(9)
5
B(2)
6
Twin
B(5)
7
Write a recursive selection sort that sorts Blobs on the sum of the integer value for the Blob and its twin. For the example
above, the end result will be.
Index
B(4)
0
Twin
B(1)
1
B(2)
2
Twin
B(5)
3
B(8)
4
Twin
B(2)
5
B(6)
6
5
Twin
B(9)
7
Download