콘텐츠로 이동

Mini Project Set 3 Answer: Movie Rating Dashboard

Complete Program

public class Main {
    public static String stars(int rating) {
        String result = "";

        for (int i = 0; i < rating; i++) {
            result += "*";
        }

        return result;
    }

    public static void main(String[] args) {
        String[] titles = {"Sky Route", "Moonlight Cafe", "Code Quest", "Ocean Signal", "Last Train"};
        int[] ratings = {4, 5, 3, 5, 2};

        int totalRatings = 0;
        int bestIndex = 0;
        int recommendedMovies = 0;

        for (int i = 0; i < ratings.length; i++) {
            System.out.println(titles[i] + ": " + stars(ratings[i])
                    + " (" + ratings[i] + ")");

            totalRatings += ratings[i];

            if (ratings[i] > ratings[bestIndex]) {
                bestIndex = i;
            }

            if (ratings[i] >= 4) {
                recommendedMovies++;
            }
        }

        double average = (double) totalRatings / ratings.length;

        System.out.printf("Average rating: %.2f%n", average);
        System.out.println("Top movie: " + titles[bestIndex]);
        System.out.println("Recommended movies: " + recommendedMovies);
    }
}

Output

Sky Route: **** (4)
Moonlight Cafe: ***** (5)
Code Quest: *** (3)
Ocean Signal: ***** (5)
Last Train: ** (2)
Average rating: 3.80
Top movie: Moonlight Cafe
Recommended movies: 3

Key Points

  • titles and ratings are parallel arrays: both values at index i describe the same movie.
  • The condition ratings[i] > ratings[bestIndex] preserves the first movie when ratings are tied.
  • stars is a reusable helper method; it uses a loop that works for any non-negative rating.
  • A double cast is needed before calculating the average.