Program: WordList (using arrays)
You may NOT use any of the specialized methods from the Array class. For this program I want you to write your own search, sort, and other methods using what we have learned about arrays so far.
You may use the toCharArray method from the String class we discussed in the lecture.
program that allows a user to enter up to 100 words and stores the words the user entered in a list. This means the user can enter less than 100 words if they would like to. The words should be stored in an array. The program will allow the user to manipulate the word list in various ways.
The program should begin by displaying the following menu:
WORD LIST MENU
--------------
1) Add Word
2) Remove Word
3) List Palindromes
4) Resort List Based on Number of Letters
5) Resort List Alphabetical
6) Print the list
7) Quit
Please make a selection:
-If the user selects 1 to add a word to the list: The program should prompt the user to enter a word they would like to add to the list. Assume the user only enters one word. The program should then call a method named addWordAlpha that adds the word the user entered to the word list in alphabetical order. So if the word being added was adam and the word currently in the list was bricks, adam should be added before bricks in the array. So the addWordAlpha method is sorting the word list alphabetically as words are added to it. Do not allow the user to add a word to the list if the array is full (it already contains 100 words).
-If the user selects 2 to remove a word from the list: The program should prompt the user to enter a word they would like to be removed from the list. Assume the user only enters one word. The program should then call a method named removeWord that removes the word the user entered from the list. This method should return true if the word was successfully removed from the list and false if the word was not found in the list and therefore couldnt be removed. Make sure not to leave any elements in your array that dont contain any data after removing the word from the list.
-If the user selects 3 to list palindromes: The program should call a method named listPalindromes that inspects every word in the word list array to see if it is a palindrome. The listPalindromes method should print all words that are palindromes to the screen. (See below for the definition of a palindrome)
-If the user selects 4 to resort the list based on the number of letters: The program should call a method named sortNumLetter that sorts the list based on the number of letters in each word. So if before this method was executed, the order of the list was: apple, fish, zoo; then after this method is executed the list should be: zoo, fish, apple.
-If the user selects 5 to resort the list alphabetically: The program should call a method named sortAlpha that sorts the list in alphabetical order. So if before this method was executed, the order of the list was: zoo, fish, apple; then after this method is executed the list should be: apply, fish, zoo. Note that if the sortNumLetter method wasnt called this method will not change the list.
-If the user selects 6 to print the list: The program should call a method named printList that prints out all of the elements in the word list array to the screen.
-If the user selects 7: End the program
-If the user makes a numerical choice other than 1-7: Print an error message to the screen
You are required to create the following algorithms for your program. Implement these algorithms as methods either in your procedural program or your object oriented program.
addWordAlpha This method should accept as an argument a name to be added to the array. The name should be added to the array in alphabetical order by first name. In other words once the user is finished entering names in the array, they should be already sorted by first name in alphabetical order.
removeWord This method should accept as an argument a name to be removed from the array. Once the item to be removed is found, the method should remove the item and rearrange the rest of the elements in the array accordingly.
isPalindrome This method should accept one string as an argument and return true if it is a palindrome and false if it is not
listPalindromes This method should inspect every element in the list and print each one that is a palindrome (notice, this method will need to call the isPalindrome method).
sortNumLetter This method should resort the list based on the number of letters in each word. The strings should be sorted starting with the ones with the smallest number of letters and ending with the ones with the largest number of letters.
sortAlpha - This method should resort the words in the list in alphabetical order
printList This method should print the list of names as they are currently stored in the array.
You may create as many additional methods as you would like to make your program more efficient and readable.
What is a palindrome? A palindrome is a series of letters that spell the same word or phrase both backwards and forwards. In this program you are only testing single words. So bob and anne are examples of palindromes. Cat, dog, and grapes are examples of words that are not palindromes. See next page for a list of palindromes to test with.
SOME HINTS:
1. Dont forget the various String class methods you have learned such as charAt() and compareToIgnoreCase()
2. You will need to account for the special case when the user is trying to add a word to the beginning of the list.
3. You may need to account for the special case when the word the user is trying to add is alphabetically the last word in the list.
4. Make sure to adjust the list size when you are adding and removing words
5. Make sure not to let the user add a word if the list is already full.
Some Examples of Palindromes to Test your program with
Aibohphobia
Alula
Cammac
Civic
Deified
Deleveled
Detartrated
Devoved
Dewed
Evitative
Hannah
Kayak
Kinnikinnik
Lemel
Level
Madam
Malayalam
[/code][code]
Minim
Murdrum
Peeweep
Racecar
Radar
Redder
Refer
Reifier
Repaper
Reviver
Rotator
Rotavator
Rotor
Sagas
Solos
Sexes
Stats
Tenet
Terret
Testset
[code]public class WordList
{
public static void main(String[]args)
{
//these are your main variables
int wordArr = new int[200];
int currSize = 0; //number of words in the list
//print your menu - get user choice
if (choice == 1)
{
//ask user what word they would like to add save in variable
//I'll call the variable wordToAdd...this variable and the array MUST
//be passed into your method - see below
//also you need to pass the currSize of the list into the method
//Before you call the method remember to ask the user what string to
//add and save in variable wordToAdd - remember the method returns
true or //false
if(addWord(wordArr, wordToAdd, currSize) == true)
{
//if word add is successful and returns true increase currSize
currSize++;
}
else
{
//print message saying list is full and can't add word
}
} //end first if
//add other else ifs here
}
//close main
//addWord method -- method should return true if word is successfully
//added and false if not
public static boolean addWord(String [] arr, String w, int numItems)
{
//right here I would call a method to sort the list
//for now let's just add it in any order
//the key is if the list is not full add the word to the end
//and return true, else don't add word and return false
if(numItems == 200) //array is full return false
{
return false;
}
else
{
//word to the end...the end is at position numItems since
//array index numbering starts at zero
arr[numItems] = w;
return true;

New Topic/Question
Reply




MultiQuote




|