Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple database with three fields : id, lastName, firstName all as Stri

ID: 3596425 • Letter: C

Question


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

Driver class with a Main!
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();
}

Explanation / Answer

Solution to given problem is provided below. Driver program execution output is also provided for reference.

File: DatabaseRecord.java

File: IndexRecord.java

File: IndexIterator.java

File: Database.java

File: Driver.java

Driver Program Execution Output:

by id:
1234 alice jones
1235 zelda smith
2233 sue charles
2244 ed smiley
3234 mac edwards
4234 roger morris
6633 ellen nance
6655 mary rogers
9988 dave bing
9999 mike adams
*************
by first name:
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
*************
by last name:
9999 mike adams
9988 dave bing
2233 sue charles
3234 mac edwards
1234 alice jones
4234 roger morris
6633 ellen nance
6655 mary rogers
2244 ed smiley
1235 zelda smith
*************
by id reverse:
9999 mike adams
9988 dave bing
6655 mary rogers
6633 ellen nance
4234 roger morris
3234 mac edwards
2244 ed smiley
2233 sue charles
1235 zelda smith
1234 alice jones
*************
by first name reverse:
1235 zelda smith
2233 sue charles
4234 roger morris
9999 mike adams
6655 mary rogers
3234 mac edwards
6633 ellen nance
2244 ed smiley
9988 dave bing
1234 alice jones
*************
by last name reverse:
1235 zelda smith
2244 ed smiley
6655 mary rogers
6633 ellen nance
4234 roger morris
1234 alice jones
3234 mac edwards
2233 sue charles
9988 dave bing
9999 mike adams

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote