Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

java program that reads a file calculates the average rating for each faculty member and outputs the average along with the number of reviews

You have collected a file of faculty where each faculty member is rated 1 (lowest)
to 5 (highest). The first line of the file is a number that identifies how many rating
entries are in the file. Each rating then consists of two lines: the unique ID of the
faculty followed by the numeric rating from 1 to 5. Here is a sample rating file with
three unique faculty IDs and six ratings:
8
Eve_8640
5
Kate_6721
5
Eve_8640
3
Rex_5432
4
Eve_8640
1
Kate_6721
2
Rex_5432
2
Kate_6721
1
Write a java program that reads a file in this format, calculates the average rating for
each faculty member, and outputs the average along with the number of reviews.
Here is the desired output for the sample data:
Eve_8640: 3 reviews, average of 3.0 / 5
Kate_6721: 3 reviews, average of 2.6 / 5
Rex_5432: 2 reviews, average of 3.0 / 5
Use an ArrayList to calculate the output. Your map(s) should index from a string
representing each faculty member’s name to integers that store the number of reviews
for the faculty ID and the sum of the ratings for the faculty member.

Answer:


/**
 * This program reads a file of movie ratings and
 * calculates the average rating for each using
 * a Hashmap.  The hashmap uses the movie name as
 * the key and the item is an object that stores
 * the total ratings and number of ratings.
 */
import java.util.HashMap;
import java.util.Set;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class RatingStats
{
	private int numRatings, sumRatings;

	public RatingStats()
	{
		numRatings = 0;
		sumRatings = 0;
	}

	public int getNumRatings()
	{
		return numRatings;
	}

	public int getSumRatings()
	{
		return sumRatings;
	}

	public void addRating(int rating)
	{
		numRatings++;
		sumRatings += rating;
	}
}

public class Question
{
	public static void main(String[] args)
	{
	   // The ratings HashMap maps from a movie name
	   // to a RatingStats object
	   HashMap<String,RatingStats> ratings =
	   		new HashMap<String,RatingStats>();

	   // First load the ratings from the file to the map
	   try
	   {
 		   Scanner inputStream =
              new Scanner(new FileInputStream("ratings.txt"));

			int numRatings = 0;
			String line = inputStream.nextLine();
			numRatings = Integer.parseInt(line);

		   // Read in each rating which is the movie
		   // name first followed by the rating
		   for (int i = 0; i < numRatings; i++)
		   {
			   String movieName = inputStream.nextLine();
			   line = inputStream.nextLine();

			   int rating = Integer.parseInt(line);
			   // Add new RatingStats object if not already there
			   if (!ratings.containsKey(movieName))
			   {
				   RatingStats rs = new RatingStats();
				   rs.addRating(rating);
				   ratings.put(movieName, rs);
			   }
			   else
			   {
				   RatingStats rs = ratings.get(movieName);
				   rs.addRating(rating);
			   }
		   }
		   inputStream.close();
	   }
       catch(FileNotFoundException e)
       {
           System.out.println("File was not found");
           System.out.println("or could not be opened.");
       }
       catch(IOException e)
       {
           System.out.println("Error reading from file.");
       }

       // Iterate through the HashMap
       // and output the average for each movie
       Set<String> movies = ratings.keySet();
	   for (String m : movies)
	   {
		   RatingStats rs = ratings.get(m);
		   double ave = rs.getSumRatings() /
		   				(double) rs.getNumRatings();
		   System.out.printf("%s: %d reviews, average of %.1f/5\n",
		   			m, rs.getNumRatings(), ave);
	   }

	}
}  


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!