Question

Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String tThe Database should have the following methods: 1. void add takes an element add adds it to the database by creating a node f6. void makeIndex (String trait): If the database is empty, this method does nothing. Otherwise, since the database contains

java

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Files

filename: DatabaseType.java

import java.util.*;

/** interface used for dictating types that are databases */
public interface DatabaseType<T> {

/** returns comparater based on trait
* @param trait String used to find which comparater
* @return comparater based on string */
public Comparator<T> getComparatorByTrait(String trait);
}

import java.util.*; /** interface used for dictating types that are databases */ public interface DatabaseType<T> { ovou AWN

filename: Database.java

import java.util.*;
import java.util.Collections;

/** class for the database itself */
public class Database<T extends DatabaseType<T>>{

/** used to signify start of node list */
private LinkedList<T> list = new LinkedList<T>();
/** used to reference based on String to find Comparator */
private Hashtable<String,ArrayList<T>> indexHash = new Hashtable<String,ArrayList<T>>();

/** adds an element to the Database linkedlist
* @param element new DatabaseType to be added */
public void add(T element) {
list.add(element);
indexHash.clear();
}
  
/** removes any elements in the database
* @param element delete this any number of times from the database */
public void remove(T element) {
/** boolean used to track for if a node was deleted */
boolean changeCheck = false;
/** list for gathering element to be deleted */
LinkedList<T> tempList = new LinkedList<T>();
/** iterator for linked list */
ListIterator<T> iter = list.listIterator();

/** iterates comparing elements and not adding ones that match */
while(iter.hasNext()) {
/** generic for comparing */
T tempElement = iter.next();
if(tempElement.toString().equals(element.toString())) {
changeCheck = true;
}
else {
tempList.add(tempElement);
}
}
  
if(changeCheck) {
indexHash.clear();
list = tempList;
}
}
  
/** makes a list of elements in database matching the filter rules
* @param element used as baseline to compare against
* @param filter runs comparing between element and list elements
* @return LinkedList with all fitting elements */
public LinkedList<T> lookupInList(T element, Comparator<T> filter) {
/** iterator for linked list */
ListIterator<T> iter = list.listIterator();
/** list with filtered elements */
LinkedList<T> returnList = new LinkedList<T>();
  
/** paces through linked list adding elements that fit the filter */
while(iter.hasNext()) {
/** generic for comparing */
T tempElement = iter.next();
if(filter.compare(element,tempElement) == 0) {
returnList.add(tempElement);
}
}
  
return returnList;
}
  
/** looks up certain element in an index for making it sorted a certain way
* @param element looking up based on this element
* @param index access String for HashTable
* @param comparator what to base search on
* @return LinkedList with fitting elements in sorted order from hashtable */
public LinkedList<T> lookupInList(T element, String index, Comparator<T> comparator) {
/** list with filtered elements from array list*/
LinkedList<T> returnList = new LinkedList<T>();
/** ArrayList from Hashtable */
ArrayList<T> aList = new ArrayList<T>();
  
return returnList;
}
  
/** goes through a array list and picks out mathing elements base on string
* @param element base for matching elements
* @param index String to refernce ArrayList in hashtable
* @param comparator used to filter
* @return LinkedList with matching elements */
public LinkedList<T> lookupInIndex(T element, String index, Comparator<T> comparator) {
/** ArrayList from Hashtable */
ArrayList<T> aList = new ArrayList<T>();
/** used in finding where other matches are based on order */
int intialMatch = -1;
/** used to input start into arrayListPart for multiple elements meeting conditions */
int firstMatch = 0;
/** used to input end into arrayListPart for multiple elements meeting conditions */
int lastMatch = 0;
  
makeIndex(index);
aList = indexHash.get(index);
  
//returns empty list if empty datbase
if(aList.size() == 0)
return new LinkedList<T>();
  
intialMatch = binarySearch(element, aList, comparator);
firstMatch = intialMatch;
lastMatch = intialMatch;
  
//returns empty list if no match was found
if(intialMatch == -1)
return new LinkedList<T>();
  
/** loop looking for matches before intial */
while(comparator.compare(element,aList.get(firstMatch - 1)) == 0 && firstMatch > 0)
firstMatch -= 1;
/** loop looking for matches after intial */
while(comparator.compare(element,aList.get(lastMatch + 1)) == 0 && lastMatch < aList.size())
lastMatch += 1;
  
return arrayListPart(aList,firstMatch,lastMatch);
}
  
/** used as helper fuction to find a spot where a match occurs in array list with binary
* @param element that needs to match according to compartor
* @param aList where data is being used from to compare
* @param dictates which elements match
* @return int for location of a match*/
private int binarySearch(T element, ArrayList<T> aList,Comparator<T> comparator) {
/** beginning range lower bound for bainary search */
int front = 0;
/** beginning range upper bounf for bainary search */
int end = aList.size() - 1;;
  
/** finding intially where matching elements are */
while(front <= end) {
/** finding center for binary */
int middle = (end + front)/2;
/** compare value between middle and given element */
int compareValue = comparator.compare(element,aList.get(middle));
if(compareValue == 0) {
return middle;
}
else if(compareValue > 0)
front = middle + 1;
else
end = middle - 1;
}
  
return -1;
}
  
/** used to construct linked list of matches for lookupInIndex
* @param aList where elements will be pulled from
* @param start first matching element location
* @param end last matching element location
* @return Linked list with selected elements */
private LinkedList<T> arrayListPart(ArrayList<T> aList, int start, int end) {
/** list with filtered elements from array list*/
LinkedList<T> returnList = new LinkedList<T>();
  
for(int i = start; i <= end; i++) {
returnList.add(aList.get(i));
}
return returnList;
}
  
  
/** creates an index in the hash based on a trait
* @param trait String to uses a filter from element getComparatorByTrait(trait) */
public void makeIndex(String trait) {
/** ArrayList to be added to HashTable */
ArrayList<T> aList = new ArrayList<T>();
/** iterator for linked list */
ListIterator<T> iter = list.listIterator();
  
/** goes through copying addresses to ArrayList */
while(iter.hasNext()) {
aList.add(iter.next());
}
if(!list.isEmpty()) {
Comparator<T> comparator = aList.get(0).getComparatorByTrait(trait);
Collections.sort(aList,comparator);
}
indexHash.put(trait,aList);
  
}
  
/** goes into hashtable finds trait and the finds matching traits and returns
* @param trait what search is based on
* @param element searh "keyword"
* @return LinkedList with sorted matching elements */
public LinkedList<T> lookup(String trait, T element) {
/** list with filtered elements from array list*/
LinkedList<T> returnList = new LinkedList<T>();
/** ArrayList from Hashtable */
ArrayList<T> aList = new ArrayList<T>();
  
try{
return lookupInIndex(element, trait, element.getComparatorByTrait(trait));
}
catch (Exception e) {
return lookupInList(element,element.getComparatorByTrait(trait));
}
}
  
/** reads off HashTable if trait has already been established
* @param trait which ArrayList to get
* @return ArrayList from hashTable */
public ArrayList<T> getList(String trait) {
if(indexHash.get(trait) == null) {
makeIndex(trait);
return indexHash.get(trait);
}
else {
return indexHash.get(trait);
}
}
  
/** method made for early testing purposes
* @return the header for the database */
public LinkedList<T> getData() {
return list;
}
}

import java.util.*; import java.util.collections; /** class for the database itself */ public class Databasect extends Databa

/** paces through linked list adding elements that fit the filter */ while(iter.hasNext()) { /** generic for comparing */ I t

return new LinkedList<T>(); 111 112 113 114 115 116 /** loop looking for matches before intial */ while(comparator.compare(el

/** creates an index in the hash based on a trait * @param trait String to uses a filter from element getComparatorByTrait(tr

public LinkedList<T> getData() { return list;

Hope this helps!

Please let me know if any changes needed.

Thank you!

I hope you're safe during the pandemic.

Add a comment
Know the answer?
Add Answer to:
java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • **********Using Java*************** Create 5 CLASSES: Driver(main), Word, AnagramFamily, and 2 Comparators classes words.txt is a very...

    **********Using Java*************** Create 5 CLASSES: Driver(main), Word, AnagramFamily, and 2 Comparators classes words.txt is a very large file containing thousands of words. I will post some words down below. some of word.txt aa aah aahed aahing aahs aal aalii aaliis aals aardvark aardvarks aardwolf aardwolves aargh aarrgh aarrghh aas aasvogel aasvogels ab aba abaca abacas abaci aback abacterial abacus abacuses abaft abaka abakas abalone abalones abamp abampere abamperes abamps abandon abandoned abandoner abandoners abandoning abandonment abandonments abandons abapical abas abase...

  • ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes,...

    ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes, where the Media superclass has the artistName and the mediaName as the common attributes. Create a subclass called CDMedia that has the additional arrayList of String objects containing the songs per album. Create another subclass called DVDMedia that has the additional year the movie came out, and an arrayList of co-stars for the movie. 1.) The superclass Media will implement Comparable, and will define...

  • Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to...

    Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • In Java You may add any classes or methods to the following as you see fit in order to complete t...

    In Java You may add any classes or methods to the following as you see fit in order to complete the given tasks. Modify the LinkedList (or DoubleLinkedList) class and add a method append. append should take another LinkedList (DoubleLinkedList) as input and append that list to the end of this list. The append method should work by doing a few "arrow" adjustments on the boxes and it should not loop through the input list to add elements one at...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String),...

    Design a JAVA class called Course. The class should contain: ○ The data fields courseName (String), numberOfStudents (int) and courseLecturer (String). ○ A constructor that constructs a Course object with the specified courseName, numberOfStudents and courseLecturer. ○ The relevant get and set methods for the data fields. ○ A toString() method that formats that returns a string that represents a course object in the following format: (courseName, courseLecturer, numberOfStudents) ● Create a new ArrayList called courses1, add 5 courses to...

  • JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {    ...

    JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet {     /**      * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList      * containing all the indexes of Strings in arr that match String s. For this method, two Strings,      * p and q, match when p.equals(q) returns true or if both of the compared references are null.      *      * @param s The string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT