private Listable[] items; /** * Adds an item to the list. This method assumes th
ID: 3568594 • Letter: P
Question
private Listable[] items;
/** * Adds an item to the list. This method assumes that the list is already * sorted in descending order based on the retail prices of the items in * the list. * * The new item will be inserted into the list in the appropriate place so * that the list will remain in descending retail price order. * * In order to accomodate the new item, the internal array must be re-sized * so that it is one unit larger than it was before the call to this method. * * @param itemToAdd refers to a Listable item to be added to this list */
public void add(Listable itemToAdd) {
}
Explanation / Answer
public class SortedListOfImmutables
{
private Listable[] items;
public SortedListOfImmutables()
{
items = new Listable[0];
}
/**
* @param other the list that is to be copied
*/
public SortedListOfImmutables(SortedListOfImmutables other)
{
Listable[] tempItems = new Listable[other.items.length];
for(int i = 0; i < other.items.length;i++){
tempItems[i] = other.items[i];
}
items = tempItems;
}
/**
* Returns the number of items in the list.
* @return number of items in the list
*/
public int getSize() {
int numItems = items.length;
return numItems;
}
/**
* Returns a reference to the item in the ith position in the list (Indexing is 0-based, so the first element is element 0).
* @param i index of item requested
* @return reference to the ith item in the list
*/
public Listable get(int i)
{
return items[i];
}
/**
* Adds an item to the list. This method assumes that the list is already sorted in descending order based on the retail prices of the items in the list.
* The new item will be inserted into the list in the appropriate place so that the list will remain in descending retail price order.
* In order to accomodate the new item, the internal array must be re-sized so that it is one unit larger than it was before the call to this method.
*
* @param itemToAdd refers to a Listable item to be added to this list
*/
public void add(Listable itemToAdd) {
int length = items.length+1;
Listable[] tempItem = new Listable[length];
if(items.length == 0){
tempItem[0] = itemToAdd;
}
else{
int addPlace = 0;
for(addPlace = 0; addPlace < items.length; addPlace++){
if(items[addPlace].getRetailValue() < itemToAdd.getRetailValue()){
break;
}
}
for(int i = 0; i < addPlace; i++){
tempItem[i] = items[i];
}
tempItem[addPlace] = itemToAdd;
for(int j = addPlace; j < items.length; j++){
tempItem[j+1] = items[j];
}
}
items = tempItem;
}
/**
* Adds an entire list of items to the current list, maintaining the
* ordering of the list by descending retail prices of the items.
*
* @param listToAdd a list of items that are to be added to the current object
*/
public void add(SortedListOfImmutables listToAdd) {
for(int i = 0; i < listToAdd.items.length; i++){
add(listToAdd.items[i]);
}
}
/**
* Removes an item from the list.
* If the list contains the same item that the parameter refers to, it will be removed from the list.
* If the item appears in the list more than once, just one instance will be removed.
* If the item does not appear on the list, then this method does nothing.
* @param itemToRemove refers to the item that is to be removed from list
*/
public void remove(Listable itemToRemove) {
Listable[] tempList = new Listable[items.length-1];
for(int delPlace = 0; delPlace < items.length; delPlace++){
if(items[delPlace].equals(itemToRemove)){
for(int i = 0; i < delPlace; i++){
tempList[i] = items[i];
}
for(int h = delPlace+1; h < items.length; h++){
tempList[h-1] = items[h];
}
items = tempList;
break;
}
}
}
/**
* Removes an entire list of items from the current list. Any items in the parameter that appear in the current list are removed; any items in the parameter that do not appear in the current list are ignored.
* @param listToRemove list of items that are to be removed from this list
*/
public void remove(SortedListOfImmutables listToRemove) {
for(int i = 0; i < listToRemove.items.length; i++){
remove(listToRemove.items[i]);
}
}
/**
* Returns the sum of the wholesale costs of all items in the list.
* @return sum of the wholesale costs of all items in the list
*/
public int getWholesaleCost() {
int wholesaleCost = 0;
for(int i = 0; i < items.length; i++){
wholesaleCost += items[i].getWholesaleCost();
}
return wholesaleCost;
}
/**
* Returns the sum of the retail values of all items in the list.
* @return sum of the retail values of all items in the list
*/
public int getRetailValue() {
int retailVal = 0;
for(int i = 0; i < items.length; i++){
retailVal += items[i].getRetailValue();
}
return retailVal;
}
/**
* Checks to see if a particular item is in the list.
* @param itemToFind item to look for
* @return true if the item is found in the list, false otherwise
*/
public boolean checkAvailability(Listable itemToFind) {
boolean check = false;
for(int i = 0; i < items.length; i++){
if(items[i].equals(itemToFind)){
check = true;
}
}
return check;
}
public boolean checkAvailability(SortedListOfImmutables listToCheck) {
boolean[] tester = new boolean[items.length];
for(int i = 0; i < listToCheck.items.length; i++){
boolean check = false;
for(int h = 0; h < items.length; h++)
if(!tester[h] && listToCheck.items[i].equals(items[h])){
tester[h] = true;
check = true;
break;
}
if(!check){
return false;
}
}
return true;
}
public String toString() {
String retValue = "[ ";
for (int i = 0; i < items.length; i++) {
if (i != 0) {
retValue += ", ";
}
retValue += items[i];
}
retValue += " ]";
return retValue;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.