bad code for bubble sort?

cannot find symbol variable, trying to use bubble sort.

Page 1 of 1

2 Replies - 2299 Views - Last Post: 02 February 2008 - 05:04 PM Rate Topic: -----

#1 rturner7720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 08

bad code for bubble sort?

Posted 02 February 2008 - 03:27 AM

I have 2 file a class file Product.java, and the java file containing the method InventoryPart2.java. I must use a bubble sort to sort the inventory, but I seem to be missing something.

public class InventoryPart2
 {
	 public static void main( String[] args)
 {
	   // create array of inventory of items



		 Product Items[] = new Product[3]; //Initializing the array
		
		 Items[0] = new Product(1,"pencils",100,0.5);
		 Items[1] = new Product(2,"notepads",200,2);
		 Items[2] = new Product(3,"printers",150,1);
		

	 }

	private static void SortProductArray() // method for bubble sort
	{
	int flag = 0; //create a value to use as an indicator for sort
	Product temp; //create a temporary object for sort

	//Enter a while loop to sort the array
	while(flag == 0)
	{
		flag = 1;

		//enter a loop through the product array for sorting
		for(int x = 0; x < Items.length - 1; ++x)
		{
			//See if this value is greater than the next
			if(Items[x].name.comparteTo(Items[x+1].name) > 0)
			{
				//Switch this item and the next
				temp = Items[x];
				Items[x] = Items[x+1];
				Items[x+1] = temp;

				//reset the flag to 0
				flag = 0;
			}//end if
		}//end for loop
	}//end while loop
   }//end sort method


	public static void displayProduct(Product items[])
  {
	//Display the inventories one at a time using a for loop
		for (int i=0;i<items.length;i++)
  {
			items[i].showInventory();
		}
	}
 
  } //end




//Robert Turner. Product class for inventory program.

public class Product {
	private int itemNum;
	private String name = new String();
	private int units;
	private double price;

   //sets and gets
	public int getItemNum() 
	{
		return itemNum;
	}

	public void setItemNum(int itemNum) 
	{
		this.itemNum = itemNum;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public int getUnits() 
	{
		return units;
	}

	public void setUnits(int units) 
	{
		this.units = units;
	}

	public double getPrice() 
	{
		return price;
	}

	public void setPrice(double price) 
	{
		this.price = price;
	}

	//Constructor that takes arguments
	Product(int _itemNum, String _name, int _units, double _price){	 //Constructor
	itemNum = _itemNum;
	name = _name;
	units = _units;
	price = _price;
	}

	//Computes value of inventory
	public double valueOfInventory()
	{
		return price * units;
	}

	//displays the details of the inventory
	public void showInventory(){
		System.out.println("Product Name   : "+name);
		System.out.println("Item Number	: "+itemNum);
		System.out.println("Number of Units: "+units);
		System.out.println("Unit Price $"+price);
		//call the valueOfInventory() method and display the value
		System.out.println("The value of the inventory of "+name+ " is = "+valueOfInventory());
	}

public int compareTo(Product o) {

		int EQUAL = 0;
		int comparison = this.name.compareTo(o.name);
		if ( comparison != EQUAL )
			return comparison;

		return 0;
	}
}



error states:
cannot find symbol
symbol : variable items
location: class InventoryPart2
for (int x =0;x < Items.length - 1; ++x)
^

what am I missing?

Is This A Good Question/Topic? 0
  • +

Replies To: bad code for bubble sort?

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: bad code for bubble sort?

Posted 02 February 2008 - 04:21 AM

You did it once displayProduct(Product items[]).

Inside SortProductArray() there is no Items object. You'll have to pass it one, e.g. SortProductArray(Product Items[]). I'd try to be consistent with the naming and make all references to items lower case.

Also, try to get out of the habit of static. It's only good for main. ;) Another approach would be to make items part of the InventoryPart2 object and all the methods use the object's data.

Here's what I'm talking about.
public class InventoryPart2 {
	// array of items available to the object
	private Product items[];
		
	public InventoryPart2() {
		//Initializing the array
		this.items = new Product[] {
			new Product(1,"pencils",100,0.5),
			new Product(2,"notepads",200,2), 
			new Product(3,"printers",150,1)
		};
	}
	
	public void SortProductArray() { ... }
	
	// all code that calls items actually calls this.items
	// each InventoryPart2 object could hold a unique list of items.
	public void displayProduct() {
		for (int i=0;i<this.items.length;i++) {
			items[i].showInventory();
		}
	}

	public static void main( String[] args) {
		// create an instance of class InventoryPart2
		// here, we'll call our object pgm
		InventoryPart2 pgm = new InventoryPart2();
		pgm.displayProduct();
		pgm.SortProductArray();
		pgm.displayProduct();
	}
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 rturner7720  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-February 08

Re: bad code for bubble sort?

Posted 02 February 2008 - 05:04 PM

Yes that helped thank you. I now have the program compile without errors. But now I am afraid I have another problem with it. When I run the program -nothing displays. I need it to display the items in the inventory in order by name of product and then display the total value of inventory (I haven't put in formula to calculate total value yet, fixing other errors first). But I simply get one blank line and then the command prmpt. At least Those pesky errors are gone.


public class InventoryPart2
 {
	 public static void main( String[] args)
 {
	   // create array of inventory of items



		 Product Items[] = new Product[3]; //Initializing the array
		
		 items[0] = new Product(1,"pencils",100,0.5);
		 items[1] = new Product(2,"notepads",200,2);
		 items[2] = new Product(3,"printers",150,1);
		

	 }

private static void modifyArray( Product items[] )//start bubble sort 

method
	{
		int flag=0;
		Product temp;
		while (flag==0)
		{
			flag = 1;
			for (int x=0; x<items.length - 1; ++x)
			{
				if (items[x].getName().compareTo(items[x+1].getName())>0)
				{
					temp=items[x];
					items[x]=items[x+1];
					items[x+1]=temp;

				//reset the flag to 0
				flag = 0;
			}//end if
		}//end for loop
	}//end while loop
   }//end sort method


  
 //Display the inventories one at a time.

  public void displayProduct(Product items[]) {
		for (int i=0;i<items.length;i++) {
			items[i].showInventory();
		}
	}


	  
 
  } //end

  [\code]
[code]
//Robert Turner. Product class for inventory program.

public class Product {
	private int itemNum;
	String name = new String();
	private int units;
	private double price;

   //sets and gets
	public int getItemNum() 
	{
		return itemNum;
	}

	public void setItemNum(int itemNum) 
	{
		this.itemNum = itemNum;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public int getUnits() 
	{
		return units;
	}

	public void setUnits(int units) 
	{
		this.units = units;
	}

	public double getPrice() 
	{
		return price;
	}

	public void setPrice(double price) 
	{
		this.price = price;
	}

	//Constructor that takes arguments
	Product(int _itemNum, String _name, int _units, double _price){	 

//Constructor
	itemNum = _itemNum;
	name = _name;
	units = _units;
	price = _price;
	}

	//Computes value of inventory
	public double valueOfInventory()
	{
		return price * units;
	}

	//displays the details of the inventory
	public void showInventory(){
		System.out.println("Product Name   : "+name);
		System.out.println("Item Number	: "+itemNum);
		System.out.println("Number of Units: "+units);
		System.out.println("Unit Price $"+price);
		//call the valueOfInventory() method and display the value
		System.out.println("The value of the inventory of "+name+ " is = 

"+valueOfInventory());
	}


public int compareTo(Product o) {

		int EQUAL = 0;
		int comparison = this.name.compareTo(o.name);
		if ( comparison != EQUAL )
			return comparison;

		return 0;
	}

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1