Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default Trying to work out this program

    I'm having a hard time understanding this program.

    Basically the program should either sort the array of items by name or by price and display the full list that is now sorted.

    I get the comparator and all that, but I must be missing something because two of the files will not compile. The objects aren't being recognized


    These two will.

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.text.*;
    
    
    public class CoffeeDriver
    {
    	public static void main(String[] args)
       {
       	char choice;
          Item inventory[] = new Item[4]; 
          
    		inventory[0] = new Item("Coffee", 1.00); 
    		inventory[1] = new Item("Water", 2.00);
    		inventory[2] = new Item("Milk", 1.50);
    		inventory[3] = new Item("Bagel", 1.25);
    		inventory[4] = new Item("Donut", 0.75);
    		
    
    		choice = JOptionPane.showInputDialog(null,
    			"Welcome to Wings Coffee Shop." +
    			"\nWould you like to see these items sorted by" +
    			"\nname or by price? (n/p)",
          JOptionPane.QUESTION_MESSAGE).charAt(0);
          choice = Character.toLowerCase(choice);                          
                           
    		switch(choice)
          {
          	case 'n':  
                Arrays.sort(inventory);
    				System.out.println();
        			System.out.println("Sorted by name");
    
        			for (int i=0; i<4; i++)
    				{
          			Item item = inventory[i];
         				String name = item.getName();
          			double price = item.getPrice();
          			System.out.println(name + "    " + price);
        			} 	
    				break;
             
    			case 'p':
    				Arrays.sort(inventory);
    				System.out.println();
        			System.out.println("Sorted by price");
    
        			for (int i=0; i<4; i++) 
    	 			{
          			Item item = inventory[i];
          			String name = item.getName();
          			double price = item.getPrice();
          			System.out.println(name + "    " + price);
        			}
                break;
             
    			default:
                JOptionPane.showMessageDialog(null,
    				"Invalid entry, Please try again");
                break;
           }         
     	 }
    }

    Java Code:
    import java.util.Comparator;
    public class Item implements Comparable<String>
    {
    	String name;
       double price = 0.00;
       
    	// Constructor
    	public Item(String itemName, double itemPrice)
    	{
    		this.name = itemName;
          this.price = itemPrice;
       }
    	// Set name method
    	public void setName(String name)
    	{
       	this.name = name;
    	}
    	// Get name  method
    	public String getName()
    	{
       	return name;
    	}
    	// Get price method
       public void setPrice(double price)
       {
       	this.price = price;
       }	  
    	// Set price method	  
    	public double getPrice()
       {
       	return price;
       }
     	//compare items by name:
      	public int compareTo(String otherName) 
    	{
        return name.compareTo(otherName);
       }
    }

    These two won't.

    Java Code:
    package comparable.ex03;
    import java.util.Comparator;
    
    public class PriceComparator<Item> implements Comparator<Item>
    {
    	public int compare(Item o1, Item o2)
    	{
    		int itemPrice1 =  o1.getPrice();
          int itemPrice2 =  o2.getPrice();
                    
          if(itemPrice1 > itemPrice2)
    		{
          	return 1;
          }
             else if(itemPrice1 < itemPrice2)
    		{
             return -1;
          }
            else return 0;
    	}
    }
    The errors:
    PriceComparator.java:8: cannot find symbol
    symbol : method getPrice()
    location: class java.lang.Object
    int itemPrice1 = o1.getPrice();
    ^
    PriceComparator.java:9: cannot find symbol
    symbol : method getPrice()
    location: class java.lang.Object
    int itemPrice2 = o2.getPrice();




    Java Code:
    package comparable.ex03;
    import java.util.Comparator;
    
    public class NameComparator<Item> implements Comparator<Item>
    {
    	public int compare(Item o1, Item o2)
    	{
    		String itemName1 = o1.getName();
    		String itemName2 = o2.getName();
    		return itemName1.compareTo(itemName2);
    	}
    }
    The errors:
    NameComparator.java:8: cannot find symbol
    symbol : method getName()
    location: class java.lang.Object
    String itemName1 = o1.getName();
    ^
    NameComparator.java:9: cannot find symbol
    symbol : method getName()
    location: class java.lang.Object
    String itemName2 = o2.getName();




    Isn't there an easier way to sort and display a list like this?
    How do I get this to work???

  2. #2
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default also

    When I run CoffeeDriver I see the initial screen to get the input from the user but then get this error:

    Exception in thread "main" java.lang.ClassCastException: Item cannot be cast to java.lang.String
    at Item.compareTo(Item.java:9)
    at java.util.Arrays.mergeSort(Arrays.java:1144)
    at java.util.Arrays.sort(Arrays.java:1079)
    at CoffeeDriver.main(CoffeeDriver.java:49)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Issues:

    I'm surprised you don't get an array index out of range when you try to add a 5th Item in the [4] slot of your Item[4] array. You should know that this array will hold four Item objects at index [0], [1], [2], and [3].

    If Item implements a generic Comparable, it shouldn't be a Comparable<String> but rather it should extend Comparable<Item> since you're comparing one Item to another. Then in the compareTo method you will pass an Item object in the parameter and compare its String with the "this" String (if String comparison is what you want to do).

  4. #4
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Yeah I fixed that, changed it to a 4.

  5. #5
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    I'm not sure I know what you mean here when referring to change the compareTo method

    how do I get to to compare it from the return statement?

    Java Code:
    import java.util.Comparator;
    
    public class Item implements Comparable<Item>
    {
    	String name;
       double price = 0.00;
       
    	// Constructor
    	public Item(String itemName, double itemPrice)
    	{
    		this.name = itemName;
          this.price = itemPrice;
       }
    	// Set name method
    	public void setName(String name)
    	{
       	this.name = name;
    	}
    	// Get name  method
    	public String getName()
    	{
       	return name;
    	}
    	// Get price method
       public void setPrice(double price)
       {
       	this.price = price;
       }	  
    	// Set price method	  
    	public double getPrice()
       {
       	return price;
       }
     	//compare items by name:
      	public int compareTo(Item otherName) ????
    	{
                return name.compareTo(otherName);????
       }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You want to compare the name held in this Item, with the name held in the other Item, so do just that:

    Java Code:
    public int compareTo(Item other) {
       return name.compareTo(other.getName());
    }

  7. #7
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default Nicee

    Thanks so much! I think I get it.

    However, I realized that the code doesn't sort the prices right. It will sort the items by name but not by the prices.

Similar Threads

  1. JScrollPane doesn't work?
    By ecliptical in forum New To Java
    Replies: 4
    Last Post: 01-25-2010, 01:35 AM
  2. Why doesn't this work?
    By Corder10 in forum New To Java
    Replies: 1
    Last Post: 07-04-2009, 11:33 PM
  3. my loop doesn't work.. pls help???
    By ashton in forum New To Java
    Replies: 5
    Last Post: 01-16-2009, 09:24 AM
  4. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 03:58 AM
  5. Program don't work
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-04-2007, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •