Not sure how to do this method


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Not sure how to do this method

  1. #1
    Join Date
    Nov 2004
    Location
    Northern CA
    Posts
    17

    Not sure how to do this method

    Here are the specs:

    2. Revise the Collection class to calculate and display (to the screen)
    the average rating AND average length for all James Bond movies. Your
    enhancements should include methods called displayAveRating and displayAveMinutes in this class.
    Your additional output should look as follows:

    The average rating for a James Bond Movie is 2.41 out of a possible 4.0
    The average length for a James Bond Movie is 2 hrs and 6.2 minutes

    3. Revise the program to sort the movies by year of release.

    ----------------

    I'm really not sure how to create #2 & #3....please help!
    ------------------------

    Here's my code:

    import chn.util.*;
    import apcslib.*;

    public class Collection
    {
    private Movie[] movieList;

    public Collection(String fileName)
    {
    loadData(fileName);
    }

    public void Search()
    {
    String actor = "Sean Connery";
    double ratingToExceed = 2.0;
    int minutesToExceed = 120;
    int index = sequentialSearch(movieList, actor, ratingToExceed,
    minutesToExceed);
    System.out.println("index = " + index);
    System.out.println(movieList[index].toString());
    }
    public int sequentialSearch(Movie[] list, String dude, double rating, int
    minutes)
    {
    for (int i = 0; i < movieList.length; i++)
    {
    if (dude.compareTo(movieList[i].getBondActor()) == 0 &&
    movieList[i].getFilmMin() + 60*movieList[i].getFilmHrs() >=
    minutes &&
    movieList[i].getFilmRating() >= rating)
    return i;
    }
    return -1;
    }
    private void loadData(String fileName)
    {
    String movieTitle;
    String bondName;
    int yearReleased;
    double movieRating;
    int lengthHours;
    int lengthMinutes;

    FileInput inFile = new FileInput(fileName);

    int numReleases = inFile.readInt();
    movieList = new Movie[numReleases];

    for (int i = 0; i < numReleases; i++)
    {
    movieTitle = inFile.readLine();
    bondName = inFile.readLine();
    yearReleased = inFile.readInt();
    movieRating = inFile.readDouble();
    lengthHours = inFile.readInt();
    lengthMinutes = inFile.readInt();

    movieList[i] = new Movie(movieTitle, bondName, yearReleased,
    movieRating, lengthHours,
    lengthMinutes);
    }
    }

    public void displayInfo()
    {
    System.out.print(Format.center("Film Title",34));
    System.out.print(Format.center("Bond Actor",16));
    System.out.print(Format.center("Year",6));
    System.out.print(Format.center("Rating",7));
    System.out.print(Format.right("Film Length",16));
    System.out.println();
    System.out.println();
    for (int i = movieList.length - 1; i >= 0 ; i--)
    {
    String output = movieList[i].toString();
    System.out.println(output);
    }
    }

    public void displayAveRating()
    {
    }

    public void displayAveMinutes()
    {
    }

    public void Sort()
    {
    quickSort(movieList,0,movieList.length - 1);
    }

    private void quickSort(Movie[] list, int first, int last)
    {
    int g = first;
    int h = last;
    int midIndex;
    Movie dividingValue;

    midIndex = (first + last) / 2;
    dividingValue = list[midIndex];
    do
    {
    while (list[g].compareTo(dividingValue) < 0)
    {
    g++;
    }
    while (list[h].compareTo(dividingValue) > 0)
    {
    h--;
    }
    if (g <= h)
    {
    // swap g and h
    Movie temp = list[g];
    list[g] = list[h];
    list[h] = temp;
    g++;
    h--;
    }
    } while (g < h);
    if (h > first)
    {
    quickSort(list, first, h);
    }
    if (g < last)
    {
    quickSort(list, g, last);
    }
    }

    public static void main(String[] args)
    {
    Collection seriesData = new Collection("bond.txt");
    seriesData.Sort();
    seriesData.displayInfo();
    seriesData.displayAveRating();
    seriesData.displayAveMinutes();
    seriesData.Search();
    }

    }

    //-------------------- End of Collection class --------------------//
    nicomen

  2. #2
    Join Date
    Oct 2005
    Location
    New Jersey
    Posts
    255
    For your second task in the assignment, you only need private variables which will record the total of ratings and movie lengths. Divide it by the total elements before displaying.
    For your third task, you need to use Collections.sort() with a custom Comparator. Refer to some tutorial
    http://java.sun.com/docs/books/tutor...x.html#sorting
    Happiness is good health and a bad memory.

Similar Threads

  1. jdi popframes method
    By caid in forum Java
    Replies: 0
    Last Post: 09-01-2005, 01:12 AM
  2. Replies: 5
    Last Post: 05-20-2002, 12:35 PM
  3. GetRows method
    By Jason West in forum VB Classic
    Replies: 1
    Last Post: 03-28-2002, 08:11 PM
  4. depricated method needs changing
    By Alan Shiers in forum Java
    Replies: 0
    Last Post: 03-15-2002, 12:51 PM
  5. Replies: 3
    Last Post: 04-13-2001, 09:13 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center