- In this lab students will practice writing and using constructors
- After this lab students should be able to produce enumerated types.
- This lab introduces the practice of writing unit tests for code being developed.
This lab introduces constructors and enumerated types.
You are expected to write unit tests for the getColor
method in this lab. Be sure to test that it returns the same color that was set in the constructor.
Remember to use Test Driven Development: create stubs of your methods and write your tests first, confirm that they run fail the tests, then implement your methods so that they pass the tests.
For this lab, create a java file called InitializationLab1.Java
- In
initialization.lab1
package create two classes,BoringThing
andColorfulThing
- Create a constructor for
ColorfulThing
that takes one String as a parameter and stores it as an instance variable. - Provide a
getColor
method forColorfulThing
- Create the
InitializationLab1
class and add amain
method. - In
main
declare and instantiate fiveBoringThing
s and fiveColorfulThing
s (pick any colors you like). - Print out each
ColorfulThing
's color. - Try creating another
ColorfulThing
using a default (no-argument) constructor. Notice that this causes an error.
Letting a user specify any string and blindly assuming it's a color is a bit naïve. Let's specify what colors of ColorfulThing
s are available.
- Add an enumerated type called
Color
toColorfulThing
and give it at least three options - Refactor the
ColorfulThing
constructor and class to use the new enumerated type. Don't forget to refactor the getter you created earlier. - Adjust your
main
method to use the new enumerated type as well. - Run your program to make sure the new enumerated type works.
- In this lab students will practice overloading constructors and initializing objects based on constructor arguments.
- After this lab students should be comfortable manipulating instance variables in constructors and other object methods.
This lab focuses on initializing objects using constructors and overloading constructors. It also introduces the use of arrays as arguments and instance variables.
You are expected to write unit tests for your code in this lab. Be sure to test the behavior of ThingContainer
in multiple scenarios, including testing the add
method when the `ThingContainer is or is not full. You are responsible for determining which other methods need to be tested and how to test them. Remember to practice Test Driven Development as you work on this lab.
For this lab, use the java file called InitializationLab2.Java
. Import your ColorfulThing
definition from the last lab.
Create a new class called ThingContainer
; this will be a class designed to hold multiple ColorfulThing
s.
Give the ThingContainer
class an array of ColorfulThing
s and a constructor that takes one argument: an integer that defines the size of the array.
ThingContainer
needs an add
method to add ColorfulThing
s to its array. It should add the ColorfulThing
s in the order they are received. If the array is full when add
is called then it should print the error message "ThingContainer is full"
ThingContainer
should also be able to print all of the ColorfulThing
s it has. Add a printThings method for this.
Create a main
method for InitializationLab2. In this method create at least three ThingContainers
and test that you can fill them with randomly generated ColorfulThing
s and that you get the error message described above.
Let's enhance our ThingContainer
. Start by adding a method to remove items. We'll call our method pop
, and it should remove the last element in the array of things and return that element.
What if we don't want the last element in the array? Let's add a remove
method, and overload it to handle two different cases. Here they are:
- We call the
remove
method with aColor
value from the enumerated type inColorfulThing
. Remove the first element of that color from the array and return it. Returnnull
if theThingContainer
does not contain aColorfulThing
of that color. - We call the
remove
method with aColorfulThing
object. Remove theColorfulThing
that matches that object and return it; if it is not in the array returnnull
.
Be sure to adjust the elements in the array after each removal so that it stays in order (the next item added should always be added to the end).
Demonstrate the use of all of these new methods in your main
method.
Add another constructor to ThingContainer
that can take an array of ColorfulThings
as its argument. This constructor should initialize its array to match the contents of the array argument.
Modify your main
method to test that this new constructor works.