Question
Program in JAVA. some example code is given and needs to be assed to. Database with Sorted Arrays as Indexes Practice (not as long as
Program in JAVA. some example code is given and needs to be assed to.
Database with Sorted Arrays as Indexes Practice (not as long as it looks)
What to do: Create a simple database with three fields : id, lastName, firstName all as Strings. You should define a class called DatabaseRecord with three private String varibles (the three fields), along with the appropriate getters and setters.
Here's what mine looked like(teacher example):
public class DatabaseRecord {
private String id; private String first; private String last;
public DatabaseRecord(String id, String first, String last) {
this.id = id; this.first = first;
this.last = last;
}
public String toString() { return id + " " + first + " " + last; } }
You should also declare an object called DatabaseArray which is an array of DatabaseRecords. Records of type DatabaseRecord should be added at the end of the DatabaseArray. Create an IndexRecord class:
public class IndexRecord {
private String key;
private int where;
//other stuff here
}
Now create an IndexIterator. This is an array of IndexRecord and is to be implemented as an OrderedArray class. That is, insertions must maintain the order in the array, where order is maintained by the key value. Hint: String class in Java comes with builtin compareTo method that you might find useful here.
Iterators
Your IndexIterator must implement an iterator. An iterator is simply a variable that maintains a current reference into the data structure. In this instance, since IndexIterator is a static array, the iterator is just an integer; a pointer into the array. You should implement the following methods:
void iteratorInitFront - set the iterator to zero
void iteratorInitBack - set the iterator to the last element in the array
boolean hasNext - returns true if iterator<= current last index in the array, false otherwise.
boolean hasPrevious - returns true if iterator>0 , false otherwise
int getNext - returns the where component of the IndexRecord referenced by iterator and then increments the iterator
int getPrevious - returns the where component of the IndexRecord referenced by iterator and then decrements the iterator
Finally, create a class called Database. Here's what mine looked like(teacher example):
public class Database {
private DatabaseArray myDbArr; private IndexIterator idIt, firstIt, lastIt;
public Database(int size) { myDbArr = new DatabaseArray(size); idIt = new IndexIterator(size); firstIt = new IndexIterator(size); lastIt = new IndexIterator(size); }
/// other stuff goes here
}
To get an idea of how the iterators work, here's the code I have for printing out the database in ascending order by first name:
public void listByFirst() { firstIt.iteratorInitFront();
while (firstIt.hasNext()) {
System.out.println(myDbArr.retrieve(firstIt.getNext())); }
System.out.println(myDbArr.retrieve(firstIt.getCurrent())); }
Here's the output from the following for my sample data set:
1234 alice jones
9988 dave bing
2244 ed smiley
6633 ellen nance
3234 mac edwards
6655 mary rogers 9999 mike adams
4234 roger morris
2233 sue charles
1235 zelda smith
Here is your Driver program:
public class Driver { public static void main(String[] args) {
Database d = new Database(10); d.insert("1234", "alice", "jones"); d.insert("1235", "zelda", "smith"); d.insert("9999", "mike", "adams"); d.insert("9988", "dave", "bing"); d.insert("2233", "sue", "charles"); d.insert("2244", "ed", "smiley"); d.insert("6655", "mary", "rogers"); d.insert("6633", "ellen", "nance"); d.insert("4234", "roger", "morris"); d.insert("3234", "mac", "edwards");
System.out.println("by id:"); d.listById(); System.out.println("****"); System.out.println("by first:"); d.listByFirst(); System.out.println("*************"); System.out.println("by last:"); d.listByLast(); }
Grading (Practice) Correct implementation of the below classes will earn you points: DatabaseArray (25 pts) OrderedArray (25 pts) IndexIterator (25 pts) Database (25 pts)
What will lose points: You should follow directions here closely. You will lose 50% of your grade for each of the following - your IndexIterator is not implemented as an OrderedArray. - if you maintain order of your IndexIterator by sorting the array - if you implement the DatabaseArray as three arrays of String - if you implement the IndexIterator as two arrays: one of type String and the other as type int. - if you do not use the driver program exactly as it is given If your code does not run the assignment automatically receives 0.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started