import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public...

50.1K

Verified Solution

Question

Programming

import java.util.ArrayList;
import java.util.Collections;
import java.lang.Exception;

public class ItemList
{
/** This is the main process for the project */
public static void main ()
{
System.out.println(\"\n\tBegin Item List Demo\n\");

System.out.println(\"Declare an ArrayList to hold Itemobjects\");
ArrayList list = new ArrayList();

try
{
System.out.println(\"\n Add several Items to the list\");
list.add(new Item(123, \"Statue\"));
list.add(new Item(332, \"Painting\"));
list.add(new Item(241, \"Figurine\"));
list.add(new Item(126, \"Chair\"));
list.add(new Item(411, \"Model\"));
list.add(new Item(55, \"Watch\"));

System.out.println(\"\nDisplay original Items list:\");
listItems(list);

int result = -1;
// 1. TO DO: change the XXX to a number in the list, and YYY to anumber
// not in the list (1 Pt)
System.out.println(\"\nLinear Search list for items XXX andYYY:\");
// 2. TO DO: code a call to linearSearch with the item number(XXX)
// that is in the list; store the return in the variable result (2Pts)

  
if(result >= 0)
// 3. TO DO: change both XXX numbers to the number searched for (1Pt)
System.out.printf(\"Item XXX found at pos %d\n\", result);
else
System.out.printf(\"Item XXX not found in list\n\");

// 4. TO DO: code a call to linearSearch with the item number(yyy)
// that is not in the list; store the return in the variable result(2 Pts)
  
  
if(result >= 0)
// 5. TO DO: change both YYY to the number searched for (1Pt)
System.out.printf(\"Item YYY found at pos %d\n\", result);
else
System.out.printf(\"Item YYY not found in list\n\");

System.out.println(\"\nSort list into Item Numbersequence\");
// 6. TO DO: code to sort the array using the Collections class (5Pts)

  
System.out.println(\"\nDisplay the Sorted Items list:\");
listItems(list);

// 7. TO DO: change the AAA to a number in the list, and BBB toa number
// not in the list (use two different numbers) (1 Pt)
System.out.println(\"\nBinary Search list for items AAA andBBB5:\");
// 8. TO DO: code a call to binarySearch with the item number(AAA)
// that is in the list; store the return in the variable result (3Pts)
  

if(result >= 0)
// 0. TO DO: change both AAA numbers to the number searched for (1Pt)
System.out.printf(\"Item AAA found at pos %d\n\", result);
else
{
System.out.printf(\"Item AAA not found in list\n\");
System.out.printf(\"Should be at pos %d\n\", -result -1); // noteconversion
}
// 10. TO DO: code a call to binarySearch with the item number(BBB)
// that is not in the list; store the return in the variable result(3 Pts)
  
  
if(result >= 0)
// 11. TO DO change both BBB numbers to the number searched for (1Pt)
System.out.printf(\"Item BBB found at pos %d\n\", result);
else
{
System.out.println(\"Item BBB not found in list\");
System.out.printf(\"Should be at pos %d\n\", -result -1); // noteconversion
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

System.out.println(\"\n\tEnd Item List Demo\n\");
}

/**
* This function performs a linear seach of the list
* @param list - reference to the ArrayList to be searched
* @param number - item number to seek
* @return i - index where item number wass found, -1 if notfound
*/
public static int linearSearch(ArrayListlist, intnumber)
{
for (int i = 0; i < list.size(); i++)
if(list.get(i).getItemNo() == number)
return i;
return -1;
}

/**
* This method traverses the Item list and displays the items
* @param list - reference to the ArrayList of Item Objects
*/
public static void listItems(ArrayList list)
{
for (Item item : list)
System.out.println(item.toString());
}

/**
* This recursive method finds a value in a range of a sortedArrayList,
* using a binary search algorithm.
* @param list - reference to the ArrayList in which to search  
* @param key - the object key value to find
* @return the index at which the key occurs,
* or -n - 1 if it does not occur in the array
* n is the position in which the key object should appear  
*/
public static int binarySearch(ArrayList list, intkey)
{
int low = 0;
int high = list.size() - 1;
int pos = 0;  

while (high >= low)
{
pos = (low + high) / 2;
if (key < list.get(pos).getItemNo())
high = pos - 1;
else if (key == list.get(pos).getItemNo())
return pos;
else
low = pos + 1;
}
return -low - 1;
}
}

import java.lang.Exception;

public class Item implements Comparable
{
/** The Item's identification number */
private int itemNo;
/** The Item's descriptive text */
private String itemDesc;

/**
* Constructs a default Item object
*/
public Item()
{
itemNo = 0;
itemDesc = \"\";
}
  
/**
* Parameter constructor for objects of class Item
* @param number - the Item's number
* @param desc - the Item's description
*/
public Item(int number, String desc) throws Exception
{
setItemNo(number);
setItemDesc(desc);
}

/**
* This method returns the Item identification number  
* @return itemNo - the Item's number
*/
public int getItemNo()
{
return itemNo;
}
  
/**
* Tis method returns the Item's description
* @return itemDesc - the Item's descriptive text
*/
public String getItemDesc()
{
return itemDesc;
}
  
/**
* This method sets the Item's identification number.
* It accepts a number change if and only if the current number iszero(0).
* Otherwise it throws an Exception
* @param number - the Item's new number
* @throw Exception, if number is invalid for this operation
*/
public void setItemNo(int number) throws Exception
{
if (itemNo == 0)
if (number > 0)
itemNo = number;
else
throw new Exception(\"Item number must be greater than zero(0)\");  
else
throw new Exception(\"An existing item number cannot bechanged!\");
}
  
/**
* This method sets the Item's description
* @param desc - the Item's descriptive text
* @throws Exception if input desc is blank or null
*/
public void setItemDesc(String desc) throws Exception
{
if(desc.trim().length() > 0)
itemDesc = desc;
else
throw new Exception(\"Item description cannot be blank\");
}
  
/**
* This method determines if one Item equals another
* @param other - the other Item to be matched
* @return true, if numbers are equal, false if not.
*/
public boolean equals(Item other)
{
return (itemNo == other.itemNo);
}
  
/**
* Provides the compareTo() method for the ComparableInterface
* @param otherObject - the other object for comparison
* @returns a: positive value, if this number is greater than theother
* negative value, if this number is less than the other
* zero, if the numbers are equal to each other
*/
@Override
public int compareTo(Item other)
{
return itemNo - other.itemNo;
}
  
/**
* This method overrides the toSting() method of Object
* Its purpose is to expose the data elements of the Product
* object in String form
* @return String - a string containing the Item's information
*/
@Override
public String toString()
{   
return String.format(\"%4d %-25s\", itemNo, itemDesc);
}
}

Answer & Explanation Solved by verified expert
4.1 Ratings (705 Votes)
Here is the code updated code for ItemList along with the outputscreenshot Please comment if need any further assistance Also doupvote if you are satisfied with my answer THANKSimport javautilArrayListimport javautilCollectionspublic class ItemList public static void mainString args SystemoutprintlnntBegin ItemList Demon SystemoutprintlnDeclarean ArrayList to hold Item objects ArrayList list newArrayList try Systemoutprintlnn Add several Items to the list listaddnewItem123 Statue listaddnewItem332 Painting listaddnewItem241 Figurine listaddnewItem126 Chair listaddnewItem411 Model listaddnewItem55 Watch SystemoutprintlnnDisplay original Items list listItemslist intresult 1 1 TO DO change the XXX to a number in the list    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students