Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class MovieRatting { public static void main(St

ID: 3726869 • Letter: I

Question

import java.util.Scanner;

public class MovieRatting {

public static void main(String[] args) {

// TODO Auto-generated method stub

int numberOfMovies=0;

int rating=0;

int ratingSum=0;

boolean flag=false;

while(rating>=0)

{

System.out.println("Enter Movie Ratings, enter negative number to quit: ");

Scanner sc=new Scanner(System.in);

rating=sc.nextInt();

if(rating<0)

{

break;

}

else if(rating>4)

{

do {

System.out.println("please enter the rating in the range of 0-4");

rating=sc.nextInt();

}while(rating>4);

if(rating<0)

{

flag=true;

break;

}

else

{

ratingSum=ratingSum+rating;

numberOfMovies++;

}

}

else

{

numberOfMovies++;

ratingSum=ratingSum+rating;

}

}

if(numberOfMovies==0)

{

System.out.println("Avg Rating of the movies is: "+numberOfMovies);

}

else

{

double avgRating=ratingSum/numberOfMovies;

System.out.println("Avg rating of the movies is "+avgRating);

}

}

}

The program is already written. Can somebody write the Comments?

Explanation / Answer

Please find my answer.

import java.util.Scanner;

public class MovieRatting {

   public static void main(String[] args) {

       // TODO Auto-generated method stub

       int numberOfMovies=0;

       int rating=0;

       int ratingSum=0;

       boolean flag=false;

      

      

       // reading till user input rating greater than or equal to ZERO

       while(rating>=0)

       {

           // readign rating

           System.out.println("Enter Movie Ratings, enter negative number to quit: ");

           Scanner sc=new Scanner(System.in);

           rating=sc.nextInt();

           // if rating is negative, stop the program

           if(rating<0)

           {

               break;

           }

           // if rating is greater than 4

           else if(rating>4)

           {

               // keep prompting user to enter rating until it is greater than 4

               do {

                   System.out.println("please enter the rating in the range of 0-4");

                   rating=sc.nextInt();

               }while(rating>4);

               // if rating entered is less than 0 then make flag true so that we can stop program

               // after checking flag value

               if(rating<0)

               {

                   flag=true;

                   break;

               }

               // if user entered ranting >=0 && rating <= 4

               else

               {

                   // add current rating value in the total

                   ratingSum=ratingSum+rating;

                   numberOfMovies++; // increasing number of movie

               }

           }

           else

           {

               // increasing number of movie

               numberOfMovies++;

               // add current rating value in the total

               ratingSum=ratingSum+rating;

           }

       }

       // if number of movie is zero then print 0 as average rating

       if(numberOfMovies==0)

       {

           System.out.println("Avg Rating of the movies is: "+numberOfMovies);

       }

       else

       {

           // calculating the average rating

           double avgRating=ratingSum/numberOfMovies;

           System.out.println("Avg rating of the movies is "+avgRating);

       }

   }

}