CS 307 - Specification 1: Designing and Implementing Algorithms

Programming Assignment 1: Individual Assignment. You must complete this assignment on your own. You may not discuss their work with anyone except the instructor and other members of the instructional staff (TA, section leader, or lab proctor). You may not acquire from any source (e.g., another student or an internet site) a partial or complete solution to a problem or project that has been assigned. You may not show another student your solution to an assignment. You may not have another person (current student, former student, tutor, friend, anyone) “walk you through” how to solve an assignment. Review the class policy on collaboration from the syllabus.


Description: The purposes of this assignment are:

  1. to learn the assignment guidelines
  2. to learn to use the required software tools for the course
  3. to review the basics of the Java language
  4. to create and implement non trivial algorithms
  5. to learn to create test cases for programs you write

Provided File: CodeCampSummer.java contains 4 method shells you will complete and a main method used for testing. CodeCampSummer.html is the documentation for the program.

Download CodeCampSummer.java and complete the methods  matches, mostVowels, findMajority and sharedBirthdays. You may add helper methods if you wish, but do not change the headers of the original methods.


Things To Do:

  1. Go to https://udb.cs.utexas.edu/amut/acut/ to request a CS lab account. You must remember the account name and password you choose. They will not be emailed to you. Unfortunately you do not get a confirmation email when the account is activated. You simply have to try logging in starting a day after you request the account. try logging into you turnin directory to see if the account is active. You must have this account to turn in your files, even if you do not plan on working in the lab.
  2. For more information on CS department accounts please see http://www.cs.utexas.edu/facilities/faq/accounts and http://www.cs.utexas.edu/facilities/policies. If you had a CS account the previous semester you account should renew automatically.
  3. If you forget your password and need to reset it visit this page: https://udb.cs.utexas.edu/passwd/
  4. Sign up for the class discussion group on Piazzza. (http://www.piazzza.com/) The class CS 307 under The University of Texas at Austin

    I strongly recommend you set up email alerts on Piazzza so you know whenever information ind questions are posted..

  5. Determine which IDE (interactive development environment. A software tool that helps you write software.) you want to use for CS307. I strongly recommend using Eclipse. Eclipse is available in the lab and can be downloaded for use on a personal computer. You are free to use whatever environment you like(JCreator, Bluejay, DrJava, Emacs, VI, etc.) but I strongly recommend using Eclipse. If you use Eclipse see the Eclipse help page on how to enable assertions in Eclipse and set the compliance level to Java 6.0.
  6. Download CodeCameSummer.java from the class website. Complete the four methods in the program named matches and mostVowels. See the method descriptions below.

    Assignment design in CS307: If you took CS305J, one thing that will be different is you will not always write complete programs. You will often, but not always, be given a partial program and have to complete it. You will do a lot of coding to "spec", that is coding to specification; the program will have already been designed and you must implement it given the design. You will have to develop algorithms on your own, but the specification of methods will already be complete. Many of the methods you write won't ask the user for input or do any output. The "input" to the methods will be via parameters and the "output " will be the return value. Of course you can add user input and output in a method that calls the specified method to provide a way of interactively testing the methods you write, but this is not required.

     

  7. Fill in the header for CodeCampSummer.java. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own, as required and did not share your code with anyone else. If you copy code from someone else you will receive an F in the course.

  8. Turn in your CodeCampSummer.java.java program using the turnin program. Turn in your file to your cs307 folder!


Method Descriptions and Specifications:

The matches method:

Very important: The only methods from the Java library you may use in completing method matches are the charAt and length methods from the String class and the constructor and add method from the ArrayList class.

Also, very important. The method does not do any input or output on its own. You don't "ask the program user" for the input string or print out the result. The input comes as a parameter to the method and the output is the return value from the method.

This method finds all complete matches of a target String in a source String. String matching is used in many different applications. Every time you use the find command or replace command in a word processing document or on a web page you are making use of string matching. String matching is also used in areas such as computer virus detection and mapping of DNA.

You may implement a naive, brute force string matching algorithm as described below and you will not lose points for efficiency. The intention is for you to design and implement your own algorithm. Don't look up an algorithm on the web and implement that. You should take pencil and paper and work out your own algorithm and then implement that in Java code. This is an exercise in creating a non trivial algorithm. Something you will have to do later in the class on assignments and exams.

Example of naive string matching algorithm: Assume target string is "aaa" and source string is "aaaabaaaa". The answer for this example is [0, 1, 5, 6]. Here is a detailed explanation of how to arrive at the answer for this example. The numbers indicate the position of each character in the source and target strings.

Initial lineup per naive algorithm. The numbers are the indices of each character in the Strings.

012345678
aaaabaaaa  source String
aaa        target String (looking for complete matches of target in source)
012

The above line up results in a match starting at index 0 in source.

Next slide the target down 1 spot.

012345678
aaaabaaaa
 aaa
 012

The above lineup results in a match starting at index 1 in source.

Slide the target down another spot.

012345678
aaaabaaaa
  aaa
  012

The above lineup is not a match.

Slide the target down another spot.

012345678
aaaabaaaa
   aaa
   012

The above lineup is not a match.

Slide the target down another spot.

012345678
aaaabaaaa
    aaa
    012

The above lineup is not a match.

Slide the target down another spot.

012345678
aaaabaaaa
     aaa
     012

The above lineup results in a match starting at index 5 in source.

Slide the target down another spot.

012345678
aaaabaaaa
      aaa
      012

The above lineup results in a match starting at index 6 on source.

Tip: The real complexity in this problem is that you have to keep track of two different indices. The index in the target string and the index in the source string. These two values are not equal to each other, except for the first line up.

In method matches you will be returning an ArrayList of Integer objects. The method includes the code to create the ArrayList. To add an int to the end of the ArrayList use the add method as show below.

ArrayList<Integer> result = new ArrayList<Integer>();
int index = 0;
result.add(index);
// even though it is an ArrayList of Integer objects, ints may
// be added directly (example of autoboxing)

The fastest known string matching algorithm is the Boyer-Moore String algorithm. If interested after you have completed your own method read sections 1- 4 from the original paper. You are NOT expected to implement the Boyer-Moore algorithm.


The mostVowels method:

On method mostVowels you can use any and all parts of the Java standard library you choose. It can be completed relatively easily using arrays and some methods from the String class.

The mostVowels methods takes in an array of Strings as a parameter and determines which String has the most vowels.

For this method vowels are the characters 'A', 'a', 'E', 'e', 'I', 'i',  'O', 'o', 'U', and 'u'. The method is not trying to determine which String has the largest number of distinct vowels. Thus "aaaaaa" has more vowels that "aeiou". "aaaaaa" has 6 vowels while the String "aeiou" only has 5 vowels. You can use whatever String methods you want when completing this method.

/**
* Determine the index of the String that
* has the largest number of vowels.
* Vowels are defined as <tt>'A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', and 'u'</tt>.
* The parameter <tt>list</tt> is not altered as a result of
* this method.
* <p>pre: <tt>list != null</tt>, <tt>list.length > 0</tt>, there is an least 1 non null element in list
* <p>post: return the index of the non-null element in list that has the
* largest number of characters that are vowels.
* If there is a tie return the index closest to zero.
* The empty String, "", has zero vowels.
* It is possible for the maximum number of vowels to be 0.
* @param list the array to check
* @return the index of the non-null element in list that has the greatest number of vowels.
*/
public static int mostVowels(String[] list)


The findMajority method

Very important. You may not use any other Java classes when completing this method. You can use native arrays and of course the array length field. (but not methods from the Arrays class.)

This method determines if an array of ints contains a majority element.

/**
* Determine if there is a majority element in an array of ints.
* The parameter list is not altered as a result of this
* method.
* @param list != null
* @return the first index of the first occurrence of the majority element if it exists.
* If a majority element does not exist return -1.
*/

A majority element in an array A of size N is an element that appears more than N/2 times. Thus there is at most one such element. For example the array

index   0  1  2  3  4  5  6  7
element 3, 4, 4, 4, 2, 4, 2, 4

has a majority element equal to 4. 

Note your method returns the index of the first occurrence of the majority element if one exists. If there is no majority element your method returns -1.

The following array does not have a majority element: 

index   0  1  2  3  4  5  6  7
element 3, 3, 4, 4, 2, 4, 2, 4

To be a majority element it must appear more than N/2 times where N is equal to the list. With a list of length 8 there must be at least 5 elements equal to one another for there to be a majority element. Your method shall return -1 if an array does not contain a majority element.
 


The sharedBirthdays method:

Very important. You may not use any other Java classes when completing this method except Random and Math. You can use native arrays and of course the array length field. (but not methods from the Arrays class.)

The birthday problem is a question where most people's intuition is proved wrong by mathematics. The problem is: Given a group of N people, how large must N be so that there is a 50% chance that at least 2 of the N people have the same birthday?

The method takes in two parameters, the number of people in a group and the number of days in the year. The method will generate random birthdays for the number of people and then determine how many pairs of people have the same birthday. You don't have to generate actual days of the year for the birthdays. You can simply use ints.

Here are two ways to generate random ints in Java. One uses an object of type Random and the other uses the random method from the Math class.

// fist approach
Random r = new Random();
int max = 10;
int x = r.nextInt(max);
// x will now hold a value between 0 and 9 inclusive.
// The distribution of values in uniform.

// second approach
int max = 10;
int x = (int) (Math.random() * max);
// x will now hold a value between 0 and 9 inclusive.
// The distribution of values in uniform.

If three people (Olivia, Kelly, Isabelle) share the same birthday that is 3 pairs of people:

After completing the method run the following experiments:

Perform 20,000 experiments with 365 days per year and 182 people . What is the average number of pairs of people with shared birthdays? (You should write a method to automate this experiment.)

Perform 20,000 experiments with 365 days per year and vary the number of people from 2 to 100. (Total of  1,980,000 runs, 20,000 * 99 = 1,980,000) For each of the given number of people determine the percentage of experiments were at least one pair of people shared a birthday. At what number of people (between 2 and 100) does the percentage first exceed 50%?

Include the results of these experiments in the comment at the top of your program.


Testing: The file CodeCampSummer.java contains a main method with some tests for the matches, mostVowels, and findMajority methods and some simple tests for sharedBirthdays. It is difficult to write tests for sharedBirthdays because it involves unpredictable behavior.

Add at least 5 more tests for each of the matches, mostVowels, and findMajority methods. (15 new tests total) You may post your tests to the class discussion group and you may use tests that other people post to the listserv.


Fill in the header for CodeCampSummer.java. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own, as required and that you did not share your code with anyone else. If you copy your code from another source (internet, another student in the course, another student who previously took the course) you will be guilty of academic dishonesty and will receive am F in the course.

When finished turn in your CodeCampSummer.java program using the turnin program.
 


Checklist: Did You:

  1. review the general assignment requirements?
  2. work on the assignment individually?
  3. fill in the header in your file CodeCampSummer.java? (Eclipse often compresses the comment. Be sure to expand it and fill it in.)
  4. complete the four methods: matches, mostVowels, findMajority, and sharedBirthdays?
  5. adhere to the restrictions when completing the methods?
  6. add 5 test cases each for the matches, mostVowels and findMajority methods?
  7. run the experiments and put the results in a comment at the top of your code.
  8. turn in your Java source code in a file named CodeCampSummer.java to the proper account in the Microlab via the turnin program before 11 pm, Monday, June 20?  Turn in the source code, (the .java file) not the byte code (the .class file).

Back to the cs307 homepage.