Where the Buffalo Roam



CIS 120-101 Fall 2007

Programming Assignment 6

Due: Week 10 (Fri 10/26/07)

Problem Description

Design and implement a program with a Java class called “Program6” that will ask the user to input a non-null string and then if string is non-null will produce a “report” (described below about the characters in that line). You should keep asking for Strings until the user hits cancel to quit. (Hint: maybe a forever loop?) First, you should convert this string to lower case (for simplicity) and store each character of the string into a char[] (an array of type char) (“hello world”→|h|e|l|l|o| |w|o|r|l|d|). With this original char array you should report the following: show the string itself, you should show the String with all duplicates removed, (e.g., “hello world” would show “helo wrd” - do this using a method that passes in the original char array and an array of the same size (already allocated). This method will return an int reflecting how many characters are in the no-dups version. In the above case the int returned would be 8 (for h, e, l, o, space, w, r, and d) – both arrays passed in would have been of length 11 to hold the full original String that the user typed in (hint NOT always 11!) The next line of your “report” should show the char-sorted version of your no-dups char array. Call a method passing in your char array AND an int telling it how many chars in that array must be dealt with [Note: array length for that no-dups version will be 11, though only the first 8 should actually be used – we DO need to track last in use!] This next line of the report should be placed in double quotes (Like : “ dehlorw” – note space at start of this String). The next part of the report should tell the first column position [starting at 1!] that each of the 5 vowels appears in the original char array and in the sorted no dups version of the array. Do this using a method that finds the column position of an arbitrary char in a char array (starting at 0) or returns -1 to indicate that the given character does not appear in the given array. Not that this version of the method will be taking 3 arguments: (a) a given character being looked for, (b) the char array being looked into, and (c) and an int that specifies the index of the last character to check in the given array. For example: int position = find(‘a’, origArray, theSize); Where theSize would be set to 10 (based on the input of “hello world”) and another use of this method might be int position = find(‘a’,sortedNoDupArray, secondSize) – where secondSize would have been set to 7 based on what that array actually holds.

See sample run below to see the content and form of the desired output (use a JOptionPane with a scrollable text-area on it to show the full output (see handout on JOptionPanes on how to do this).

Code Requirements:

You must write and use the class methods described below as part of your solution. These methods will for the most part be called from your main (though some of them may call each other). You will be supplied with a main-method to be used for testing!

Error Checking: none.

Description of Methods:

|Method Signature and Return Type (required) |Purpose |

|void putStringIntoArray (String s, char[] cArray) |Passed the user input string and an array (preallocated to a size that matches the length of |

| |the user input string). This method stores each char of the string into the corresponding |

| |position in the array. |

|int removeDuplicateCharacters (char[] withDups, |Passed an array that may have duplicate characters in it this method will load in order, one |

|char[] withoutDups) |copy of each char in the first array into the second array. Note, both arrays will have the |

| |SAME size on arrival. This method will return the position of the last index actually USED in |

| |the second array. |

| |For each char in the first char array, you check to see if that char is present already in the |

| |second array, and if not add it to “the current end” of that second array. Use the method |

| |isCharacterPresent to help you do this. If that method reports a false for a given char in the|

| |first array (when looking for it in the second array), then that char will be stored into the |

| |second array. Example: if this method is passed an two arrays of length 5, with the first |

| |holding | a | b | a | c | b | then this method will set the second formal parameter to hold | |

| |a | b | c | and the int returned would be a 2 since the ‘c’ is in position 2 of the second |

| |array (i.e. after return caller should not use past two in the second array!) |

|boolean isCharacterPresent (char c, char[] cArray, |Passed a char to search for, an array containing the non-repeated chars stored to this point, |

|int lastInUse) |and an integer indicating the index of the last character stored in that array. Determines if |

| |the char is stored in the array. Returns true if yes, false if not. |

|int find(char c, char[] cArray, int lastInUse) |Passed a char to search for in a given char-array (up to the last char position actually in |

| |use) this method returns a -1 if the given char is not found, otherwise it returns the position|

| |(starting at 0) of the first use of the given char in the given array. Example: if char is |

| |‘a’, cArray is | b | a | a | ? | ? | and last in use is 2, this method would return a 1 since |

| |that is position of first a. |

|void sortArray (char[] cArray, int lastInUse) |Passed an array containing the non-repeated chars and an integer indicating the index of the |

| |last character stored in that array. Sorts the array in ascending alphabetical order using one|

| |of the sorting algorithms from chapter 6. |

|String putArrayIntoString (char[] cArray, |This method takes an array of char (and the index of the last char in actual use) and returns a|

|int lastInUse) |String holding the chars from the array. |

|String putArrayIntoString (char[] cArray) |Like the above method except that this version assumes that ALL characters from the array are |

| |to be added to the String that is to be returned. Note: this is an example of method |

| |overloading. |

|void showOutputInJOptionPane(String output) |This method simply shows the given output String in a JOptionPane that has a scrollable |

| |text-area on it (see handout). The output string itself is assumed to have been formed with |

| |new-line characters (\n) wherever needed. |

Hints:

Use charAt() to access the individual chars in the user input string. When processing the arrays that might not be full (containing non-repeated chars), use the integer passed in to determine the number of times to loop. For the full arrays (containing all chars), use array.length.

Test Design (You must run the program 3 times to test your 3 cases)

|Input String |Original string |No repeated chars |Sorted no repeated chars output |

| |output |output | |

|1 | | | |

|2 | | | |

|3. | | | |

You must submit a printed and electronic (CD or Floppy) copy of your commented program along with a printed copy of your completed test design table and complete screen captures of the input and output for all test runs in a manila envelope.

Sample Output

First input:

[pic]

Output for the above:

[pic]

Second input:

[pic]

Second output:

[pic]

CIS 120-101 Fall 2007

Programming Assignment 6 Cover Sheet Grade ____________

 

Name (Print): __________________________________________ Student #: _________

Spring 2007 Instructor: _________________________________ Section: ___________

Your program documentation should be labeled and organized according to the following:

TEST DESIGN: 10% Paper copy and electronic copy are mandatory.

_____10 pt. Test Design and corresponding program output

Minimum of three documented test cases with screen captures of the associated input/output for each case. Your test design cases must exercise all paths through your program. Displays correct input and output expected by program.

PROGRAM: 90% Paper and electronic copy of your source code submitted in a manila envelope.

_____10 pt Proper global documentation (at the top). See lab 1 programs for an example.

_____10 pt. Internal documentation and comments. See lab 1 programs for an example.

_____10 pt. Style (indentation, spacing, blank lines, correct capitalization of identifiers based on coding convention, line breaks and line continuations used appropriately, output is clear and correctly formatted, etc.)

_____10 pt. Meaningful identifiers - matches data dictionary

_____50 pt. Correctness of program. Including: Program compiles, program runs, produces correct output

Please note: additional points may be deducted if the package submitted does not contain all of the required contents in an acceptable format.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download