Write a Java application and use a two-dimensional array that will store the three ratings of five TV shows. Your program must: Q.2.1 Contain a two-dimensional array to contain three ratings for five different series and a single array to contain the series names. SERIES NAME RATING 1 RATING 2 RATING 3 Big Bang Theory 9 8 5 Scandal 7 5 5 Modern Family 8 8 8 Dexter 10 7 8 Grand Designs 3 1 5 (6) Q.2.2 Calculate and print the average of the TV rating for each TV show. (4) Q.2.3 Determine if the series will be continued for another season. If the average rating of the series is greater than or equal to seven then the TV network will continue with a new season. (5) Q.2.4 Print out each series name with the average rating and if there will be a series continuation.
// TVShowRatings.java
public class TVShowRatings {
public static void main(String[] args) {
// creating and initializing a 2D array containing three ratings of all
// 5 series
int ratings[][] = { { 9, 8, 5 }, { 7, 5, 5 }, { 8, 8, 8 },{ 10, 7, 8 }, { 3, 1, 5 } };
// creating a String array containing series names
String seriesNames[] = { "Big Bang Theory", "Scandal", "Modern Family", "Dexter", "Grand Designs" };
// displaying heading
System.out.printf("%-24s %-16s %-20s\n", "TV SHOW", "RATING", "SERIES CONTINUATION");
System.out.println("-------------------------------------------------------------");
// looping from i=0 to i=seriesNames.length-1
for (int i = 0; i < seriesNames.length; i++) {
int sum = 0;
// looping and summing the ratings of current series.
for (int j = 0; j < ratings[i].length; j++) {
sum += ratings[i][j];
}
// finding integer average of the rating by dividing sum by count
int avg = sum / ratings[i].length;
// using a ternary operator, assigning to a String variable "Yes" if
// the average rating is greater than or equal to 7, "No" otherwise
String continuation = (avg >= 7) ? "Yes" : "No";
// displaying show name, average rating and continuation status
System.out.printf("%-24s %-16d %-20s\n", seriesNames[i], avg, continuation);
}
}
}
Comments
Leave a comment