Definition:
An Array is a collection of items of the same type, stored in sequence at memory locations. An Array is used to store data and is used in almost every programming language out there. In Java, an Arraylist is an Array that can be modified and they are more flexible and versatile.
Examples:
FRQ Question:
Explaination:
First, We must read the question and understand what it is telling us. Next, We figure out the solution via our understanding:
public double getAverageRating()
{
int sum = 0;
for (Review r : allReviews)
{
sum += r.getRating();
}
return (double) sum / allReviews.length;
}
public ArrayList<String> collectComments()
{
ArrayList<String> commentList = new ArrayList<String>();
for (int i = 0; i < allReviews.length; i++)
{
String comment = allReviews[i].getComment();
if (comment.indexOf("!") >= 0)
{
String last =
comment.substring(comment.length() – 1);
if (!last.equals("!") && !last.equals("."))
{
comment += ".";
}
commentList.add(i + "-" + comment);
}
}
return commentList;
}
This code defines a method named
getAverageRating()that calculates and returns the average rating for a collection of reviews. Here’s a breakdown of what the code does:
-
It initializes an integer variable called
sumwith an initial value of 0. This variable will be used to accumulate the sum of all the ratings. -
It enters a for-each loop that iterates over a collection of reviews stored in the
allReviewsarray or list (assumingallReviewsis an array or list ofReviewobjects). -
Inside the loop, for each
Reviewobjectrin theallReviewscollection, it retrieves the rating for that particular review using thegetRating()method of theReviewobject and adds it to thesum. This step effectively accumulates the sum of all ratings in the collection. -
After the loop completes, it calculates the average rating by dividing the
sumby the number of reviews in theallReviewscollection. To ensure that the result is a decimal value (a double), it explicitly castssumto adoublebefore performing the division. -
Finally, it returns the calculated average rating as a double.
In summary, this code computes the average rating for a collection of reviews by iterating through the reviews, summing up their ratings, and then dividing the sum by the number of reviews. It returns the average rating as a decimal (double) value.
- The second part:
This code defines a method named
collectComments()that processes an array (or list) of reviews, performs some modifications to their comments, and then returns a list of formatted comments as strings. Let’s break down what this code does step by step:
-
It initializes an empty ArrayList of strings called
commentList. This ArrayList will be used to store the processed comments. -
It enters a for loop that iterates through the elements of an array called
allReviews. The loop variableirepresents the index of the current review in the array. -
Inside the loop, it retrieves the comment associated with the current review using the
getComment()method and stores it in a string variable calledcomment. -
It checks if the
commentcontains an exclamation mark (‘!’) using theindexOf("!")method. If an exclamation mark is found in the comment, it proceeds to modify it. -
It extracts the last character of the
commentusing thesubstring()method. This character is stored in a string variable calledlast. -
It checks if
lastis not equal to an exclamation mark (‘!’) and not equal to a period (‘.’). Iflastis neither ‘!’ nor ‘.’, it appends a period (‘.’) to the end of thecomment. -
It constructs a new string by concatenating the index
i, a hyphen (‘-‘), and the modifiedcomment. This new string is then added to thecommentListArrayList. -
The loop continues to the next review, repeating the process for each review in the
allReviewsarray. -
After processing all the reviews, the method returns the
commentListArrayList, which contains the formatted comments.
In summary, this code processes a collection of reviews, checks if each review’s comment contains an exclamation mark (‘!’), and ensures that the comment ends with a period (‘.’) if it doesn’t already. It then constructs a string in the format “index-comment” and adds it to an ArrayList. The method ultimately returns the ArrayList of formatted comments.