DOC PREVIEW
UNC-Chapel Hill COMP 401 - COMP 401 Program Assignment 2

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

401-16.1COMP 401Spring 2008Programming assignment 2Due: March 6, 2008 at 4:30 pm est (Note this is a change from the original schedule)Objectives■ Write executable assertions■ Detect program errors with assertions■ Handle error conditions using exceptions■ Document a program to COMP 401 standardsRequired readingS&W through Chapter 3This is an unusual programming assignment; the program has already been written. At the end of this document, you will find a listing of the program. It consists of two classes: PgmMain andBoundedList.Create a new program in Eclipse by creating the two classes and copying the code from the web into your classes. Try compiling the program. There should be no syntax errors. But you may encounter logic errors when you run the program.You will notice that there are three versions of the implementation methods for swap, insert, delete, search, and max. They are distinguished by the version number appended to the method name (e.g. swapV1, swapV2). The program currently uses version one of each method. You canchange versions by simply changing the lines in the specification methods marked with the comment "<<<<<<<<". Some of the methods are correct; others contain semantic errors. That is, they (sometimes) don't do what they are supposed to do. Further, while the main program checks for some kinds of errors (e.g. entering a non-integer when an integer is expected, or entering an empty line when a line of at least one character is expected), most other user errors are not handled. These include, for example, trying to insert into a full list, trying to delete from an empty list, trying to insert to or delete from an inappropriate position, or trying to perform a Dutch flag sort on an array with characters other than 'R', 'W', and 'B'. The main program is set up to catch these errors, but the various methods in the BoundedList class don't throw them yet.Your first job is to add appropriate documentation to the main class and to the BoundedList class.Some comments have been provided, but they are not up to course standards. You can list yourself as the author.Second, add the necessary executable preconditions to the specification methods in the BoundedList class (swap, insert, delete, search, and max) so that user errors are noted and dealt with in a reasonable way (e.g. an appropriate error message and no crash). The locations for these preconditions are noted in the code. Next, add executable postconditions that verify the correctness of each method. Again, the location for these postconditions is indicated in the code. For example, the following specification method for square root has the precondition that the argument must be greater than401-16.2or equal to zero, and the postcondition that the returned value is sufficiently close to the true square root. The additional precondition that the argument must be numeric cannot be handled by the method.// Calculate the square root of x.public double sqrt(double x){final double TOLERANCE = 0.0001;assertTrue("Argument to square root must not be negative." ,x >= 0);// Call implementation method.double result = sqrtV1(x);assertTrue("Square root is not within tolerance.", Math.abs(result*result - x) < TOLERANCE); return result;}Finally, add an executable loop invariant to the Dutch flag sort method where indicated. You should also think about and write loop invariants for the search and max loops, but these do not have to be implemented in your code. Where would they go and what would they consist of? Would they detect the errors in the incorrect versions?The program you turn in should contain all the required assertions. For each multi-version method (swap, insert, delete, search, and max) your specification method should call a correct version of the method. You should not correct the incorrect methods.Turn in on paper1. As usual, a listing of your program and Javadoc output.2. For each of the multi version methods, indicate which are correct and which are incorrect. For the incorrect versions, indicate what's wrong and how you discovered the error (e.g. whatdata you used and what sort of violation clued you into the error.)3. What are the loop invariants for the search and max methods?Etc.On this and all remaining programs, you cannot collaborate on the actual code writing. You are free (encouraged even) to discuss this program with others, but write your own code.You are permitted, if you want, to submit your paper programs printed on both sides.Remember the rules:1. Start early!2. When all else fails, remember rule 1.401-16.3Eight pages of Java code follows (not included in the paper version).import java.io.*;import java.util.*;public class Pgm2Main {// Shorthand display methods.public static void Sn(String s) // Display; no new line.{System.out.print(s);}public static void S(String s) // Display; new line.{Sn(s+"\n");}// Strip leading and trailing blanks from a string.// String method trim could also be used.public static String blankStrip(String s){// Strip leading blanks.while (s.length()>0 && s.charAt(0)==' ') // SC evals=s.substring(1);// Strip trailing blanks.while (s.length()>0 && s.charAt(s.length()-1)==' ') // SC evals=s.substring(0,s.length()-1);return s;}// Display menupublic static void showInfo(){S("Operations:"+"\n i insert a new character onto the list."+"\n x delete a character from the list."+"\n f perform Dutch flag sort (requires all"+"\n characters to be R, W, or B)."+"\n d display current list contents."+"\n s search the list."+"\n m show maximum character on the list."+"\n ? show this menu again."+ "\n q quit.");}public static void main (String[] args)throws IOException{Scanner kb = new Scanner(System.in);String inLine=""; // User's input line.char op=' '; // User's operation.BoundedList list1 = new BoundedList(); // Bounded list // of characters.showInfo();401-16.4// User interface loopwhile (true){try{ Sn("Enter an operation (i, x, f, d, s, m, ?, or q) -> "); op=kb.nextLine().charAt(0); int pos; char c; switch (op) { case 'i': // InsertSn("Enter a character -> ");c=kb.nextLine().charAt(0);Sn("Enter a position (0..."+list1.size()+") -> ");inLine=blankStrip(kb.nextLine());pos=Integer.parseInt(inLine);list1.insert(c,pos);break; case 'x': // Deleteif (list1.size()==0) S("List is empty; cannot delete.");else{ Sn("Enter


View Full Document

UNC-Chapel Hill COMP 401 - COMP 401 Program Assignment 2

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Download COMP 401 Program Assignment 2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view COMP 401 Program Assignment 2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view COMP 401 Program Assignment 2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?