Results 1 to 16 of 16
  1. #1
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Need Help With Program!!!!!!!!!!

    I need to make the dvd's go into alphabetical order. I think there is a much easier way to go about this I just don't know if I am doing this write. I have to use only algorithmic search. The three class are Movies, Dvd, and DvdCollections. I get it so that it sorts the Dvd but then it doesn't output the dvds.

    Code:
    Java Code:
    public class Movies
    {
    
    public static void main (String[] args)
       {
          DVDCollection movies = new DVDCollection();
          
          movies.addDVD ("The Godfather", "Francis Ford Coppala", 1972, 24.95, true);
          movies.addDVD ("District 9", "Neill Blomkamp", 2009, 19.95, false);
          movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
          movies.addDVD ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
          movies.addDVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
    
          System.out.println (movies);
    
          movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
          movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
    
          System.out.println (movies);
      
       }
     
    }
    
    ======================================================================================
    
    import java.text.NumberFormat;
    
    public class DVDCollection  {
    	private DVD[] collection;
    	private String[] collectiontemp;
    	private double[] revenuetemp;
    	private static int count;
    	private double totalCost;
    
    	// -----------------------------------------------------------------
    	// Constructor: Creates an initially empty collection.
    	// -----------------------------------------------------------------
    	public DVDCollection() {
    		collection = new DVD[100];
    		count = 0;
    		totalCost = 0.0;
    		// BubbleSort(collection, totalCost);
    
    	}
    
    	public void addDVD(String title, String director, int year, double cost,
    			boolean bluRay) {
    		if (count == collection.length)
    			increaseSize();
    
    		collection[count] = new DVD(title, director, year, cost, bluRay);
    		totalCost += cost;
    		count++;
    	}
    
    	public static int binarySearch(String[] collection, String key) {
    		int first = 0;
    		int last = collection.length;
    
    		while (first < last) {
    			int mid = (first + last) / 2; // Compute mid point.
    			if (key.compareTo(collection[mid]) < 0) {
    				last = mid; // repeat search in bottom half.
    			} else if (key.compareTo(collection[mid]) > 0) {
    				first = mid + 1; // Repeat search in top half.
    			} else {
    				return mid; // Found it. return position
    			}
    		}
    		return -(first + 1); // Failed to find key, comment:: same as returning
    								// "-1" as a value
    	}
    
    	public String toString() {
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    
    		String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    		report += "My DVD Collection\n\n";
    		report += "Number of DVDs: " + count + "\n";
    		report += "Total cost: " + fmt.format(totalCost) + "\n";
    		report += "Average cost: " + fmt.format(totalCost / count);
    
    		report += "\n\nDVD List:\n\n";
    			
    		createArray();
    		BubbleSort(collectiontemp,revenuetemp);
    		
    		for (int dvd = 0; dvd < count; dvd++)
    			report += collection[dvd].toString() + "\n";
    
    		return report;
    	}
    
    	private void increaseSize() {
    		DVD[] temp = new DVD[collection.length * 2];
    		System.out.println("IncreaseSize function hit");
    		
    		for (int dvd = 0; dvd < collection.length; dvd++)
    			temp[dvd] = collection[dvd];
    
    		collection = temp;
    	}
    	
    	public void createArray() {
    		//DVD[] temp = new DVD[collection.length * 2];
    		String[] tempCollection = new String[count];
    		double[] tempRevenue = new double[count];
    		
    		for (int dvd = 0; dvd < count; dvd++){
    			
    			
    			//System.out.println("currently at "+ dvd + " of "+ count);
    			tempCollection[dvd] = collection[dvd].getTitle();
    			//System.out.println(collection[dvd].getTitle());
    			tempRevenue[dvd] = collection[dvd].getRevenue();
    			
    		}
    		//collectiontemp = temp;
    		collectiontemp = tempCollection;
    		revenuetemp = tempRevenue;
    		
    	}
    
    	private static void BubbleSort(String[] collection, double[] totalCost) {
    		String temp;
    		double total;
    		int count2;
    		count2 = count;
    		// Begin For loop
    		for (int dvd = 0; dvd < count2; dvd++) {
    			// Open For loop
    			
    			
    			for (int j = 0; j < count2 - 1 - dvd; j++) {
    				
    				System.out.println(collection[j] +" | "+ collection[j+1]);
    				
    				// Open If Statement Compare and sort strings
    				if (collection[j].compareTo(collection[j + 1]) > 0) {
    					temp = collection[j];
    					total = totalCost[j];
    					collection[j] = collection[j + 1];
    					collection[j + 1] = temp;
    					totalCost[j] = totalCost[j + 1];
    					totalCost[j + 1] = total;
    
    					// start Binary Search from here
    
    				}// End of If Statement
    			}// End of For Loop
    		}// End of For Loop
    	}//
    
    }
    =============================================================================================
    
    import java.text.NumberFormat;
    
    public class DVD {
    	private String title, director;
    	private int year;
    	private double cost;
    	private boolean bluRay;
    
    	// -----------------------------------------------------------------
    	// Creates a new DVD with the specified information.
    	// -----------------------------------------------------------------
    	public DVD(String title, String director, int year, double cost,
    			boolean bluRay) {
    		this.title = title;
    		this.director = director;
    		this.year = year;
    		this.cost = cost;
    		this.bluRay = bluRay;
    	}
    
    	public String toString() {
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    
    		String description;
    
    		description = fmt.format(cost) + "\t" + year + "\t";
    		description += title + "\t" + director;
    
    		if (bluRay)
    			description += "\t" + "Blu-Ray";
    
    		return description;
    	}
    
    	public int compareTo(DVD dvd) {
    		// TODO Auto-generated method stub
    		return 0;
    
    	}
    
    	public String getTitle() {
    		return title;
    	}
    	
    	public void setTitle(String strTitle){
    		
    		title = strTitle;
    	}
    	
    	public double getRevenue() {
    		return cost;
    	}
    	
    }
    I think there is a much easier way, and suggestions would be great.
    Last edited by Norm; 03-01-2012 at 04:11 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    it doesn't output the dvds.
    Are you trying to print a list of the DVDs on the console?
    What does the program output when you execute it?

  3. #3
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    Right now the DVD show up in the console out of alphabetical order. I need to make the DVDs show up alphabetically in the console when added to the DVD collection

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    Then you should sort the collection before printing it. I see a method call BubbleSort(). What does that do?

    Can you explain what the problem with the program is?

  5. #5
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    bubble sort is putting collection array into a temp so it can sort the dvd titles

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    putting collection array into a temp so it can sort the dvd titles
    Is the copy in the temp sorted ok?
    Why not use the contents of the temp to "make the DVDs show up alphabetically"

  7. #7
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    i dont know how exactly how to go about that. :/ I know how to put them in alphabetical order if all the contents were in one class but since there are 3 classes its hard for me to understand how to make them communicate with each other. Right now when you run the code it shows that it is sorting the dvd's correctly but then it shows the collections out of order still.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    all the contents were in one class but since there are 3 classes
    Are you saying that the objects that you want to sort are not in one place?
    What are the objects you want to sort and where are they located?

    it shows the collections out of order still.
    You sort the temp collection and the print the other collection.
    Why don't you print the collection that is sorted?
    Why do you use a temp? Why not sort the original collection?

  9. #9
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    Dvd has parameters that requires String title, String director, int year, double cost,boolean bluRay, then dvdcollection is making an array of dvds. Then dvd is being add in Movies which is the main method.

  10. #10
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    I thought by putting it into a temp it will allow me to sort them. I dont really know how to sort any other way.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    What if you sort the original and leave out the temp?

  12. #12
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    So your suggestion not to use the bubble sort? Cause right now it is sorting the titles i just dont know how to make the console show all the other parameters of dvd along with the title to be in alphabetical order.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    No, I'm suggesting that you use the bubbleSort with a slight change.
    Look at the method that the bubbleSort method uses to compare two elements in the list.
    What if the DVD class had that method? Then the sort would work as you have written it by changing the parameter passed to the method to be an array of DVD objects.

    Now think what the compareTo method would do. If you want to sort on the name, a String, then it could use the String class's compareTo method to do the comparison and return what that method returns.
    The compareTo method would be passed an instance of the DVD class which has a method to get to its name.

  14. #14
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    So by just putting the compareTo method in dvd that should help?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Need Help With Program!!!!!!!!!!

    That should allow the current bubbleSort method to work with the change to the parameters and to the temp being a DVD instead of a String.

  16. #16
    Rodojz is offline Member
    Join Date
    Mar 2012
    Posts
    9
    Rep Power
    0

    Default Re: Need Help With Program!!!!!!!!!!

    I'm not that familiar with compareTo method but would I compare what is being sorted??

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 07:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 07:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 10:33 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
  •