Quick Index
Objectives
Java generics Association concepts Java packages Writing Test Code
Steps
Take a shot at writing the program after viewing the problem statement When desired, look at the hints When desired, look at the solution
Also see as needed Generics-Just-Variables
Problem Statement
Concept
Association is a wonderful utility helper class that is useful when coding data structures.
Association is a simple key-value object:
Association
key
value
📋
The instance variables (ivar) are "key" and "value".
Coding requirements:
Write a class that uses a generic type parameter "K" and "V" specified on the class that specifies the data types for ivars "key" and "value", respectively Write a constructor that receives values to set all ivars Write a getter for each ivar
Hints
Solution
Here is code with explanations
Test Code
Testing Problem Statement
Write code to test the model. If you get stumped, look at test code solution.
Test Code Solution
First, try to write test code yourself. Then review and compare the following for possible ideas.
Here is support code that is needed for the test code.
Note that this support code is just one example of a minimal testing framework. Feel free to build your own.
Prepare Project
Download the support code from above Build up your project directory using the given package names, e.g. if a Java file has this at the top of the file "package foo.model;" that means the given file should be placed in directory "foo/model" The directories and files should match the listing below
Directory and Files Listing
assoc\model\Association.java
assoc\test\AssociationSmokeTest.java
assoc\test\AssociationTest.java
testutil\AbstractTest.java
testutil\Employee.java
📋
Compile
Here are the console commands to compile all project files (you may also use your favorite IDE):
javac -cp "." assoc/model/*.java
javac -cp "." assoc/test/*.java
javac -cp "." testutil/*.java
📋
Run Tests
Here are the console command to run the tests (you may also use your favorite IDE):
java -cp "." assoc/test/AssociationTest
📋
Association