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

Program in JAVA. Build off code provided. Database with Sorted Arrays as Indexes

ID: 3600054 • Letter: P

Question

Program in JAVA. Build off code provided.

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.

Explanation / Answer

package com;
public class DList<T> {
private DNode<T> header, trailer;
private int size;
public DList() {
size = 0;
header = new DNode<T>(null, null, null);
trailer = new DNode<T>(null, header, null);
header.setNext(trailer);
}
// utility methods
public int size() {
return size;
}
public boolean isEmpty() {
return size == 0;
}
// give clients access to nodes, but not to the header or trailer
public DNode<T> getFirst() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return header.getNext();
}
public DNode<T> getLast() throws Exception {
if (isEmpty())
throw new Exception("Empty");
return trailer.getPrev();
}
public DNode<T> getNext(DNode<T> v) throws Exception {
DNode<T> ans = v.getNext();
if (ans == null || ans == trailer)
throw new Exception("No such node");
return ans;
}
public DNode<T> getPrev(DNode<T> v) throws Exception {
DNode<T> ans = v.getPrev();
if (ans == null || ans == header)
throw new Exception("No such node");
return ans;
}
// methods to change the list
public void addBefore(T d, DNode<T> v) {
DNode<T> u = v.getPrev();
DNode<T> x = new DNode<T>(d, u, v);
u.setNext(x);
v.setPrev(x);
size++;
}
public void addAfter(T d, DNode<T> v) {
DNode<T> w = v.getNext();
DNode<T> x = new DNode<T>(d, v, w);
v.setNext(x);
w.setPrev(x);
size++;
}
public void addFirst(T d) {
addAfter(d, header);
}
public void addLast(T d) {
addBefore(d, trailer);
}
public T remove(DNode<T> v) throws Exception {
if (v == header || v == trailer)
throw new Exception("Sentinel");
DNode<T> u = v.getPrev();
DNode<T> w = v.getNext();
w.setPrev(u);
u.setNext(w);
size--;
return v.getData();
}
// LinkedList testing methods:
public String toString() {
String ans = "";
DNode<T> n = header;
ans += "(H)<-->";
do {
n = n.getNext();
if (n == trailer)
ans += "(T)";
else
ans += (n.getData() + "<-->");
} while (n != trailer);
return ans;
}
}

Dr Jack
Hire Me For All Your Tutoring Needs
Quick quotes • Clear explanations • Study support
Chat Now And Get Quote