Q&A: please help me make this java program do what i want?

Question by lookadistraction: please help me make this java program do what i want?
in the end of all of this i need to use a second for loop to traverse the array and find the highest rating but i cant seem to do it. i made an attempt in the end but it doesnt work.

i also need to change the Movie class to allow the ratings to be numbers from 1.0 to 10.0 (note that this is a single decimal place) and set them randomly, while keeping all the previous functionality. i think i need it to be float instead of int but im not sure

i also need something In the case of a tie for maximum rating, i need it to print both movies. i think need two System.out.println?
class Movie
{
String title;
String genre;
int rating;
}

class MovieTestDrive
{
public static void main( String[] args )
{
Movie[] movi = new Movie[10];
movi[0] = new Movie();
movi[0].title = “Harry Potter,”;
movi[0].genre = “fantasy, “;

movi[1] = new Movie();
movi[1].title = “James Bonds,”;
movi[1].genre = “action, “;

movi[2] = new Movie();
movi[2].title = “Home alone,”;
movi[2].genre = “comedy, “;

movi[3] = new Movie();
movi[3].title = “Star wars,”;
movi[3].genre = “scifi, “;

movi[4] = new Movie();
movi[4].title = “March of the penguins,”;
movi[4].genre = “Documentary, “;

movi[5] = new Movie();
movi[5].title = “The day after tomorrow,”;
movi[5].genre = “scifi, “;

movi[6] = new Movie();
movi[6].title = “101 dalmations,”;
movi[6].genre = “action, “;

movi[7] = new Movie();
movi[7].title = “Dark Knight,”;
movi[7].genre = “action, “;

movi[8] = new Movie();
movi[8].title = “Spiderman,”;
movi[8].genre = “action, “;

movi[9] = new Movie();
movi[9].title = “The ring,”;
movi[9].genre = “horror, “;

for (int x=0; x<10; x++) { int rating = ( int )( Math.random( )*10 )+1; System.out.println( "Movie: " + movi[x].title + " Genre:" + movi[x].genre + "Rating:"+ movi[x].rating ); } int max=0; for (int x=0; x<10; x++) { if(maxBest answer:

Answer by MathJakko
First there is a bug in your program:
you did not copy the rating into Movie
you need
movi[x].rating = rating;
after the
int rating = ( int )( Math.random( )*10 )+1;

otherwise the rating printed is always 0.
insert this line and try it.

The second question is how to have a rating between 1.0. and 10.0
double drating = (double) rating;
of course you have to change the definition of Movie.

Try this and come back for the third question.

Add your own answer in the comments!

Get the book now