401 16 1 COMP 401 Spring 2008 Programming assignment 2 Due 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 standards Required reading S W through Chapter 3 This 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 and BoundedList 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 can change 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 than 401 16 2 or 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 paper 1 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 what data 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 3 Eight 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 System out print s public static void S String s Sn s n Display no new line Display new line 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 eval s s substring 1 Strip trailing blanks while s length 0 s charAt s length 1 SC eval s s substring 0 s length 1 return s Display menu public 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 loop while 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 Insert Sn 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 Delete if list1 size 0 S List is empty cannot delete else Sn Enter position of element to be deleted 0 list1 size 1 inLine blankStrip kb nextLine pos Integer parseInt inLine list1 delete pos break case f Dutch flag sort list1 dutchFlag break case d Display the list S list1 toString break case s Search the list Sn Enter search character char key kb nextLine charAt 0 int searchResult list1 search key if searchResult 0 S key first found in position searchResult else S key was not found on the list break case m Find the max S Maximum character is list1 max break 401 16 5 case Show menu showInfo case q quit break default S Invalid
View Full Document
Unlocking...