Introduction Part two for this Lab you implement two classes
Introduction
Part two for this Lab, you implement two classes that will serve as the foundation of a
simple key/value database. A programmer could use your database to implement a
note-taking program, where each note has a label and consists of some text. It could
also be used to implement a phone directory or a calendar or a writer\'s planning
program.
We will extend this program a couple of times throughout the semester as we learn
more about Java and object-oriented programming.
Your programming task is defined below as a sequence of small requirements. Write
your program by implementing one requirement at a time in order.
Programming Advice
• First, write a test that expresses what you would like your code to do.
• Then write the code to do it.
• Finally, refactor your code to improve the design.
Take small steps, and your tests will give you feedback as soon as possible. Use
the GenerateTest program to create your test class.
Evaluating the Program
To evaluate the functionality of your submission, we will do two things:
• To evaluate your primary classes: run my tests against your code.
• To evaluate your tests: run your tests against a program that violates the spec in
a variety of simple ways.
As soon as we encounter a requirement that is not satisfied, we will stop grading. So
solving an item lower in the list won\'t be of any use to you if the you haven\'t
completed the tasks that precede it in the list.
Let\'s expand our knowledge of Java with this program.
Comparing Two Objects
In Java, we compare two objects using the equals(Object obj) method. Notice that
the argument to this method is of type Object. As a result, we write a method of this
sort:
public boolean equals(Object obj)
{
if (! obj instanceof CLASSNAME)
return false;
CLASSNAME name = (CLASSNAME) obj;
// ... code to compare receiver to the argument
}
... where CLASSNAME is the name of the class that contains the method. If obj is an
instance of CLASSNAME, we cast it as such so that we can treat it as such. (This method
overrides the equals()defined in class Object.)
Working with Strings
Strings respond to many useful messages that we can use to work with them. In
particular, a String will find substrings of itself and compare itself to another String.
ArrayList, A Dynamically-Sized Collection
One of the shortcomings of using arrays is that we have to know exactly how many
objects we need. But in many programs we don\'t know the number of objects we store
until we execute the program.
In such situations, we can use a ArrayList to hold our objects. Unlike an array,
a ArrayList can hold any number of objects and will grow as necessary. Here are
some standard ways to work with ArrayLists:
• Put an object into a ArrayList using an add() message.
• To access the elements of a ArrayList one at a time, as you would an array,
use get(int index) message. Because ArrayLists can hold any type of object,
you will need to cast the items you retrieve from the ArrayList to the correct
class. For example:
• for (int i = 0; i < arryListOfFrames.size(); i++)
• {
• (Frame) frame = (Frame) myArrayList.get(i);
• // ... process frame
• }
Tasks
Write tests and code for each of the following requirements, in order. The words in
bold indicate message names. Whenever a requirement says the user can \"ask
whether...\", the expected answer is boolean. Whenever a requirement speaks of a
\"particular\" item, then that item will be an argument to the method.
1. The user can create an Association between a key and a value.
The Association responds to a toString() message by returning a string of the
form:
[key]:[value]
2. The user can ask an Association whether its keyContains a particular
substring.
3. The user can ask an Association whether its valueContains a particular
substring.
4. The user can ask an Association whether it contains a particular substring in
either its key or value.
5. The user can ask an Association whether it hasKey that is a particular string.
6. The user can create an AssociationList that contains no Associations.
7. The user can add an existing Association to an AssociationList.
8. The user can add a new Association to an AssociationList by sending a key
and a value.
9. The user can ask an AssociationList whether it contains a
particular Association.
10. The user can ask an AssociationList whether it containsKey for a particular
key.
11. The user can ask an AssociationList to lookup a particular key, returning
a ArrayList of the Associations it contains that have that key.
12. The user can ask an AssociationList to findAll matches for a particular string,
returning a ArrayList containing all Associations that have that string
anywhere in their key or value.
13. The user can ask an AssociationList to remove a particular Association.
14. The user can ask an AssociationList to update every Association having a
particular key by appending a particular string to its value.
You may not add so-called access methods to the Association class. That is, you may
not add \"get\" or \"set\" methods, whose only behavior is to return a field\'s value or
assign a field\'s value, respectively.
I need these files for it:
Part Two
o AssociationTest.java
o Association.java
o AssociationListTest.java
o AssociationList.java
please help me:
Solution
import java.util.*;
public category ArrayListExample i am attending to add String
*elements therefore I created it of string kind */
ArrayList<String> obj = new ArrayList<String>();
/*This is however components ought to be value-added to the array list*/
obj.add(\"Ajeet\");
obj.add(\"Harry\");
obj.add(\"Chaitanya\");
obj.add(\"Steve\");
obj.add(\"Anuj\");
/* Displaying array list components */
System.out.println(\"Currently the array list has following elements:\"+obj);
/*Add part at the given index*/
obj.add(0, \"Rahul\");
obj.add(1, \"Justin\");
/*Remove components from array list like this*/
obj.remove(\"Chaitanya\");
obj.remove(\"Harry\");
System.out.println(\"Current array list is:\"+obj);
/*Remove part from the given index*/
obj.remove(1);
System.out.println(\"Current array list is:\"+obj);
}
}
Output:
Currently the array list has following elements:[Ajeet, Harry, Chaitanya, Steve, Anuj]
Current array list is:[Rahul, Justin, Ajeet, Steve, Anuj]
Current array list is:[Rahul, Ajeet, Steve, Anuj]
Methods of ArrayList category
In the on top of example we\'ve got used ways like add and take away. but there square measure range of ways accessible which may be used directly exploitation object of ArrayList category. Let’s discuss few of the necessary ways.
1) add( Object o): This methodology adds Associate in Nursing object o to the arraylist.
obj.add(\"hello\");
This statement would add a string how-do-you-do within the arraylist ultimately position.
2) add(int index, Object o): It adds the item o to the array list at the given index.
obj.add(2, \"bye\");
It will add the string bye to the 2d index (3rd position because the array list starts with index 0) of array list.
3) remove(Object o): Removes the item o from the ArrayList.


