Nokomis Sheridan

Greenhorn
+ Follow
since Dec 05, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Nokomis Sheridan

Arf! Okies, thanks so much! I think I've just been looking at this code too long *grin*....

It works now!
18 years ago
Thank you for your response.

hmmm...okies...can you be just a "hair" more specific...please?
18 years ago
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!
18 years ago
oops! sorry!

it's fixed now!
18 years ago
Did I break the question rule? I didn't think it was that difficult...just for me it was.
18 years ago
I'm not sure what I'm doing wrong, but I am getting a .class error in my main method. Could someone please look at it for me?

public class Compact
{
int data[] = new int[100];
int count = 0;

public static void main(String[] args)
{
System.out.println("Compact.java by Chicken Little");
System.out.println();
Compact a = new Compact();
a.removeZeros();
}
public Compact()
{
String fileName = "readme.txt";
FileInput inFile = new FileInput(fileName);
while (inFile.hasMoreTokens())
{
data[count] = inFile.readInt();
count++;
}
}
public void removeZeros(int arrayWithZeros[])
{
for (int i = 0; i < arrayWithZeros.length; i++)
{
if (arrayWithZeros[i] == 0)
{
// Move all elements 1 position
for (int j = i; j < arrayWithZeros.length - 1; j++)
{
arrayWithZeros[j] = arrayWithZeros[j+1];
}
// Then put something at the end, in this case, i want to put a -1
arrayWithZeros[arrayWithZeros.length - 1] = -1;
}
System.out.println(arrayWithZeros[i]);
}
}

}
18 years ago