1. Users of a website are asked to provide a review of the website at the end of each visit. Each review, represented by an object of the Review class, consists of an integer indication the user's rating of the website and an optional String comment field. The comment field in a Review object ends with a period ("."), exclamation point ("!"), or letter, or is a String of length 0 if the user did not enter a comment.

    a. Write the ReviewAnalysis method getAverageRating, which returns the average rating (arithmetic mean) of all elements of allReviews.

    b. Write the ReviewAnalysis method collectComments, which collects and formats only comments that contain an exclamation point. The method returns an ArrayList of String objects containing copies of user comments from allReviews that contain an exclamation point, formatted as follows. An empty ArrayList is returned if no comment in allReviews contains an exclamation point.

    • The String inserted into the ArrayList to be returned begins with the index of Review in allReviews.
    • The index is immediately followed by a hyphen ("-").
    • The hyphen is followed by a copy of the original comment.
    • The String must end with either a period or an exclamation point. If the original comment from allReviews does not end in either a period or an exclamation point, a period is added.
// Review class for initializing individual Review objects from users
public class Review {
    private int rating;
    private String comment;

    // Review constructor for setting rating and comment values
    public Review(int r, String c) {
        rating = r;
        comment = c;
    }

    // Method for getting rating data from another class
    public int getRating() {
        return rating;
    }

    // Method for getting comment data from another class
    public String getComment() {
        return comment;
    }
}

// ReviewAnalysis class for taking all of the user reviews and making analyses about them
public class ReviewAnalysis {
    private Review[] allReviews;
    private ArrayList<String> sortedComments;

    // Constructor for initializing the allReviews array, which contains all user Review objects
    public ReviewAnalysis(Review[] reviews) {
        allReviews = reviews;
    }

    // Method for finding the average rating given by all of the users, which it does by adding all of the ratings up,
    // then dividing the sum by the number of reviews

    public double getAverageRating() {
        double averageRating = 0;
        for (Review review : allReviews) {
            averageRating += review.getRating();
        }
        averageRating /= allReviews.length;
        return averageRating;
    }

    // Method for sorting all of the comments that contain an exclamation point, which it does by iterating through the allReviews array,
    // then looping through the characters of the selected Review object's comment property, and adds the comment if it contains an exclamation point,
    // adding a period to the end of it if it does not already end with an exclamation point or period
    public ArrayList<String> collectComments() {
        sortedComments = new ArrayList<String>();
        for (int i = 0; i < allReviews.length; i++) {
            String comment = allReviews[i].getComment();
            String finalComment = "";
            for (int j = 0; j < comment.length(); j++) {
                if (comment.charAt(j) == '!') {
                    finalComment += i + "-" + comment;
                    if (comment.charAt(comment.length() - 1) != '!' && comment.charAt(comment.length() - 1) != '.') {
                        finalComment += '.';
                    }
                    break;
                }
            }
            sortedComments.add(finalComment);
        }
        
        return sortedComments;
    }
}

// Main class for presenting test cases
public class Main {
    public static void main(String[] args) {
        Review firstReview = new Review(4, "Good! Thx");
        Review secondReview = new Review(3, "OK site");
        Review thirdReview = new Review(5, "Great!");
        Review fourthReview = new Review(2, "Poor! Bad.");
        Review fifthReview = new Review(3, "");
        Review[] reviews = {firstReview, secondReview, thirdReview, fourthReview, fifthReview};
        ReviewAnalysis myObj = new ReviewAnalysis(reviews);
        System.out.println(myObj.getAverageRating());
        for (String sortedComment : myObj.collectComments()) {
            System.out.print(sortedComment + " ");
        }
    }
}

Main.main(null);
3.4
0-Good! Thx.  2-Great! 3-Poor! Bad.