2 Replies - 372 Views - Last Post: 10 December 2011 - 02:59 PM Rate Topic: -----

#1 jediv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-October 11

Modify an add method for an array that keeps array in sorted order

Posted 10 December 2011 - 12:20 PM

public class Item implements Comparable
{
    /**Declaration of variables is done below*/
    private int itemNum; //integer variable called item number is declared
    private String title; //String variable called title is declared
    private double price; //a double variable called price is declared

    /**Constructor named Item is created.*/
    public Item(int id, String t, double p)
    {
        /**initializing of instance variables.*/
        itemNum = id;
        title = t;
        price = p;
    }

    /**Return the item number of this item.*/
    public int getItemNumber()
    {
        return itemNum;
    }
    
    /**Return the title of this item*/
    public String getTitleNumber()
    {
    	return title;
    }
    
    
    /**Return the item type. This is overridden in subclasses.*/
    public String getItemType()
    {
        return "Item";
    }
    
    /**Return a printable String representation of this item.*/
    public String toString()
    {
        String line1, line2, line3, line4, out;
        String itemtype = this.getItemType();
        line1 = String.format("Item number: %d%n", itemNum);
        line2 = String.format("Item type:   %s%n", itemtype);
        line3 = String.format("Item title:  %s%n", title);
        line4 = String.format("Item price:  %.2f%n", price);
        out = line1 + line2 + line3 + line4;
        return out;
    }
    
    public boolean equals (Object other)
    {
    	return (this.title.equals(((Item)other).getTitleNumber()) &&
    			this.itemNum == (((Item)other).getItemNumber()));
    }
    
    
    public int compareTo (Object other)
    {
    int result;
    String s;
    String otherS;
    
    int otherItemNum = ((Item)other).getItemNumber();
    String otherTitle = ((Item)other).getItemType();
    
    if (title.equals(otherTitle))
    	result = (s = "" + itemNum).compareTo(( otherS = "" + otherItemNum));
    else 
    	result = title.compareTo(otherTitle);
    
    return result;
    }
}



public class Catalog
{

	private Item [] item;
	private int size;
	private static final int maxsize = 20;
	/**
	 * Instantiates the array
	 */
	public Catalog()
	{
		item = new Item[maxsize];
		size = 0;
	}

	/**method will allow items to be added to array*/
	public void addItem(Comparable[] list, Item x)
	{ 
		for (int index = 1; index < size; index++)
	      {
	         Comparable key = list[index];
	         int position = index;

	         //  Shift larger values to the right
	         while (position > 0 && key.compareTo(list[position-1]) < 0)
	         {
	            list[position] = list[position-1];
	            position--;
	         }
	            
	         list[position] = key;
	      }		
		
		if(size < maxsize)
		{	
			item[size] = x;
			size++;
		}
		
		

		else if (size > maxsize)
			System.out.println("You have reached the maximum number of items.");
		 
}

	/**method will allow items to be displayed that are in array*/
	public void displayItems(int id)
	{
		for (int i = 0; i< size; i++)
		{
			if (item[i].getItemNumber()==id)
			{
				System.out.println(item.toString());
			}
		}
	}
	/**method will allow items to be searched for in an array*/
	public Item findItem(int id)
	{ 
		for (int i = 0; i < size; i++)
		{
			if(item[i].getItemNumber()==id)
			{
				return item[i];
			}
		}
		return null;
	}
	/**method will allow item(s) to be deleted from an array if they are in the array*/
	public Item removeItem(int id)
	{
		for(int i = 0; i < size; i++)
		{      
			if(item[i].getItemNumber()==id)
			{
				item[i] = item[i+1];
				item[i] = item[i-1];
			}
		}           	
		return null;
	}

	public Item displayAllItems()
	{
		for(int i = 0; i < size; i++)

			System.out.println(item[i]);

		return null;
	}
}






public static void insertionSort (Comparable[] list)
   {
      for (int index = 1; index < list.length; index++)
      {
         Comparable key = list[index];
         int position = index;

         //  Shift larger values to the right
         while (position > 0 && key.compareTo(list[position-1]) < 0)
         {
            list[position] = list[position-1];
            position--;
         }
            
         list[position] = key;
      }
   }


My code is working up to the point when I am trying to modify the addItem method. I have to use array rather than arrayList. Here are my directions for this segment of my project... In Catalog: The method that adds an Item into the array should be modified so that it adds the Item not just at the end, but in the proper position in the array so that it stays sorted. (You will need one loop similar to the inner loop of insertion sort, and will use the compareTo method of Item here). I have been trying to figure this out. I am looking for some guidance, I really am having a hard time wrapping my head around this. So my question is this, how can I properly implement this inner loop while not blowing up my code that is already working. I really don't want the answers but want to understand it. I need someone to dumb it down for me if you would not mind. It is Saturday and reaching anyone for help at my school is not working very well. I can conceptualize this but the code snippet just does not make much sense to me as I try to implement it as I have done.

Error code I get...

Exception in thread "main" java.lang.NullPointerException
at Catalog.addItem(Catalog.java:39)
at CatalogInterface.readInfileIntoCatalog(CatalogInterface.java:209)
at MyProgram.main(MyProgram.java:6)

Is This A Good Question/Topic? 0
  • +

Replies To: Modify an add method for an array that keeps array in sorted order

#2 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 268
  • Joined: 21-May 09

Re: Modify an add method for an array that keeps array in sorted order

Posted 10 December 2011 - 02:50 PM

thats not VB6 code
thats C
Was This Post Helpful? 0
  • +
  • -

#3 jediv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-October 11

Re: Modify an add method for an array that keeps array in sorted order

Posted 10 December 2011 - 02:59 PM

View PostNeku, on 10 December 2011 - 02:50 PM, said:

thats not VB6 code
thats C


It is Java. Sorry I posted in the wrong forum. I need to find a way to delete it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1