This project will help you practice the skills and concepts you learned related to collections in Java. For this project you'll build an app with a text field for the user to enter a word. When they tap the button, the app will look for synonyms for the entered word, and will display them in a text view if any are found.
A synonym is a word with the same meaning as another word. For example,
difficult
andhard
are synonyms.
- Create a new Android project
- Name the project "Thesaurus"
- Make sure you select Kotlin as the development language
- Open the app's activity_main.xml file.
- Add a edit text field to the main screen
- Add a button to the main screen
- Make the button's text "Show Synonyms"
- Add a text view to the screen.
- Add ids to all the elements
- Create an onClickListener for the button in the MainActivity.kt file.
- In MainActivity.kt, create a variable that is a 2 dimensional String array where each element is an array of strings that are synonyms of the key word.
- If you need help getting started, you can copy and paste the list found here. Feel free to add any additional words that you'd like.
- In this file, create a function called
synonyms
. It should take a single string as an argument, and return a array ofString
objects.
The return type should be
Array<String>?
so you can return a null array if there isn't a list of synonyms
- Write the
synonyms
function. It should return an array of strings that are synonyms for the passed in string, or null if there are no synonyms. - Use a for loop to step through the top level array and look for the string in the first element of each element array.
- In the listener for the "Show Synonyms" button, get the string entered in the text field, look up synonyms for it using your
synonyms
function, and if any synonyms are found, display them in the text view. - Be sure to combine the strings in the array into a single string to be displayed in the text view.
- Build and run your app using your preferred testing environment.
- Enter various words, including those you know are in your synonyms directory, and others that aren't into the text field and make sure the synonym listing works.
If you finish and want another challenge try making it so that if a any word in the element array is equivalent to the value entered, that all its sibling synonyms are found and returned.
For example, if your synonyms array were:
val synonyms = arrayOf(arrayOf("learn", "determine", "master", "study", "get", "grasp")))
and the user entered "study"
, the app should output "determine", "master", "get", "grasp", and "learn".