Data Structures using java Your code must be well formatted and commented. While
ID: 672314 • Letter: D
Question
Data Structures using java
Your code must be well formatted and commented. While marks will not be assigned for properly formatting and commenting your code, All programs must include the “Essential Features of Program Documentation” (page 122). Details are included throughout Chapter 2 on best practices for documentation.
3. Chapter 4 describes the ADT Sorted List using an array implementation with a maximum of 20 items. The pseudocode for the ADT Sorted List Operations are provided on page 210. Use this information to create an ADT for handling a collection of license plate numbers, provided as a single String in the form “XXX 9999” where X is any capital letter A-Z and 9 is any digit 0-9. This implementation should allow for duplicate names. Make a test driver program to demonstrate this ADT by:
a) Adding 20 license plates containing at least one duplicate
b) Print the list of the plate numbers (the display method must be in the test driver class and use the sortedGet() method.)
c) Adding a 21st item results in a custom unchecked exception called ADTSortedListException (i.e. list is full).
d) Print the size of the SortedList, remove an item, show the size has changed and print the list again showing the item has been removed.
e) Demonstrate the locate method works by locating the duplicate item.
f) add another copy of the duplicate value (so there are now three) and print the list again.
Explanation / Answer
<?
// open file handle
$file = "p"; // filename and path if not root access
$fh = fopen($file, 'a+');
while($x<200) {
// get random number
$rand_number = rand(100,999);
// get random letters
$letters = '';
for($y=0; $y<3; ++$y) $letters .= chr(rand(65,90));
// mix up combination
$string = (rand(1,2)==1) ? "$rand_number-$letters" : "$letters-$rand_number";
// assume is ok
$ok = 1;
// Set checks to be excluded on new plates.
$checks = array('ASS','666','69','kkk','SHT');
// do the exclusions
foreach ($checks as $check) !preg_match("/$check/", $string) ? : ++$ok;
// if all ok, check is not on the blacklist
if($ok == 1) {
// read blacklist
$blacklist = fread($fh, filesize($file));
// if not on blacklist, add it to file, echo it to output, increment counter
if (!strpos($blacklist, $string)) {
fwrite($fh, $string);
echo "$string<br />";
++$x;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.