Results 1 to 7 of 7
- 02-27-2012, 08:55 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Keeping objects sorted in an array.
Hi my assignment is to modify this application so it keeps objects sorted by title at all times.
I can't use sorting. I have to modify the addDVD method in DVDCollection class so the arraylist is in alphabetical order at all times.
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); movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false); movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false); System.out.println (movies); } }
Java Code:import java.text.NumberFormat; public class DVDCollection { private DVD[] collection; private int count; private double totalCost; public DVDCollection () { collection = new DVD[100]; count = 0; totalCost = 0.0; } public void addDVD (String title, String director, int year, double cost, boolean blueray) { if (count == collection.length) increaseSize(); collection[count] = new DVD (title, director, year, cost, blueray); totalCost += cost; count++; } 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"; for (int dvd = 0; dvd < count; dvd++) report += collection[dvd].toString() + "\n"; return report; } private void increaseSize () { DVD[] temp = new DVD[collection.length * 2]; for (int dvd = 0; dvd < collection.length; dvd++) temp[dvd] = collection[dvd]; collection = temp; } }
Java Code:public class DVD extends DVDCollection { private String title, director; private int year; private double cost; private boolean bluRay; 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; } }
Last edited by artur; 02-28-2012 at 01:45 AM.
- 02-27-2012, 09:10 PM #2
Re: Keeping objects sorted in an array.
Yikes, that's a lot of code.
Buy just looking at it quickly, DVDCollection is an Object that contains an array. Your selectionSort() method takes an array, not an Object that contains an array.How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 02-27-2012, 09:20 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Re: Keeping objects sorted in an array.
Thanks. Any advice on how i can fix it ? I'm kind of lost.
- 02-27-2012, 09:35 PM #4
Re: Keeping objects sorted in an array.
I think that the fix is implied by the problem. You don't want to pass the DVDCollection into the sort function. You want to pass the array that's inside the DVDCollection to the sort function.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 02-27-2012, 10:32 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Re: Keeping objects sorted in an array.
I just found out from my teacher that we should not use sorting. But instead modify the addDVD method so it keeps the DVDs in order after each DVD is added.
Is there any way to add the object to specific place in a array ?
- 02-28-2012, 01:19 AM #6
Re: Keeping objects sorted in an array.
Sure, just set whatever index of the array you want equal to the Object. The trick is then shifting things around so as not to lose what used to be in that index.
How to Ask Questions the Smart Way
Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
Static Void Games forum - Come say hello!
- 02-28-2012, 01:46 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
I used a TreeSet model to get a sorted JList but it isn't sorted.
By eLancaster in forum New To JavaReplies: 1Last Post: 08-11-2011, 08:56 PM -
Checking to see if Array is sorted
By pytho in forum New To JavaReplies: 5Last Post: 07-06-2011, 02:39 AM -
Keeping Only The JButtons In a Selected Row Active In A 2D Array
By java999 in forum Advanced JavaReplies: 8Last Post: 04-22-2011, 02:55 PM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 04:21 AM -
Sorting, Searching, and Inserting into a sorted array
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 09:39 PM
Bookmarks