FRQ-Homework

// CODE_RUNNER: Question 3: Array / ArrayList
public class Main {
public static void main(String[] args) {
Review[] testReviews = {
new Review(4, "Good! Thx"),
new Review(3, "OK site"),
new Review(5, "Great!"),
new Review(2, "Poor! Bad."),
new Review(3, "")
};
ReviewAnalysis analysis = new ReviewAnalysis(testReviews);
double avg = analysis.getAverageRating();
System.out.println("Average Rating: " + avg);
System.out.println("Expected: 3.4");
System.out.println("Match: " + (Math.abs(avg - 3.4) < 0.001));
}
}
class Review {
private int rating;
private String comment;
public Review(int r, String c) {
rating = r;
comment = c;
}
public int getRating() {
return rating;
}
public String getComment() {
return comment;
}
}
class ReviewAnalysis {
private Review[] allReviews;
public ReviewAnalysis(Review[] reviews) {
allReviews = reviews;
}
public double getAverageRating() {
int totalRating = 0;
for (int index = 0; index < allReviews.length; index++) {
int currentRating = allReviews[index].getRating();
totalRating = totalRating + currentRating;
}
double result = (double) totalRating / allReviews.length;
return result;
}
}
Main.main(null);

// CODE_RUNNER: Question 3: Array / ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Review r1 = new Review(4, "Good! Thx");
Review r2 = new Review(3, "OK site");
Review r3 = new Review(5, "Great!");
Review r4 = new Review(2, "Poor! Bad.");
Review r5 = new Review(3, "");
Review[] reviewArray = {r1, r2, r3, r4, r5};
ReviewAnalysis analyzer = new ReviewAnalysis(reviewArray);
System.out.println("=== Part A: getAverageRating() ===");
double avg = analyzer.getAverageRating();
System.out.println("Average Rating: " + avg);
System.out.println("Expected: 3.4");
System.out.println("Correct: " + (Math.abs(avg - 3.4) < 0.001));
System.out.println();
System.out.println("=== Part B: collectComments() ===");
ArrayList<String> collected = analyzer.collectComments();
System.out.println("Formatted Comments:");
for (int i = 0; i < collected.size(); i++) {
System.out.println(" " + collected.get(i));
}
System.out.println();
System.out.println("Expected: 0-Good! Thx., 2-Great!, 3-Poor! Bad.");
System.out.println("Total comments: " + collected.size());
}
}
class Review {
private int rating;
private String comment;
public Review(int r, String c) {
rating = r;
comment = c;
}
public int getRating() {
return rating;
}
public String getComment() {
return comment;
}
}
class ReviewAnalysis {
private Review[] allReviews;
public ReviewAnalysis(Review[] reviews) {
allReviews = reviews;
}
// Part A
public double getAverageRating() {
int total = 0;
for (int index = 0; index < allReviews.length; index++) {
total = total + allReviews[index].getRating();
}
return (double) total / allReviews.length;
}
// Part B
public ArrayList<String> collectComments() {
ArrayList<String> commentList = new ArrayList<String>();
for (int pos = 0; pos < allReviews.length; pos++) {
String text = allReviews[pos].getComment();
if (text.indexOf("!") >= 0) {
String entry = pos + "-" + text;
char end = entry.charAt(entry.length() - 1);
if (end != '.' && end != '!') {
entry = entry + ".";
}
commentList.add(entry);
}
}
return commentList;
}
}
Main.main(null);
Reflection on the Development Process
Skills and Concepts I Gained
- I learned how to organize information using classes so related data like ratings and comments are stored together.
- I learned how arrays can be used to hold multiple objects of the same type and how to access each object using a loop.
- I learned how methods can process data stored in an array and return a calculated result, such as the average of several ratings.
- I learned how to use a loop to go through every element in a collection and apply the same operation to each one.
- I learned how to work with ArrayList objects to store results that may change in size.
- I learned how to check whether a string contains a specific character using methods like indexOf.
- I learned how to manipulate strings, including adding extra characters when formatting the final output.
- I learned how to combine numbers and text into a single formatted string for display.
Challenges I Faced During the Assignment
- I struggled with remembering to call the correct accessor methods (getRating and getComment) instead of trying to access the variables directly.
- I initially forgot to cast the total rating to a double when calculating the average, which caused incorrect results.
- I had difficulty figuring out how to check whether a comment contained an exclamation mark.
- I struggled with formatting the comments correctly, especially adding a period only when the comment did not already end with punctuation.
- I made some mistakes with index positions when combining the index number with the comment text.
- I also had to debug small logic errors that caused the output to not match the expected format.