Objectives

  • Define a new data type with a Java class
  • Create and manipulate an ArrayList of objects

Overview

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 Problem Statement Text

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 Problem Statement - TextCollection

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:

  • The size of the collection
  • Whether the collection is empty or not
  • Whether a given Text is present in the collection
  • The Text object in the collection that's furthest to the left on the screen
  • The Text object in the collection that contains the fewest number of characters
  • The Text object in the collection that's the lowest on the screen

Some of the functionality of these two classes is already provided to you, others will need to be added as you go.

What to do

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:

  • To make things a little easier, the //TODO comments have been 'numbered'. If you do them in order, then you should be able to compile and test the code as you go (always a good idea).
  • We'll be looking CLOSELY at the variable names you choose this week, so please be sure that they are full words (not abbreviations), are descriptive about what they are storing, and follow the other recommendations in the Java Best Practices Document on Moodle

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:

  • Enter some text into the text box in the toolbar and press Enter
  • When you do this, the text you typed should display somewhere on the screen
  • Enter some other text into the text box and press Enter
  • You should now see both text items displayed in different, random locations on the screen

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).

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.