Results 1 to 16 of 16
Thread: Need Help With Program!!!!!!!!!!
- 03-01-2012, 02:50 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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:I think there is a much easier way, and suggestions would be great.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; } }Last edited by Norm; 03-01-2012 at 03:11 AM. Reason: added code tags
- 03-01-2012, 03:12 AM #2
Re: Need Help With Program!!!!!!!!!!
Are you trying to print a list of the DVDs on the console?it doesn't output the dvds.
What does the program output when you execute it?
- 03-01-2012, 03:36 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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
- 03-01-2012, 03:39 AM #4
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?
- 03-01-2012, 02:04 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
Re: Need Help With Program!!!!!!!!!!
bubble sort is putting collection array into a temp so it can sort the dvd titles
- 03-01-2012, 02:36 PM #6
Re: Need Help With Program!!!!!!!!!!
Is the copy in the temp sorted ok?putting collection array into a temp so it can sort the dvd titles
Why not use the contents of the temp to "make the DVDs show up alphabetically"
- 03-01-2012, 05:14 PM #7
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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.
- 03-01-2012, 05:36 PM #8
Re: Need Help With Program!!!!!!!!!!
Are you saying that the objects that you want to sort are not in one place?all the contents were in one class but since there are 3 classes
What are the objects you want to sort and where are they located?
You sort the temp collection and the print the other collection.it shows the collections out of order still.
Why don't you print the collection that is sorted?
Why do you use a temp? Why not sort the original collection?
- 03-01-2012, 06:34 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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.
- 03-01-2012, 06:36 PM #10
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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.
- 03-01-2012, 06:43 PM #11
Re: Need Help With Program!!!!!!!!!!
What if you sort the original and leave out the temp?
- 03-01-2012, 08:03 PM #12
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
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.
- 03-01-2012, 08:11 PM #13
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.
- 03-01-2012, 08:44 PM #14
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
Re: Need Help With Program!!!!!!!!!!
So by just putting the compareTo method in dvd that should help?
- 03-01-2012, 08:55 PM #15
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.
- 03-01-2012, 10:06 PM #16
Member
- Join Date
- Mar 2012
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks