Question: /* * NOTE TO PROGRAMMER: * Do not alter this file! */ public interface List { /** * Inserts an item to into the list

/*
* NOTE TO PROGRAMMER:
* Do not alter this file!
*/
public interface List
/**
* Inserts an item to into the list at the positioned index.
* Any items after the new item will have their index position increased by one.
* For example, if list is a list of integers {1,2,3}, then
* list.add(9,0) will alter the list to contain {9,1,2,3}, in
* that order.
* Another call, list.add(5,2) will alter the list to contain {9,1,5,2,3},
* in that order.
* @param item The object to add to the list.
* @param index The index position for the new item.
* Note that the index position may be equal to the number of items, meaning that
* the item will be placed at the end of the list.
* @throws IndexOutOfBoundsException if the index is outside(0 … size).
*/
public void add(E item, int index);
/**
* Accesses the item by its position in the list.
* @param index The index of the position.
* @return The item at that index position.
* @throws IndexOutOfBoundsException if the index is outside (0 … size-1).
*/
public E get(int index);
/**
* @return true if the list is empty, false otherwise.
*/
public boolean isEmpty();
/**
* @return The number of items in the list.
*/
public int size();
/**
* Finds the index position of an equivalent item in the list.
* @param item The item that is equivalent to the item we are looking for.
* @return The index position of the equivalent item.
* If the item is not in the list, then -1 is returned.
*/
public int indexOf(E item);
/**
* Removes the item at the index position of the list.
* @param index The index number.
* @throws IndexOutOfBoundsException if the index is outside (0 … size-1).
*/
public void remove(int index);
/**
* Removes all the items from the list, resulting in an empty list.
*/
public void removeAll();
/**
* Removes all matching items from the list.
* For example, if the list is made up of integers and contains {67,12,13,12},
* then remove(12)
* will alter the list to {67,13}.
* @param value The item that identies what to remove.
* Any item in the list that matches this item will be removed.
* Note that if no matching item can be found, then nothing is removed.
*/
public void remove(E value);
/**
* A printout of the list is in the form:
* List: {
*
* …,
*
*
* where each item is the string returned by its toString method.
* Note that all the items can be listed on one line or placed on separate lines, depending on the
* aesthetics.
* An empty list is represented by List: {}
* @return The string representation of the list, formatted as above.
*/
public String toString();
}
-----------------------------------------------
public class Medication {
private String genericName;
// in milligrams
private double dosage;
/**
* Creates a single medication object.
* @param name The generic name, which is not currently checked for accuracy.
* @param dosage The number of milligrams in a single tablet, capsule or patch.
*/
public Medication(String name, int dosage) {
genericName = name;
this.dosage = dosage;
}
/**
* Creates a single medication object with a default
* dosage of 0 mg.
* @param name The generic name, which is not currently checked for accuracy.
*/
public Medication(String name) {
genericName = name;
}
/**
* @return The generic name of the medication.
*/
public String getName() {
return genericName;
}
/**
* @return The current dosage of the medication.
*/
public double getDosage() {
return dosage;
}
/**
* Updates the dosage.
* @param dosage The updated dosage (in milligrams).
*/
public void setDosage(double dosage) {
this.dosage = dosage;
}
/**
* @return A string representation of this medication,
* using the following format:
*
*
*
* where is the actual name and is a number,
* with no more than 3 decimal precision.
*/
public String toString() {
return genericName+":"+dosage+"mg";
}
/**
* Two medications are equivalent if they have the same
* generic name and equal dosages.
* @param other The medication that may or may not be equivalent to this medication.
* @return True if this medication is equivalent to other, false otherwise.
*/
public boolean equals(Medication other) {
return genericName.equalsIgnoreCase(other.genericName)
&& dosage==other.dosage;
}
/**
* Determines the ordering of two medications.
* Ordering is determined by the alphabetical ordering of the name
* followed by the numerical ordering of the dosage.
* @param other The medication to compare to this medication.
* @return n, where n is either:
- 0 if the medications are equivalent,
-
*
- > 0 if this medication comes after the other mediation.
*
*
*
*/
public int compareTo(Medication other) {
int cmpName = genericName.compareTo(other.genericName);
if (cmpName == 0) {
return (int)(dosage*1000) - (int)(other.dosage*1000);
} else {
return cmpName;
}
}
----------------------------------------------------
class MedicationNode {
protected Medication item;
protected MedicationNode prev;
protected MedicationNode next;
/**
* Creates a Node that contains a Medication object.
* @param item The Medication contained in the node.
* @param prev The node that comes before this one in the list.
* @param next The node that comes after this one in the list.
*/
protected MedicationNode(Medication item, MedicationNode prev, MedicationNode next) {
this.item = item;
this.prev = prev;
this.next = next;
}
/**
* Creates a Node that contains a Medication that is not attached to the list.
* @param item The Medication contained in the node.
*/
protected MedicationNode(Medication item) {
this(item,null,null);
}
}
-----------------------------------------------
import java.util.NoSuchElementException;
// To provide a comparison between a List that uses an array and a list that uses a linked data structure
/*
* The MedListArrayBased is a list that uses an array as
* the storage for its Medications.
*/
public class MedListArrayBased implements List
private Medication[] storage;
private int count;
private static final int INITIAL_SIZE=3;
/**
* Creates and initializes an empty List of Medication objects.
*/
public MedListArrayBased() {
storage = new Medication[INITIAL_SIZE];
count = 0;
}
/**
* A private helper method for use within this file only.
* This doubles the size of the storage array.
*/
private void growAndCopy() {
Medication[] newStorage = new Medication[storage.length*2];
// Move all the integers from storage into newStorage
for (int i=0; i newStorage[i] = storage[i]; } storage = newStorage; } /** * A private helper method that moves all the elements after * the given index position, one position to the left. * Because it is private, error-checking is up to the programmer. * @param fromIndex The index position where the sliding begins. */ private void slideLeftTo(int fromIndex) { for (int i=fromIndex; i storage[i] = storage[i+1]; } } /** * A private helper method that moves all the elements after * the given index position, one postion to the right. * No error-checking is done for the validity of the index variable. * @param fromIndex The index position where the sliding begins. */ private void slideRightFrom(int fromIndex) { for (int i=count; i>fromIndex; i--) { storage[i] = storage[i-1]; } } /** * A private helper method that is used primarly for debugging * and testing the array object during operations. */ private void printArray() { System.out.println("Actual Array contents:"); for (int i=0; i if (storage[i] != null) { System.out.println(i+" : "+storage[i]); } else { System.out.println(i+" : null"); } } System.out.println("Count = "+count); } /* * List Interface methods... * NOTE THAT THESE do not need header comments. They inherit * the comments from the List interface. */ public void add(Medication k,int index) { // since we are adding, the index can be one place after the end. if (index count) { throw new IndexOutOfBoundsException("The index "+index+" is out of bounds."); } if (count==storage.length) { growAndCopy(); } slideRightFrom(index); storage[index] = k; count++; } public Medication get(int index) { if (index count-1) { throw new IndexOutOfBoundsException("The index "+index+" is out of bounds."); } return storage[index]; } public boolean isEmpty() { return count==0; } public int size() { return count; } public int indexOf(Medication item) { for (int i=0; i if (storage[i].equals(item)) { return i; } } // if we get here, the item is not there. return -1; } public void remove(int index) { if (index count-1) { throw new IndexOutOfBoundsException("The index "+index+" is out of bounds."); } slideLeftTo(index); storage[count--] = null; } public void remove(Medication item) { for (int i=0; i if (storage[i].equals(item)) { slideLeftTo(i); storage[count--] = null; // need to look at the new item in this spot i--; } } } public void removeAll() { // create a new array and leave the other for garbage collection. storage = new Medication[INITIAL_SIZE]; count = 0; } public String toString() { if (count == 0) return "{}"; StringBuilder s = new StringBuilder(count*10); // just an estimate of the size s.append("List: { "); for (int i=0; i s.append("\t"+storage[i]+", "); } if (count>0) { s.append("\t"+storage[count-1]+" "); } s.append("}"); return s.toString(); } /** * The main method is a test harness that allows this programmer to * do some tests to make sure the code is good enough for market. * @param args Some command line arguments that are not used. */ public static void main(String[] args) { MedListArrayBased list = new MedListArrayBased(); list.add(new Medication("meperidine",100),0); list.add(new Medication("acetylsalicylic acid",325),0); list.add(new Medication("acetominophen",100),0); list.add(new Medication("cimetidine",150),3); list.add(new Medication("meperidine",100),0); System.out.println("The list should be {meperidine,acetominophen,asa,meperidine,cimetidine}"); System.out.println(list); list.printArray(); // check to make sure the private shift methods work: System.out.println("After sliding everything right from the second spot:"); list.slideRightFrom(1); list.printArray(); // The list thinks its longer now. list.count++; System.out.println("list version: "+list); System.out.println("After sliding them back again:"); list.slideLeftTo(1); list.count--; list.printArray(); System.out.println("list version: "+list); list.remove(new Medication("meperidine",100)); System.out.println("After removing meperidine:"); System.out.println(list); System.out.println("The number of elements is "+list.size()); System.out.println("Testing the remove request for an item not in the list:"); if (list.indexOf(new Medication("meperidine",100)) != -1) { System.out.println("didn't work"); } else { list.remove(new Medication("meperidine",100)); if (list.size() == 3) { System.out.println("worked as expected"); } else { System.out.println("Something went wrong"); } } System.out.println("After removing the item at index 1:"); list.remove(1); System.out.println(list); list.removeAll(); System.out.println("After removing all the elements:"); System.out.println(list); System.out.println("The number of elements is now "+list.size()); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
