COW Q3 N1 - Recursion and MergeSort

advertisement

COW Q3 N1

Recursion and MergeSort

Below are the items that you will be graded on. But you should also work through the Recursion activities in codingbat. Many of your Recursion Quiz questions will be based on the codingbat problems.

Level 1

BabyNamer – You are working at a baby doll factory where each doll is produced with a unique name. For this you were asked to come up with a program to come up with names for each doll. For level 1, finish putting in code for constructor of the BabyNamer class so that it reads in the list of first names and the list of last names from a file and stores them in the corresponding two ArrayLists. Also have the constructor create ( = new ) the fullNames ArrayList. Then program the following methods.

You do not need to use recursion to program them.

BabyNamer(String firstNameFileName, String lastNameFileName) – Constructor

 printFirstNames()

 printLastNames() print out the ArrayList of first names print out the ArrayList of last names

 getRandomFirstName()

 getRandomLastName() returns a randomly chosen first name returns a randomly chosen last name

Level 2

BabyNamer – Complete the following methods. You do not need to use recursion to program them.

 generateRandomNames(int n) generates n number of random full names and adds them

 to the fullNames ArrayList. printList() prints the names in fullNames

 printListInReverse() prints the names in fullNames in reverse

 findLongestName() returns the longest name in fullNames

 findShortestName() returns the shortest name in fullNames

Level 3

BabyNamer – Complete the following methods:

 sort() calles MergeSort and passes in fullNames

MergeSort(ArrayList<String>) uses the recursive MergeSort algorithm to sort the names in fullNames

Level 4

Recursion Practice – Create a program that produces the following output to the screen. Each item sent to standard output should be a value stored in a variable. Each line should be produced by a separate recursive function:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,

2, 4, 6, 8, 10, 12, 14, 16, 18, 20,

1, 2, 4, 8, 16, 32, 64, 128, 256, 512,

1, 3, 7, 15, 31, 63, 127, 255, 511,

1, 4, 10, 22, 46, 94, 190, 382, 766,

1, 2, 5, 14, 41, 122, 365, 1094,

1, -10, 100, -1000, 10000, -100000,

1, -1, 1, -1, 1, -1, 1, -1, 1, -1,

2, 4, 16, 256, 65536,

Level 5

BabyNamer – Complete the following methods. You do not need to use recursion to program them.

 eliminateRepetitions () – eliminates all repetitions of names in fullNames

so

{“Mark Jones”, “Sally Doe”, “Mark Jones”, “Petter Pipper”}

becomes

{“Mark Jones”, “Sally Doe” , “Petter Pipper”}

 eliminateName(String name) – eliminates all names in fullNames that contain the specified

so when eliminateName(“Doe”) is called on the following

{“Mark Jones”, “Sally Doe”, “Mark Jones”, “Petter Pipper”}

it becomes

{“Mark Jones”, “Mark Jones”, “Petter Pipper”}

 eliminateNames(ArrayList<String> names) – eliminates all names in fullNames that contain the specified names

so when eliminateNames({“Doe”, “Petter”}) is called on the following

{“Mark Jones”, “Sally Doe”, “Mark Jones”, “Petter Pipper”}

it becomes

{“Mark Jones”, “Mark Jones”}

Download