I am trying to sort my array of DVDs without using a sorting algorithm. This works with 4 or so DVDs but displays error java.lang.ArrayIndexOutOfBoundsException: 4 with anymore added.
I don't know why it is doing this.
My other problem is implementing a binary search.
import java.text.NumberFormat; public class DVD { public 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 getTitle(){ return title; } 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; } }
import java.text.NumberFormat; public class DVDCollection { private DVD[] collection; private int count; private double totalCost; private double average; public DVDCollection () { collection = new DVD[0]; count = 0; totalCost = 0.0; } public void addDVD (String title, String director, int year, double cost, boolean blueray) { DVD[] temp = new DVD[collection.length + 1]; int index = 0; int i = 0; for(; 0 < collection.length; i++) { if(collection[i].getTitle().compareTo(title)>0) { temp[index++] = new DVD (title, director, year, cost, blueray); break; } temp[index++] = collection[i]; } for(; i < collection.length; i++){ temp[index++] = collection[i]; } if(index != temp.length) temp[index] = new DVD (title, director, year, cost, blueray); collection = temp; totalCost += cost; average=totalCost/collection.length; } public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; report += "My DVD Collection\n\n"; report += "Number of DVDs: " + collection.length + "\n"; report += "Total cost: " + fmt.format(totalCost) + "\n"; report += "Average cost: " + average; report += "\n\nDVD List:\n\n"; for (int i = 0; i < collection.length; i++) report += collection[i] + "\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; } }
public class Movies { //----------------------------------------------------------------- // Creates a DVDCollection object and adds some DVDs to it. Prints // reports on the status of the collection. //----------------------------------------------------------------- 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); } }
public static Comparable searchForDVD (Comparable[] collection, Comparable target) { int min=0, max=collection.length-1, mid=0; boolean found = false; while (!found && min <= max) { mid = (min+max) / 2; if (collection[mid].compareTo(target) == 0) found = true; else if (target.compareTo(collection[mid]) < 0) max = mid-1; else min = mid+1; } if (found) return collection[mid]; else return -1; }