Beginner to Java: Constructors and Sorts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #16
    I don't know how to pass the entire array of objects to my method. I keep getting errors when I try. Here is how I attempt to pass it.
    The line "sortPrice(arr) ;" is the correct way of passing the array as an argument.

    The problem is with the next line which should not be inside the main() method. Eg it should appear after main() the way you had it in #5.

    Code:
    public static void main(String[] args) {
        // etc
        sortPrice(arr);
    }
    
    private void sortPrice(Item[] arr) {
        // TODO
    }

    Comment

    • Misaio
      New Member
      • Mar 2010
      • 16

      #17
      pb:

      I'm still having error codes with that, even after taking the methods out of the main.

      Errors:
      CoffeeDriver.ja va:37: missing method body, or declare abstract
      private static void sortPrice(Item array[]);
      ^
      CoffeeDriver.ja va:41: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      for (x = 0; x < array.length; x++)
      ^
      CoffeeDriver.ja va:43: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      if(array[x].getPrice() > array[x+1].getPrice())
      ^
      CoffeeDriver.ja va:43: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      if(array[x].getPrice() > array[x+1].getPrice())
      ^
      CoffeeDriver.ja va:45: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      temp = array[x];
      ^
      CoffeeDriver.ja va:46: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      array[x] = array[x+1];
      ^
      CoffeeDriver.ja va:46: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      array[x] = array[x+1];
      ^
      CoffeeDriver.ja va:47: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      array[x+1] = temp;
      ^
      CoffeeDriver.ja va:50: cannot find symbol
      symbol : variable array
      location: class CoffeeDriver
      System.out.prin tln(array[x]);
      ^
      CoffeeDriver.ja va:52: missing method body, or declare abstract
      private static void sortName(Item arr[]);
      ^
      CoffeeDriver.ja va:56: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      for (x = 0; x < arr.length; x ++)
      ^
      CoffeeDriver.ja va:58: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      if(arr[x].getName().comp areTo(arr[x+1].getName) > 0)
      ^
      CoffeeDriver.ja va:58: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      if(arr[x].getName().comp areTo(arr[x+1].getName) > 0)
      ^
      CoffeeDriver.ja va:60: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      temp = arr[x];
      ^
      CoffeeDriver.ja va:61: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      arr[x] = arr[x+1];
      ^
      CoffeeDriver.ja va:61: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      arr[x] = arr[x+1];
      ^
      CoffeeDriver.ja va:62: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      arr[x+1] = temp;
      ^
      CoffeeDriver.ja va:64: cannot find symbol
      symbol : variable arr
      location: class CoffeeDriver
      System.out.prin tln(arr[x]);
      ^
      Item.java:9: missing method body, or declare abstract
      public String getName();
      ^
      Item.java:11: return outside method
      return itemName;
      ^
      Item.java:15: return outside method
      return itemPrice;
      ^


      Code:
      import java.util.Arrays;
      import javax.swing.JOptionPane;
      public class CoffeeDriver
      {
      	public static void main (String[] args)
      	{
      		String userInput;
      		int x;
      		int selectSort;
      		Item arr[] = new Item[5];
      		arr[0] = new Item("Coffee", 1.00);
      //....etc
      	}
      
      	private static void sortPrice(Item array[]);
      	{
      		int x;
      		Item temp;
      		for (x = 0; x < array.length; x++)
      		{	
      			if(array[x].getPrice() > array[x+1].getPrice())
      				{
      					temp = array[x];
      					array[x] = array[x+1];
      					array[x+1] = temp;
      				}
      		}
      			System.out.println(array[x]);
      	}
      	private static void sortName(Item arr[]);
      	{
      		int x;
      		Item temp;
      		for (x = 0; x < arr.length; x ++)
      		{
      			if(arr[x].getName().compareTo(arr[x+1].getName) > 0)
      				{
      					temp = arr[x];
      					arr[x] = arr[x+1];
      					arr[x+1] = temp;
      				}
      			System.out.println(arr[x]);
      		}
      	}
      }
      Code:
      public class Item
      {
      	private String itemName;
      	private double itemPrice;
      	public String getName();
      	{
      		return itemName;
      	}
      	public double getPrice;
      	{
      		return itemPrice;
      	}
      	public Item ()
      	{
      		//use this for my temp sorting!
      	}
      	public Item (String itemName, double itemPrice)
      	{
      		this.itemPrice = itemPrice;
      		this.itemName = itemName;
      	}
      	public void setItemName(String someItem)
      	{
      		itemName = someItem;
      	}
      	public void setItemPrice(double somePrice)
      	{
      		itemPrice = somePrice;
      	}
      }

      Comment

      • Misaio
        New Member
        • Mar 2010
        • 16

        #18
        Is there anyway someone can take that last post out? The errors were completely my oversight. I put a lot of extra ;'s in there, but a nap and some coffee allowed me to wake up and see it.

        Comment

        • pbrockway2
          Recognized Expert New Member
          • Nov 2007
          • 151

          #19
          I think you can edit the post...

          I haven't run that code, but it looks good. One small point: I don't think you need the no argument Item constructor. Although you declare a temp variable in the sorting methods, a declaration doesn't require that you have a no argument constructor. "Item temp;" means something like "let 'temp' be a reference to an item object" and until you assign something else to it the value of temp will be null.

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #20
            On second thoughts you might want to check the sort methods: the Wikipedia bubble sort has a for loop inside a do loop. Your single for loop may not completely sort the items.

            Comment

            • Misaio
              New Member
              • Mar 2010
              • 16

              #21
              Yeah, I was having problems with my output and looking into ways of sorting it. I was hoping I wouldn't need a nested loop, but...I have to go with what works, right? I'll check Wiki.

              Comment

              • Misaio
                New Member
                • Mar 2010
                • 16

                #22
                I'm still trying to figure out just what I'm trying to do with this. This is what I started with:
                Code:
                	public static void sortPrice(Item array[])
                	{
                		int x;
                		int j =0;
                		Item temp;
                			while (j < 5)
                			{
                				j++;
                				for (x = 0; x < array.length; x++)
                				{	
                					if(array[x].getPrice() > array[x+1].getPrice())
                					{
                						temp = array[x];
                						array[x] = array[x+1];
                						array[x+1] = temp;
                					}
                				}
                		}
                But that didn't seem to do much of anything.

                Comment

                • Misaio
                  New Member
                  • Mar 2010
                  • 16

                  #23
                  Alright, so I'm just working on the price method right now. It seems to go rather well until I get to the output. I get this bizarre thing that I'm sure many of you have seen before.

                  [Item@fabe9, Item@df6ccd, Item@601bb1, Item@1ba34f2, Item@1ea2dfe]

                  I looked up the odd outputs and was told that Arrays.deepToSt ring(arrays)); would help. Here is the method I'm working with. But as I print it out again...I still get the weird output!

                  Code:
                  	public static void sortPrice(Item array[])
                  	{
                  		int x;
                  		int j;
                  		int compare = array.length - 1;
                  		Item temp;
                  			for(j = 0; j < array.length - 1; ++j)
                  			{
                  				for (x = 0; x < compare; x++)
                  				{	
                  					if(array[x].getPrice() > array[x+1].getPrice())
                  					{
                  						temp = array[x];
                  						array[x] = array[x+1];
                  						array[x+1] = temp;
                  					}
                  				}
                  				--compare;
                  			}
                  			System.out.println(Arrays.deepToString(array));
                  	}

                  Comment

                  • pbrockway2
                    Recognized Expert New Member
                    • Nov 2007
                    • 151

                    #24
                    "Item@df6cc d" is the default way of printing a reference to an object like an Item. To get a more human readable output you have to add a method to the Item class.

                    Code:
                    public class Item
                    {
                        // etc as before
                    
                        public String toString()
                        {
                                // or whatever string you want to represent an item
                            return itemName + ": " + itemPrice;
                        }
                    }

                    Comment

                    Working...