This week the text introduces two types of collections - ArrayLists and simple arrays. To keep things simple, this week we'll just work with ArrayLists. The text also talks about the 'enhanced for loop', also known as the 'foreach loop', which is designed especially for use with collections please be sure you are using the appropriate loop type in your applications.
We'll practice working with these new structures with a Graphical User Interface (GUI) application that allows the user to enter text that gets placed randomly on the GUI. Then the user can click a set of buttons to have specified items moved around the screen to a new random location.
Don't stress too much about the GUI code, we haven't learned about this yet so I'm giving you *all* of the GUI code that you'll need, along with some structure in some other classes to get you started.
The Text class will keep track of a set of characters (the text) and an x and y location on the screen where the text is located. It provides accessor (getter) methods for each of these instance variables. Additionally, this class provides the functionality to get the length of the text and a way to randomly move the text to a new (x, y) location on the screen.
The TextCollection class will keep track of a collection (an ArrayList) of Text objects. This class allows a new Text object to be added and provides methods to give:
Some of the functionality of these two classes is already provided to you, others will need to be added as you go.
Be sure to read through all items before you begin.
1. Download the Zipped started file from Moodle and extract its contents to a location where you'll be able to find it later.
2. Use Eclipse to open this Java project (if you've forgotten how to do this, refer to the earlier lab that describes this process) and rename the Eclipse Project to YourLastNameLab11.11a
Figure: see image.
3. Once opened, you'll find four (4) different class files. Please be aware that you will not make any changes within the ApplicationDriver.java. Go through the code, remove the existing //TODO comments and replace each one with one or more Java statements that will perform the task. Pay close attention to existing code and comments as they may give you a hint as to how to model your code.
NOTES:
4. Start by working through the first 5 TODO's:
#1 inside the Text class
#2, 3, and 4 inside the TextCollection class
#5 inside the TextController class
5. Once you have code for these items, it should compile and you can then run the application:
6. Return to the TextController class and complete TODO #6
You should now be able to compile and test out the new functionality. Run the application, add a few different texts, click the 'All' button, and all of the text objects should move to a new, random position on the screen.
7. Back in the TextCollection class, complete TODO's #7, 8, 9, and 10
Once this is correct, you should be able to run the application and use the 'Leftmost' button and only the word that's farthest to the left should move to a new location.
8. Complete //TODO #11 (TextCollection) and //TODO #12 (TextController)
Once this is correct, you should be able to run the application and use the 'Shortest' button to move the word with the fewest number of characters.
9. Complete //TODO #13 (TextCollection) and //TODO #14 (TextController)
Once this is correct, you should be able to run the application and use the 'Bottom' button to move the word that is lowest on the screen.
10. At this point, all of the buttons should be functioning correctly. Of course, it's possible that at some point while adding new functionality, you may have accidentally broken something that used to be working. Therefore, if you haven't already tested each of them, you should do it now (this is called 'regression testing' - testing code that has previously passed tests).