CS 307Assignment 1, Introduction to Software Tools, Creating and Implementing Algorithms, Java Expressions and Syntax, and Simple Java Programs

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.

Placed online: Friday, May 30
20 points, ~2% of final grade.
Due: no later than 11 pm, Wednesday, June 25
General Assignment Requirements

Description The purposes of this assignment are:
  1. to learn the assignment guidelines and what is expected of the student when creating programs CS307.
  2. to learn to use the required software tools for the course
  3. to review the basics of the Java language including syntax, variables, and expressions
  4. to create and implement non trivial algorithms
  5. to learn to create test cases for programs you write
  • ShortCodeExamples.java contains a series of code snippets you will examine and explain
  • A1.java contains 2 methods you will complete and a main method used for testing.

  • 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.
  • 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/utcs/.
  • If you forget your password and need to reset it visit this page: https://udb.cs.utexas.edu/passwd/
     
  • Sign up for the class listserv. There is a class listserv to facilitate communication in the class. Subscribe to the listserv by sending an email to

    See this web page to log onto the UT lists system: www.utexas.edu/its/mailinglists/answers/subscriber_logon.php

    go to this web page and follow the instructions to subscribe to the listserv: www.utexas.edu/its/mailinglists/answers/subscribing.php

    The name of the list is cs307-scott@utlists.utexas.edu and the subject is CS307 class mailing list.

    I strongly recommend you set up a filter in whatever email program you use to direct messages from the listserv to a separate folder as instead of your normal inbox.


    Determine which IDE 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. Even if you used BlueJ before, I recommend moving on to Eclipse. It is a much better tool for the kinds of programs we will be writing in CS307 and on into CS315. If you are using Eclipse see the Eclipse help page on how to enable assertions in Eclipse and set the compliance level to Java 6.0.


  • Download ShortCodeExamples.java from the class website.

    There are 30 short snippets of code in this program. The program does not compile right now due to syntax errors.

    For each code snippet:

    • Record what you expect the output to be. Do this before you run the program.
    • Run the program and record what the output actually was.
    • Provide a brief explanation the actual output.  If there is a difference between your expected answer and the actual answer that is okay! Include in your explanation what you didn't understand about the code. If a snippet would result in a syntax or compile error then the expected result is "syntax error" or "compile error". If the code causes a syntax or runtime error explain why the error occurred. You do not have to explain how to correct the error.
    • Place all of your answers to expected output, actual output and explanation in a comment at the bottom of A1.java
       
  • For code that gives a compile or runtime error, simply comment out the entire snippet and run the rest of the program; do not attempt to correct the code. 
     
  • Here is an example of a code snippet, expected and actual answer.

    int x = 5;
    x = 2 + x + x;
    System.out.println(x);

    /* expected answer: 12
       actual answer: 12
       explanation. The variable x is assigned 5. The expression 2 + x + x is evaluated. x holds the value 5 so after substitution the expression is 2 + 5 + 5 which evaluates to 12. 12 is assigned to the variable x.
    */

Assignment design in CS307: If you took CS305J one thing that will be different this term is you will not always be writing entire programs from scratch. You will often, but not always, be given a partial program and have to complete it. You will also be doing a lot of "coding to spec" or specification. The program will have already been designed and you must implement it given the design. 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.

Download A1.java and complete method matches and findPrimes. You may add helper methods if you wish, but do not change the headers for either of the original methods.

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. Link to the Wikipedia article on string matching.

You may implement a naive, brute force string matching algorithm as described below and you will not lose points for efficiency.

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.

Initial lineup per naive algorithm.

aaaabaaaa
aaa

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

Next slide the target down 1 spot.

aaaabaaaa
 aaa

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

Slide the target down another spot.

aaaabaaaa
  aaa

The above lineup is not a match.

Slide the target down another spot.

aaaabaaaa
   aaa

The above lineup is not a match.

Slide the target down another spot.

aaaabaaaa
    aaa

The above lineup is not a match.

Slide the target down another spot.

aaaabaaaa
     aaa

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

Slide the target down another spot.

aaaabaaaa
      aaa

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

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 read sections 1- 4 from the original paper. You are NOT expected to implement the Boyer-Moore algorithm.


Complete method findPrimes in A1.java. You may add helper methods if you wish.

Very important. You may not use the BigInteger class from the Java Standard Library in this method. You will find the modulus operator (%) useful in determining if one integer is a factor of another.

Prime Numbers entry from Wikipedia

Another explanation of prime numbers.

Why are primes important? Here is one set of answers. The page fails to mention that prime numbers are also very important in cryptography.


The file A1.java contains a main method with some tests for methods matches and findPrimes. Some of these tests may be incorrect. You are required to find and fix any incorrect tests. Use the class listserv to share information about other students about which tests are in error and how to correct them.

Add more tests of your own to the main method. A simple way to test these methods is to create ArrayLists that hold the expected results and then use the equals method to check the returned ArrayList with the expected ArrayList. Here is an example:

ArrayList<Integer> result;
ArrayList<Integer> expected = new ArrayList<Integer>();

//test 1
result = matches("aaaa", "aaa");
expected.add(0);
expected.add(1);

if( result.equals( expected ) ) {  
    System.out.println( "passed test 1." );
}
else {  
    System.out.println( "failed test 1." );
}

//empty expected to get it ready for next test
expected.clear();


Fill in the header for A1.java. Replace <NAME> with your name. Note, you are stating, on your honor, that you did the assignment on your own, as required.

When finished turn in your A1.java program using the turnin program. Please realize that your turnin account will have multiple folders (or directories) if you are registered for more than one computer science class. (For example CS313k). Ensure you turn your assignment in to your CS307 folder. We don't have access to your other folders so it will cost you 2 slip days if we have to take the time to straighten out the mistake.

Files
File Responsibility
ShortCodeExamples.java (Javadoc for this class) Provided by me
A1.java (Javadoc for this class) Provided by me and you. Do not alter or add to the javadoc comments for methods matches and findPrimes. Add comments for your implementation. You may not alter the method headers for methods matches and findPrimes.
Checklist Did you remember to:
  • review the general assignment requirements?
  • work on the assignment individually?
  • fill in the header in your file A1.java
  • include comments about each of the 30 snippets regarding the expected output and the actual output?
  • implement the two required methods?
  • ensure your program does not suffer a compile error or runtime error?
  • identified and correct any incorrect tests provided with the assignment?
  • ensure your program passes all corrected tests in A1?
  • add your own tests to the main method of A1?
  • turn in your Java source code in a file named A1.java to the proper account in the Microlab via the turnin program before 11 pm, Wednesday, June 25 ? Please realize that your turnin account will have multiple folders (or directories) if you are registered for more than one computer science class. (For example CS313k). Ensure you turn your assignment in to your CS307 folder. We don't have access to your other folders so it will cost you 2 slip days if we have to take the time to straighten out the mistake.
Tips
  1. If working at home, start early to ensure you can get the required software installed and working, and become familiar with it in time to complete the assignment.

  2. If you are in more that one class in the Microlab be sure you turn the program into the correct class

  3. Your program must compile and run! If it does not then you lose 8 out of 20 points on the assignment (All of the correctness / functionality points.)

  4. Be sure you turn in your Java source code, not the .class file or anything else.

  5. Be sure you are turning in the right file with the correct name! There may be multiple versions of A1.java on your computer.

  6. Be sure you are turning in the right file with the correct name! (Yes I am repeating myself. If you turn in the wrong file and discover it after the due date you will either waste slip days or get a 0.)

  7. Turn your assignment in on time! Anything turned in after the deadline will result in you using up your slip days.

Back to the cs307 homepage.