Question

Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt.

public interface List {
   public int size();
   public boolean isEmpty();

   public Object get(int i) throws OutOfRangeException;

   public void set(int i, Object e) throws OutOfRangeException;

   public void add(int i, Object e) throws OutOfRangeException;

public Object remove(int i) throws OutOfRangeException;

   }


public class ArrayList implements List {
   private int CAP;
private int size;
private Object[] item;
public ArrayList(int capacity){
this.size = 0;
this.CAP = capacity;
this.item = new Object [capacity];
}
public ArrayList () {
this(100);
}
public int size(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public Object get(int i) throws OutOfRangeException {
if (i<0 || i>=size)
throw new OutOfRangeException ("Out of Range");
return item [i];
}
public void set(int i, Object e) throws OutOfRangeException {
if (i<0 || i>=size)
throw new OutOfRangeException ("Out of Range");
item [i] = e;
}
public void add(int i, Object e) throws OutOfRangeException {
if (i<0 || i>size)
throw new OutOfRangeException ("Out of Range");
for (int j = size - 1; j>=i; j--)
item[j+1] = item [j];
size++;
}
public Object remove(int i) throws OutOfRangeException {
if (i<0 || i>=size)
throw new OutOfRangeException ("Out of Range");
Object answer = item[i];
for (int j=i; j<size-1; j++)
item[j] = item[j+1];
item[size-1] = null;
size--;
return answer;
}
}

public class OutOfRangeException extends Exception {
   public OutOfRangeException(String str){
       System.out.println("OutOfRangeException:" + str);}
       public OutOfRangeException (){
       this("Out Of Range");}
}

public abstract class AbstractMap {
   public abstract Object get(Object key);
   public abstract Object put(Object key, Object value);
   public abstract Object remove(Object key);
   public abstract boolean isEmpty();
   public abstract int size();
   public abstract Iterable keySet();
   }

import java.util.Iterator;
public class SArrayIterator implements Iterator {
   private ArrayList data;
   private int i = 0;
   private boolean removable = false;

   public SArrayIterator(ArrayList a) {
   data = a;
   }

   public boolean hasNext() {return i < data.size();}

   public Object next() {
   Object answer = null;
   if (i == data.size())
   return null;
   try {
   answer = data.get(i++);
   } catch (OutOfRangeException e) {
   System.out.println("Out Of Range");
   }
   removable = true;
   return answer;
   }

   public void remove() {
   if (!removable)
   throw new IllegalStateException("nothing to remove");
   try {
   data.remove(i-1);
   } catch (OutOfRangeException e) {
   System.out.println("Out of Range");
   }
   i--;
   removable = false;
   }
}


public class MapEntry {
private String key;
private String value;
public MapEntry() {this(null, null);}
public MapEntry(String key, String value) {
   key = key;
value = value;
}
public String getKey() {return key;}
public String getValue() {return value;}
public void setKey(String k) {key = k;}
public Object setValue(String value) {
   String old = this.getValue();
   this.value = value;
   return old;
}

public int hashCode () {
   if (key == null) return 0;
   int h = 0;
   for (int i = 0; i<key.length(); i++){
       h += (int)key.charAt(i);
       h = (h<<5) | (h>>>27);
   }
   return h;
}
}


import java.util.Random;
public abstract class AbstractHashMap extends AbstractMap {
protected int n = 0;
protected int capacity;
private int prime;
private long scale, offset;
public AbstractHashMap(int c, int p) {
   this.capacity = c;
   Random rand = new Random();
   this.scale = rand.nextInt(prime-1)+ 1;
   this.offset = rand.nextInt(prime);
   createTable();
}
public AbstractHashMap() { this(61,999331);}
public int size() {return n;}
public boolean isEmpty() {return n==0;}
private int hashValue(Object key) {
   return (int)((Math.abs(key.hashCode()*scale + offset) % prime) % capacity);}

public Object get(Object key) {
   int h = hashValue(key);
   return bucketGet(h, key);
  
}
public Object put (Object key, Object value) {
   int h = hashValue(key);
   return bucketPut(h, key);
}
public Object remove (Object key) {
   int h = hashValue(key);
   return bucketRemove(h,key);
}
protected abstract void createTable();
protected abstract Object bucketGet(int h, Object k);
protected abstract Object bucketPut(Object k, Object v);
protected abstract Object bucketRemove(int h, Object k);
}
import java.util.Iterator;
public class ProbeHashTable extends AbstractHashMap {  
   private MapEntry[] table;
   private MapEntry DEFUNCT = new MapEntry(null, null);

   private int ncollisions = 0;
   public ProbeHashTable() {super();}
   public ProbeHashTable(int cap,int p) {super(cap, p); }
   private boolean compareStr(String s, String t) {
       boolean result = s.equals(t);
       if (!result)
   ncollisions++;
   return result;
   }
   protected void createTable() {
   table = (MapEntry[]) new MapEntry[capacity]; }
     
