Need help with Class Project!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JenniferT
    New Member
    • Jan 2007
    • 15

    Need help with Class Project!

    OK, so I'm very new to Java programming and I've been able to squeek by so far, but I'm completely stuck on this assignment. Here is the assignment that is due this week -
    • Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

    • Create a method to calculate the value of the entire inventory.

    • Create another method to sort the array items by the name of the product. DON'T use array.sort.

    Compile and run the Java program.

    And here is my code from last week -

    Code:
    //Inventory Program Part 1
    
    
    class Inventory
    
    {
    	private String Name; //stores DVD name
    	private long itemNumber; //stores item number
    	private long stockQuantity; //stores quanity in stock
    	private double dvdPrice; //stores DVD price
    	public Inventory ()
    
    	{
    		Name = "";
    		itemNumber = 0L;
    		stockQuantity = 0L;
    		dvdPrice = 0.0;
    		}
    		   public Inventory (String Name, long itemNumber, long stockQuantity, double dvdPrice)
    			  {
    				 this.Name = Name;
    				 this.itemNumber = itemNumber;
    				 this.stockQuantity = stockQuantity;
    				 this.dvdPrice = dvdPrice;
    		   }
    
    
    		  public void setItemName(String Name)  //Method to set and get the item name
    		   {
    			  this.Name = Name;
    		   }
    		   public String getItemName()
    		   {
    			  return Name;
    		   }
    
    
    		   public void setItemNumber(long itemNumber)  //Method to set and get the item number
    		   {
    			  itemNumber = itemNumber;
    		   }
    		   public long getItemNumber()
    		   {
    			  return itemNumber;
    		   }
    
    
    		   public void setStockQuantity(long quantity)  //Method to set and get the quantity in stock
    		   {
    			  stockQuantity = stockQuantity;
    		   }
    		   public long getStockQuantity()
    		   {
    			  return stockQuantity;
    		   }
    
    
    		   public void setItemPrice(double dvdPrice)  //Method to set and get the item price
    		   {
    			  this.dvdPrice = dvdPrice;
    		   }
    		   public double getItemPrice()
    		   {
    			  return dvdPrice;
    		   }
    
    
    		   public double calculateInventoryValue()  //Method to calculate the value of the in stock inventory
    		   {
    			  return dvdPrice * stockQuantity;
    		   }
    
    		}//end class Inventory
    
    
    		public class Inventory1
    
    
    		{
    
    		   public static void main( String args[])
    		   {
    
    			Inventory p = new Inventory("24: Season 4", 1, 10, 24.99); //Lists 1st DVD inventory information
    			System.out.println();
            	System.out.print( "Inventory of DVD Movies: " ); //display title
            	System.out.printf("\n\nDVD Title:         %s\n",p.getItemName()); //display DVD title
    			System.out.printf("Item Number:       %s\n",p.getItemNumber()); //display item number
    			System.out.printf("Quantity in Stock: %s\n",p.getStockQuantity()); //display quantity in stock
    			System.out.printf("Item Price:        $%.2f\n",p.getItemPrice()); //display DVD price
    			System.out.printf("Inventory Value:   $%.2f\n",p.calculateInventoryValue()); //display total value of inventory for item
    			System.out.println();
    			System.out.println();
    
    		   }
    }//end class Inventory1
    Can somebody please help me get to the next step!! Many thanks in advance!! :)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by JenniferT
    OK, so I'm very new to Java programming and I've been able to squeek by so far, but I'm completely stuck on this assignment. Here is the assignment that is due this week -
    • Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.

    • Create a method to calculate the value of the entire inventory.

    • Create another method to sort the array items by the name of the product. DON'T use array.sort.

    Compile and run the Java program.

    And here is my code from last week -

    //Inventory Program Part 1


    class Inventory

    {
    private String Name; //stores DVD name
    private long itemNumber; //stores item number
    private long stockQuantity; //stores quanity in stock
    private double dvdPrice; //stores DVD price
    public Inventory ()

    {
    Name = "";
    itemNumber = 0L;
    stockQuantity = 0L;
    dvdPrice = 0.0;
    }
    public Inventory (String Name, long itemNumber, long stockQuantity, double dvdPrice)
    {
    this.Name = Name;
    this.itemNumber = itemNumber;
    this.stockQuant ity = stockQuantity;
    this.dvdPrice = dvdPrice;
    }


    public void setItemName(Str ing Name) //Method to set and get the item name
    {
    this.Name = Name;
    }
    public String getItemName()
    {
    return Name;
    }


    public void setItemNumber(l ong itemNumber) //Method to set and get the item number
    {
    itemNumber = itemNumber;
    }
    public long getItemNumber()
    {
    return itemNumber;
    }


    public void setStockQuantit y(long quantity) //Method to set and get the quantity in stock
    {
    stockQuantity = stockQuantity;
    }
    public long getStockQuantit y()
    {
    return stockQuantity;
    }


    public void setItemPrice(do uble dvdPrice) //Method to set and get the item price
    {
    this.dvdPrice = dvdPrice;
    }
    public double getItemPrice()
    {
    return dvdPrice;
    }


    public double calculateInvent oryValue() //Method to calculate the value of the in stock inventory
    {
    return dvdPrice * stockQuantity;
    }

    }//end class Inventory


    public class Inventory1


    {

    public static void main( String args[])
    {

    Inventory p = new Inventory("24: Season 4", 1, 10, 24.99); //Lists 1st DVD inventory information
    System.out.prin tln();
    System.out.prin t( "Inventory of DVD Movies: " ); //display title
    System.out.prin tf("\n\nDVD Title: %s\n",p.getItem Name()); //display DVD title
    System.out.prin tf("Item Number: %s\n",p.getItem Number()); //display item number
    System.out.prin tf("Quantity in Stock: %s\n",p.getStoc kQuantity()); //display quantity in stock
    System.out.prin tf("Item Price: $%.2f\n",p.getI temPrice()); //display DVD price
    System.out.prin tf("Inventory Value: $%.2f\n",p.calc ulateInventoryV alue()); //display total value of inventory for item
    System.out.prin tln();
    System.out.prin tln();

    }
    }//end class Inventory1


    Can somebody please help me get to the next step!! Many thanks in advance!! :)
    Hi next time you post code please remember to use code tags. Now for your problem please read this and post if you still need any help.

    Comment

    • JenniferT
      New Member
      • Jan 2007
      • 15

      #3
      Originally posted by r035198x
      Hi next time you post code please remember to use code tags. Now for your problem please read this and post if you still need any help.

      Yes, I read through that whole post and it did not help much as it's somewhat jumbled. That is the same project I'm working on though. Their part3 is close to what I need, but not quite there.

      Can you please explain code tags?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JenniferT
        Yes, I read through that whole post and it did not help much as it's somewhat jumbled. That is the same project I'm working on though. Their part3 is close to what I need, but not quite there.

        Can you please explain code tags?
        I've just added them to your post there. Every time you make a post and want to include code then just make sure to wrap the code around code tags

        Comment

        • JenniferT
          New Member
          • Jan 2007
          • 15

          #5
          Originally posted by r035198x
          I've just added them to your post there. Every time you make a post and want to include code then just make sure to wrap the code around code tags
          Thanks, I just read the code tags rule to the right! Shows you how observant I am!

          Comment

          • JenniferT
            New Member
            • Jan 2007
            • 15

            #6
            Originally posted by JenniferT
            Thanks, I just read the code tags rule to the right! Shows you how observant I am!
            OK, I've been playing around with some of the code and I'm making progress. Here's what I have now. I would like to display the total amount at the bottom instead of the top and display the list as is then display as sorted. I also can't use the array.sort so I need to sort another way.

            I would also like to display the DVD's in a uniform line rather than 1 space after the field:. For example:
            DVD Name:___DVD1
            Item:________1
            Cost:________10 .99
            And is there a way to change the field names on the printout? I was able to more easily do this in part1.

            Any ideas?


            Code:
             
            //Inventory2.java
            
            
            import java.util.*;
            class Product implements Comparable
            
            {
               private String name; 	// class variable that stores the item name
               private long number;	  // class variable that stores the item number
               private long stockQuantity;   // class variable that stores the quantity in stock
               private double price;	  // class variable that stores the item price
            
               public Product() // Constructor for the Product class
               {
            	  name = "";
            	  number = 0L;
            	  stockQuantity = 0L;
            	  price = 0.0;
               }
               public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
            	  {
                  this.name = name;
            	  this.number = number;
            	  this.stockQuantity = stockQuantity;
            	  this.price = price;
               	  }
            
               public void setItemName(String name)  // Method to set the item name
               {
            	  this.name = name;
               }
               public String getItemName()  // Method to get the item name
               {
            	  return name;
               }
            
            
               public void setItemNumber(long number)  // Method to set the item number
               {
            	  this.number = number;
               }
               public long getItemNumber()  // Method to get the item number
               {
            	  return number;
               }
            
            
               public void setStockQuantity(long quantity)  // Method to set the quantity in stock
               {
            	  stockQuantity = quantity;
               }
               public long getStockQuantity()  // Method to get the quantity in stock
               {
            	  return stockQuantity;
               }
            
            
               public void setItemPrice(double price)  // Method to set the item price
               {
            	  this.price = price;
               }
               public double getItemPrice()  // Method to get the item price
               {
            	  return price;
               }
            
            
               public double calculateInventoryValue()  // Method to calculate the value of the inventory
               {
            	  return price * stockQuantity;
               }
               public int compareTo (Object o)
               {
            
            
               Product p = (Product)o;
            	  return name.compareTo(p.getItemName());
               }
            
               public String toString()
               {
            	  return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
               }
            
            }//end class Product
            
            public class Inventory2
            {
               // main methods begins execution of java application
               public static void main( String args[])
               {
            
               Product[] supplies = new Product[3];
            
               Product p1 = new Product("DVD1", 1, 11, 10.99);
               Product p2 = new Product("DVD2", 2, 22, 20.99);
               Product p3 = new Product("DVD3", 3, 33, 30.99);
            
               supplies[0] = p1;
               supplies[1] = p2;
               supplies[2] = p3;
            
               double total = 0.0;
            
               for(int i= 0; i < 3;i++)
               {
               total = total + supplies[i].calculateInventoryValue();
               }
            
              System.out.println("Total Value is: $"+total);
            
              Arrays.sort(supplies);
            
                for(Product p: supplies)
                {
                System.out.println(p);
                System.out.println();
                }
            
               } // end main method
            }//end class Inventory2

            Comment

            • JenniferT
              New Member
              • Jan 2007
              • 15

              #7
              OK, I'm making some progress. I still need to sort without using array.sort. I tried a bubble sort which I kept in there, but it wouldn't work for me. And I would still like to know how to allign the names better.
              Here is what I just came up with:

              Code:
              //Inventory2.java
              
              
              import java.util.*;
              class Product implements Comparable
              
              {
                 private String name; 	// class variable that stores the item name
                 private long number;	  // class variable that stores the item number
                 private long stockQuantity;   // class variable that stores the quantity in stock
                 private double price;	  // class variable that stores the item price
              
                 public Product() // Constructor for the Product class
                 {
              	  name = "";
              	  number = 0L;
              	  stockQuantity = 0L;
              	  price = 0.0;
                 }
                 public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
              	  {
                    this.name = name;
              	  this.number = number;
              	  this.stockQuantity = stockQuantity;
              	  this.price = price;
                 	  }
              
                 public void setItemName(String name)  // Method to set the item name
                 {
              	  this.name = name;
                 }
                 public String getItemName()  // Method to get the item name
                 {
              	  return name;
                 }
              
              
                 public void setItemNumber(long number)  // Method to set the item number
                 {
              	  this.number = number;
                 }
                 public long getItemNumber()  // Method to get the item number
                 {
              	  return number;
                 }
              
              
                 public void setStockQuantity(long quantity)  // Method to set the quantity in stock
                 {
              	  stockQuantity = quantity;
                 }
                 public long getStockQuantity()  // Method to get the quantity in stock
                 {
              	  return stockQuantity;
                 }
              
              
                 public void setItemPrice(double price)  // Method to set the item price
                 {
              	  this.price = price;
                 }
                 public double getItemPrice()  // Method to get the item price
                 {
              	  return price;
                 }
              
              
                 public double calculateInventoryValue()  // Method to calculate the value of the inventory
                 {
              	  return price * stockQuantity;
                 }
                 public int compareTo (Object o)
                 {
              
              
                 Product p = (Product)o;
              	  return name.compareTo(p.getItemName());
                 }
              
                 public String toString()
                 {
              	  return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
                 }
              
              }//end class Product
              
              public class Inventory2
              {
                 // main methods begins execution of java application
                 public static void main( String args[])
                 {
              
                 Product[] supplies = new Product[3];
              
                 Product p1 = new Product("cDVD3", 1, 11, 10.99);
                 Product p2 = new Product("aDVD2", 2, 22, 20.99);
                 Product p3 = new Product("bDVD1", 3, 33, 30.99);
              
                 supplies[0] = p1;
                 supplies[1] = p2;
                 supplies[2] = p3;
              
               //  product temp[] supplies = new product[1];
              
                 double total = 0.0;
              
                 for(int i= 0; i < 3;i++)
                 {
                 total = total + supplies[i].calculateInventoryValue();
                 }
              
              
              
                    for(Product p: supplies)
                    {
                    System.out.println(p);
                    System.out.println();
                  }
              
               Arrays.sort(supplies);
              //		sorting the array using Bubble Sort
              	//	for(int j = 0; j < supplies.length - 1; j++)
              	//	{
              //
              	//	    for(int k = 0; k < supplies.length - 1; k++)
              	//	    {
              //
              	//	        if(supplies[k].getItemName().compareToIgnoreCase(supplies[k+1].getItemName()) > 0)
              	//	        {
              //
              	//	            temp[0] = supplies[k];
              	//	            supplies[k] = supplies[k+1];
              	//	            supplies[k+1] = temp[0];
              //
              	//	        }//end if
              //
              	//	    }//end for loop
              //
              	//	}//end for loop
              
              
              
              
              
                  for(Product p: supplies)
                  {
                  System.out.println(p);
                  System.out.println();
                  }
              
              
                 System.out.println("Total Value is: $"+total);
              
              
                 } // end main method
              }//end class Inventory2

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                Are you posting this because the sort doesn't work?
                Does it alter the order at all?

                Not sure why you have 2 loops around the sort....

                Comment

                • DeMan
                  Top Contributor
                  • Nov 2006
                  • 1799

                  #9
                  You will need two loops (my mistake) but in your code one of them does nothing.
                  Try Something like this (which I think is cliose to yours, so you were on the right track):

                  Code:
                  swapped = false;
                  do
                  {
                      swapped = false;
                      for(int i=0; i<supplies.length; i++)
                      {
                        if(supplies[i].getItemName().compareToIgnoreCase( supplies[i+1].getItemName()) >0)
                        {
                          temp[0] = supplies[k];
                          supplies[k] = supplies[k+1];
                          supplies[k+1] = temp[0];
                          swapped = true;
                        }//end if
                      }//end for loop
                  } while(swapped == false);

                  Comment

                  • JenniferT
                    New Member
                    • Jan 2007
                    • 15

                    #10
                    Yes, the array.sort does work fine, but for some reason we are not allowed to use that method to sort.

                    I get a bunch of errors when I insert your code. Here's what I get:
                    C:\Inventory2.j ava:142: cannot find symbol
                    symbol : variable swapped
                    location: class Inventory2
                    swapped = false;
                    ^
                    C:\Inventory2.j ava:145: cannot find symbol
                    symbol : variable swapped
                    location: class Inventory2
                    swapped = false;
                    ^
                    C:\Inventory2.j ava:150: cannot find symbol
                    symbol : variable temp
                    location: class Inventory2
                    temp[0] = supplies[k];
                    ^
                    C:\Inventory2.j ava:150: cannot find symbol
                    symbol : variable k
                    location: class Inventory2
                    temp[0] = supplies[k];
                    ^
                    C:\Inventory2.j ava:151: cannot find symbol
                    symbol : variable k
                    location: class Inventory2
                    supplies[k] = supplies[k+1];
                    ^
                    C:\Inventory2.j ava:151: cannot find symbol
                    symbol : variable k
                    location: class Inventory2
                    supplies[k] = supplies[k+1];
                    ^
                    C:\Inventory2.j ava:151: incompatible types
                    found : <nulltype>
                    required: int
                    supplies[k] = supplies[k+1];
                    ^
                    C:\Inventory2.j ava:152: cannot find symbol
                    symbol : variable k
                    location: class Inventory2
                    supplies[k+1] = temp[0];
                    ^
                    C:\Inventory2.j ava:152: incompatible types
                    found : <nulltype>
                    required: int
                    supplies[k+1] = temp[0];
                    ^
                    C:\Inventory2.j ava:152: cannot find symbol
                    symbol : variable temp
                    location: class Inventory2
                    supplies[k+1] = temp[0];
                    ^
                    C:\Inventory2.j ava:153: cannot find symbol
                    symbol : variable swapped
                    location: class Inventory2
                    swapped = true;
                    ^
                    C:\Inventory2.j ava:156: cannot find symbol
                    symbol : variable swapped
                    location: class Inventory2
                    } while(swapped == false);
                    ^
                    12 errors

                    Tool completed with exit code 1

                    And here's the code I was using if you want to compile it yourself:

                    Code:
                    //Inventory2.java
                    
                    
                    import java.util.*;
                    class Product implements Comparable
                    
                    {
                       private String name; 	// class variable that stores the item name
                       private long number;	  // class variable that stores the item number
                       private long stockQuantity;   // class variable that stores the quantity in stock
                       private double price;	  // class variable that stores the item price
                    
                       public Product() // Constructor for the Product class
                       {
                    	  name = "";
                    	  number = 0L;
                    	  stockQuantity = 0L;
                    	  price = 0.0;
                       }
                       public Product(String name, long number, long stockQuantity, double price) // Constructor for the Supplies class
                    	  {
                          this.name = name;
                    	  this.number = number;
                    	  this.stockQuantity = stockQuantity;
                    	  this.price = price;
                       	  }
                    
                       public void setItemName(String name)  // Method to set the item name
                       {
                    	  this.name = name;
                       }
                       public String getItemName()  // Method to get the item name
                       {
                    	  return name;
                       }
                    
                    
                       public void setItemNumber(long number)  // Method to set the item number
                       {
                    	  this.number = number;
                       }
                       public long getItemNumber()  // Method to get the item number
                       {
                    	  return number;
                       }
                    
                    
                       public void setStockQuantity(long quantity)  // Method to set the quantity in stock
                       {
                    	  stockQuantity = quantity;
                       }
                       public long getStockQuantity()  // Method to get the quantity in stock
                       {
                    	  return stockQuantity;
                       }
                    
                    
                       public void setItemPrice(double price)  // Method to set the item price
                       {
                    	  this.price = price;
                       }
                       public double getItemPrice()  // Method to get the item price
                       {
                    	  return price;
                       }
                    
                    
                       public double calculateInventoryValue()  // Method to calculate the value of the inventory
                       {
                    	  return price * stockQuantity;
                       }
                       public int compareTo (Object o)
                       {
                    
                    
                       Product p = (Product)o;
                    	  return name.compareTo(p.getItemName());
                       }
                    
                       public String toString()
                       {
                    	  return "DVD Title: "+name + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
                       }
                    
                    }//end class Product
                    
                    public class Inventory2
                    {
                       // main methods begins execution of java application
                       public static void main( String args[])
                       {
                    
                       Product[] supplies = new Product[3];
                    
                       Product p1 = new Product("cDVD3", 1, 11, 10.99);
                       Product p2 = new Product("aDVD2", 2, 22, 20.99);
                       Product p3 = new Product("bDVD1", 3, 33, 30.99);
                    
                       supplies[0] = p1;
                       supplies[1] = p2;
                       supplies[2] = p3;
                    
                     //  product temp[] supplies = new product[1];
                    
                       double total = 0.0;
                    
                       for(int i= 0; i < 3;i++)
                       {
                       total = total + supplies[i].calculateInventoryValue();
                       }
                    
                    
                    
                          for(Product p: supplies)
                          {
                          System.out.println(p);
                          System.out.println();
                        }
                    //OLD CODE-------------------------------------
                     //Arrays.sort(supplies);
                    //		sorting the array using Bubble Sort
                    	//	for(int j = 0; j < supplies.length - 1; j++)
                    	//	{
                    //
                    	//	    for(int k = 0; k < supplies.length - 1; k++)
                    	//	    {
                    //
                    	//	        if(supplies[k].getItemName().compareToIgnoreCase(supplies[k+1].getItemName()) > 0)
                    	//	        {
                    //
                    	//	            temp[0] = supplies[k];
                    	//	            supplies[k] = supplies[k+1];
                    	//	            supplies[k+1] = temp[0];
                    //
                    	//	        }//end if
                    //
                    	//	    }//end for loop
                    //
                    	//	}//end for loop
                    //END OLD CODE------------------------------------
                    
                    	swapped = false;
                    	do
                    	{
                    	    swapped = false;
                    	    for(int i=0; i<supplies.length; i++)
                    	    {
                    	      if(supplies[i].getItemName().compareToIgnoreCase( supplies[i+1].getItemName()) >0)
                    	      {
                    	        temp[0] = supplies[k];
                    	        supplies[k] = supplies[k+1];
                    	        supplies[k+1] = temp[0];
                    	        swapped = true;
                    	      }//end if
                    	    }//end for loop
                    } while(swapped == false);
                    
                    
                    
                    
                    
                        for(Product p: supplies)
                        {
                        System.out.println(p);
                        System.out.println();
                        }
                    
                    
                       System.out.println("Total Value is: $"+total);
                    
                    
                       } // end main method
                    }//end class Inventory2

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Do the sorting in a seperate method. Here is an example program that you should go through and make sure you understand before proceeding

                      Code:
                      import java.util.*;
                      class Church {
                      	private String name;
                      	private String pastor;
                      	public Church(String name, String pastor) {
                      		this.name = name;
                      		this.pastor = pastor;
                      	}
                      	public String getPastor() {
                      		return pastor;
                      	}
                      	public String getName() {
                      		return name;
                      	}
                      	public void setPastor(String pastor) {
                      		this.pastor = pastor;
                      	}
                      	public String toString() {
                      		return getName() + " is Pastored by "+getPastor();
                      	}
                      	public int compareByPastor(Church c) {
                      		int x = pastor.compareTo(c.getPastor());
                      		return x;
                      	}
                      	public int compareByName(Church c) {
                      		int x = name.compareTo(c.getName());
                      		return x;
                      	}
                      }
                      
                      class Churches {
                      	private final List<Church> churches;
                      
                      	public Churches() {
                      		churches = new ArrayList<Church>();
                      	}
                      	public void addWithoutSorting(Church c) {
                      		churches.add(c);
                      	}
                      
                      	//You could always add using this method
                      	public void addWithSorting(Church c) {
                      
                      	}
                      	public void display() {
                      		for(int j = 0; j < churches.size(); j++) {
                      			System.out.print(churches.get(j).toString());
                      			System.out.println("");
                      		}
                         }
                         public List<Church> getChurches() {
                      	   return churches;
                         }
                         public void sortBy(String s) {
                      	   for (int i = 1; i < churches.size(); i++) {
                      		   int j;
                      		   Church val = churches.get(i);
                                 for (j = i-1; j > -1; j--) {
                      			   Church temp = churches.get(j);
                      			   if(s.equals("Pastor")) {
                      				   if (temp.compareByPastor(val) <= 0) {
                      					   break;
                      				   }
                      			   }
                      			   else if(s.equals("Name")) {
                      				   if (temp.compareByName(val) <= 0) {
                      				   	   break;
                      				   }
                      			   }
                      			   churches.set(j+1, temp);
                      			}
                      			churches.set(j+1, val);
                             }
                           }
                      
                          public static void main(String[] args) {
                      		Churches baptists = new Churches();
                      	    baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
                      	    baptists.addWithoutSorting(new Church("New Life", "Tudor"));
                      	    baptists.addWithoutSorting(new Church("My Church", "r035198x"));
                      	    baptists.addWithoutSorting(new Church("AFM", "Cathy"));
                      	    System.out.println("**********************Before Sorting***********************");
                      	    baptists.display();
                      	    baptists.sortBy("Pastor");
                      	    System.out.println("**********************After sorting by Pastor**************");
                      	    baptists.display();
                      	    baptists.sortBy("Name");
                      	    System.out.println("**********************After sorting by Name****************");
                      	    baptists.display();
                      
                      	}
                      
                        }

                      Comment

                      • DeMan
                        Top Contributor
                        • Nov 2006
                        • 1799

                        #12
                        Originally posted by "JenniferT"
                        Yes, the array.sort does work fine, but for some reason we are not allowed to use that method to sort.

                        I get a bunch of errors when I insert your code. Here's what I get:
                        C:\Inventory2.j ava:142: cannot find symbol
                        symbol : variable swapped
                        location: class Inventory2
                        swapped = false;
                        My code isn't meant to fit directly into your code, it was an ide of the logic flow that you need..... in particular 'swapped' hasn't been declared which is why the program can't find it - I hope to have described the method so that you can implement it.......

                        Comment

                        • JenniferT
                          New Member
                          • Jan 2007
                          • 15

                          #13
                          That doesn't really help me much! I'm not sure I understand the example though and the code doesn't compile so I can't see how the program works. I'm also not sure why my original sort doesn't work. What's wrong with the code?

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by JenniferT
                            That doesn't really help me much! I'm not sure I understand the example though and the code doesn't compile so I can't see how the program works. I'm also not sure why my original sort doesn't work. What's wrong with the code?
                            If you make an effort to understand the program I posted then you will be able to get your sort to work easily yourself. What errors are you getting with it?
                            Which version of jdk are you using?

                            If you are using a version earlier than 1.5 then use this

                            Code:
                            import java.util.*;
                            class Church {
                            	private String name;
                            	private String pastor;
                            	public Church(String name, String pastor) {
                            		this.name = name;
                            		this.pastor = pastor;
                            	}
                            	public String getPastor() {
                            		return pastor;
                            	}
                            	public String getName() {
                            		return name;
                            	}
                            	public void setPastor(String pastor) {
                            		this.pastor = pastor;
                            	}
                            	public String toString() {
                            		return getName() + " is Pastored by "+getPastor();
                            	}
                            	public int compareByPastor(Church c) {
                            		int x = pastor.compareTo(c.getPastor());
                            		return x;
                            	}
                            	public int compareByName(Church c) {
                            		int x = name.compareTo(c.getName());
                            		return x;
                            	}
                            }
                            
                            class Churches2 {
                            	private final List churches;
                            
                            	public Churches2() {
                            		churches = new ArrayList<Church>();
                            	}
                            	public void addWithoutSorting(Church c) {
                            		churches.add(c);
                            	}
                            
                            	//You could always add using this method
                            	public void addWithSorting(Church c) {
                            
                            	}
                            	public void display() {
                            		for(int j = 0; j < churches.size(); j++) {
                            			System.out.print(churches.get(j).toString());
                            			System.out.println("");
                            		}
                               }
                               public List getChurches() {
                            	   return churches;
                               }
                               public void sortBy(String s) {
                            	   for (int i = 1; i < churches.size(); i++) {
                            		   int j;
                            		   Church val = (Church)churches.get(i);
                                       for (j = i-1; j > -1; j--) {
                            			   Church temp = (Church)churches.get(j);
                            			   if(s.equals("Pastor")) {
                            				   if (temp.compareByPastor(val) <= 0) {
                            					   break;
                            				   }
                            			   }
                            			   else if(s.equals("Name")) {
                            				   if (temp.compareByName(val) <= 0) {
                            				   	   break;
                            				   }
                            			   }
                            			   churches.set(j+1, temp);
                            			}
                            			churches.set(j+1, val);
                                   }
                                 }
                            
                                public static void main(String[] args) {
                            		Churches baptists = new Churches();
                            	    baptists.addWithoutSorting(new Church("Pac", "Pastor G"));
                            	    baptists.addWithoutSorting(new Church("New Life", "Tudor"));
                            	    baptists.addWithoutSorting(new Church("My Church", "r035198x"));
                            	    baptists.addWithoutSorting(new Church("AFM", "Cathy"));
                            	    System.out.println("**********************Before Sorting***********************");
                            	    baptists.display();
                            	    baptists.sortBy("Pastor");
                            	    System.out.println("**********************After sorting by Pastor**************");
                            	    baptists.display();
                            	    baptists.sortBy("Name");
                            	    System.out.println("**********************After sorting by Name****************");
                            	    baptists.display();
                            
                            	}
                            
                              }
                            You should use at least jdk1.5 these days

                            Comment

                            • JenniferT
                              New Member
                              • Jan 2007
                              • 15

                              #15
                              I am using 1.5 and I just realized what I did wrong (duh). I saved the file as Church.java instead of Churches.java. It still compiled, but I got a run error when it ran. Churches run fine and I see how you are sorting it. I will try to apply this to my program and will post on here if I can't get it.

                              I'm sure I'll have more question with the later assignments so I will post on here in the future if that's OK. Or I can create a new post. Whatever is best.

                              Comment

                              Working...