Results 1 to 7 of 7
  1. #1
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default 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.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Keeping objects sorted in an array.

    Thanks. Any advice on how i can fix it ? I'm kind of lost.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default 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 ?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default 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 - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    artur is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Keeping objects sorted in an array.

    Thanks for answering my question. But i need a loop that will take added DVD and put in an arraylist in alphabetical order. So the arraylist is in alphabetical order at all time. I have a real problem with figuring how to do that without using sorting.

Similar Threads

  1. Replies: 1
    Last Post: 08-11-2011, 08:56 PM
  2. Checking to see if Array is sorted
    By pytho in forum New To Java
    Replies: 5
    Last Post: 07-06-2011, 02:39 AM
  3. Replies: 8
    Last Post: 04-22-2011, 02:55 PM
  4. Replies: 8
    Last Post: 04-22-2008, 04:21 AM
  5. Replies: 0
    Last Post: 04-14-2008, 09:39 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
  •