   private boolean isAvailable(int i) {
   return (table[i] == null || table[i] == DEFUNCT);
   }
   private int findSlot(int h, String k) {
   int avail = -1;
   int i = h;
   do {
   if(isAvailable(i)) {
   if(avail == -1) avail = i;
   if(table[i] == null) break;
   }
   else if(compareStr(table[i].getKey(), k)) { return i;}
   i = (i + 1) % capacity;
   }
   while(i != h);
   return -(avail + 1);
   }
   protected Object bucketPut(int h, Object key, Object value) {
   int i = findSlot(h, (String) key);
   if(i >= 0)
   return table[i].setValue((String) value);
   table[-(i + 1)] = new MapEntry((String) key, (String) value);
   n++;
   return null;
   }
   protected Object bucketGet(int h, Object key) {
   int i = findSlot(h, (String) key);
   if(i < 0)
   return null;
   return table[i].getValue();
   }
   protected Object bucketRemove(int h, Object key) {
   int i = findSlot(h, (String) key);
   if(i < 0)
   return null;
   Object answer = table[i].getValue();
   table[i] = DEFUNCT;
   n--;
   return answer;
   }
   private class KeyIterable implements Iterable{

public Iterator iterator() {
   ArrayList buffer = new ArrayList(n);
   for(int i = 0; i < capacity; i++)
   try {
   if(!isAvailable(i))
   buffer.add(buffer.size(), table[i].getKey());
   }
   catch(OutOfRangeException e) {
   System.out.println("keySet: Out Of Range");
   }
   return new SArrayIterator(buffer);
   }
   }
   public Iterable keySet() {
   return new KeyIterable();
   }
   public int getCollisions(){
   return ncollisions;
   }
   public String toString() {
   String str = "";
   for (int i = 0; i < table.length; i++) {
   str += i + " " + table[i] + " " + table[i].getValue() + "\n";
   }
   return str;
   }

   }

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

Hi,

The problem has been resolved. A file named "Test.java" has been created with Scanner to read a file "roaster.txt". You haven't mentioned how you need to use the Test class and what is your task to perform so I have created a main test file to use for every purpose.

You can read a file line by line and put the data into ArrayList and other classes according to you. The test file has been tested on a random data that roster.txt file have.

Please refer to the screenshot that having the data of file and the code output. The line by line file data has been printed in output.

Screenshot:

Code:

---------------

import java.io.*;
import java.util.*;
public class Test
{
public static void main(String[] args) throws Exception
{

   Scanner sc = new Scanner(new File("roster.txt"));

   while (sc.hasNextLine())
       System.out.println(sc.nextLine());
   }
}

--------------

The problem has been resolved, I hope you will like the solution. If you have any question or query regarding this problem or other, please comment below and give the positive rating.

Thank You!! Happy Learning!! !!

Add a comment
Know the answer?
Add Answer to:
Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...
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
  • Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: ...

    Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete ALPQueue class (an ArrayList based priority queue) that extends the AbstractPQueue discussed in the class, along with PQueue.java, ArrayList.java, List.java, PQEntry.java , EntryComparitor.java, and OutOfRangeException.java given in the class (any other implementation of above classes that are not compatible with List interface and AbstractPQueue class will not receive any credit). 2/ Task2: implement a concrete HeapPQueue class (a Heap based priority queue) that extends...

  • Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete...

    Comparing the insertion and removing performance between ALPQueue with HeapPQueue. Requirements: 1/ Task1: implement a concrete ALPQueue class (an ArrayList based priority queue) that extends the AbstractPQueue discussed in the class, along with PQueue.java, ArrayList.java, List.java, PQEntry.java , EntryComparitor.java, and OutOfRangeException.java given in the class (any other implementation of above classes that are not compatible with List interface and AbstractPQueue class will not receive any credit). 2/ Task2: implement a concrete HeapPQueue class (a Heap based priority queue) that extends...

  • 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; /**...

  • Can you help with the merge method? This method should create and return an ArrayBagcontaining one...

    Can you help with the merge method? This method should create and return an ArrayBagcontaining one occurrence of any item that is found in either the called object or the parameter other. For full credit, the resulting bag should not include any duplicates. Give the new ArrayBag a maximum size that is the sum of the two bag’s maximum sizes. import java.util.*; /** * An implementation of a bag data structure using an array. */ public class ArrayBag { /**...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • I need help fixing my java code for some reason it will not let me run...

    I need help fixing my java code for some reason it will not let me run it can someone pls help me fix it. class LinearProbingHashTable1 { private int keyname; private int valuename; LinearProbingHashTable1(int keyname, int valuename) { this.keyname = keyname; this.valuename = valuename; } public int getKey() { return keyname; } public int getValue() { return valuename; } } class LinearProbingHashTable2 { private final static int SIZE = 128; LinearProbingHashTable2[] table; LinearProbingHashTable2() { table = new LinearProbingHashTable2[SIZE]; for (int...

  • I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think...

    I hope someone can explain this exercise to me. Thanks +++++++++++++ Programming Exercise Try to think about how to implement KWArrayList class. Please implement the following constructor and methods: public KWArrayList() public boolean add(E anEntry) public E get(int index) { public E set(int index, E newValue) public E remove(int index) private void reallocate() public int size() public int indexOf(Object item)       Study the code for ArrayList implementation (enclosed in the folder) and work on the following exercise Provide a constructor...

  • I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHea

    I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHeap.Java package structures; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; public abstract class AbstractArrayHeap<P, V> {   protected final ArrayList<Entry<P, V>> heap;   protected final Comparator<P> comparator; protected AbstractArrayHeap(Comparator<P> comparator) {     if (comparator == null) {       throw new NullPointerException();     }     this.comparator = comparator;     heap = new ArrayList<Entry<P, V>>();   } public final AbstractArrayHeap<P, V> add(P priority, V value) {     if (priority == null || value...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Java: Return an array of booleans in a directed graph. Please complete the TODO section in...

    Java: Return an array of booleans in a directed graph. Please complete the TODO section in the mark(int s) function import algs13.Bag; import java.util.HashSet; // See instructions below public class MyDigraph { static class Node { private String key; private Bag<Node> adj; public Node (String key) { this.key = key; this.adj = new Bag<> (); } public String toString () { return key; } public void addEdgeTo (Node n) { adj.add (n); } public Bag<Node> adj () { return adj;...

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