Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Couple of Methods...Help Please  RSS feed

 
Nokomis Sheridan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't quite figure out how to do these methods...I've done everything else except this last part. Can someone help me, please?

Here's the part of the lesson that's giving me problems:

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.

and here's the code that I've done so far:

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 --------------------//

class Movie implements Comparable
{
private String myTitle; // title of Bond film
private String myBondActor; // name of actor of portrayed James Bond
private int myYear; // year film was released
private double myFilmRating;// from all-reviews.com
private int myLengthHours; // hours (truncated) portion of film length
private int myLengthMinutes;// minutes beyond truncated hours

public Movie(String title, String name, int yr, double rating, int hrs, int
min)
{
myTitle = title;
myBondActor = name;
myYear = yr;
myFilmRating = rating;
myLengthHours = hrs;
myLengthMinutes = min;
}

public String getTitle()
{
return myTitle;
}

public String getBondActor()
{
return myBondActor;
}

public int getYearFilmReleased()
{
return myYear;
}

public double getFilmRating()
{
return myFilmRating;
}

public int getFilmHrs()
{
return myLengthHours;
}

public int getFilmMin()
{
return myLengthMinutes;
}

public int compareTo(Object other)
{
return (int)(myFilmRating*10) - (int)((((Movie)other).myFilmRating)*10);
}

public String toString()
{
return (Format.left(myTitle,34) + Format.left(myBondActor,16)
+ Format.center(myYear,6) + Format.center(myFilmRating,7,1)
+ " " + myLengthHours + " hr " + myLengthMinutes + " min");
}
}

Any help would be greatly appreciated!
 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your needs, it's sufficient to just go through your list, retrieve the lengths/ratings, and average them all together. The routines displayAveRating and displayAveLength should each take maybe six lines of code a piece. Just think about how a human would do it, and tell the computer to do the same.
 
Nokomis Sheridan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response.

hmmm...okies...can you be just a "hair" more specific...please?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this (in pseudocode)

now all you have to do is "translate" that into java syntax.
 
Nokomis Sheridan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arf! Okies, thanks so much! I think I've just been looking at this code too long *grin*....

It works now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!