2022 FRQ Question 3
- 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 indicating the user’s rating of the website and an optionalString
comment field. The comment field in a review object ends with a period (“.”), exclamation point (“!”), or letter, or is aString
of length 0 if the user did not enter a comment.
I was happy to see that this question had only two parts (a) and (b), and I mainly used this issue I found on github. This was nice and I was able to reference this because I had no idea what this FRQ wanted. I was not sure how my group mates figured out what to do, but I was really confused and I did not know how to start. Once Mr. Mortensen said ChatGPT was allowed, I tried to use it but wasn’t sure what to ask it, and then I found this
public class Review
{
private int rating;
private String comment;
public Review(int r, String c)
{
rating = r;
comment = c;
}
public int getRating() // Accessor Method
{
return rating;
}
public String getComment() // Accessor Method
{
return comment;
}
}
public class ReviewAnalysis
{
private Review[] allReviews;
public ReviewAnalysis()
{
allReviews = new Review[5]; // Create an array with a size of 5 (can be adjusted as needed)
allReviews[0] = new Review(1, "Terrible Product!");
allReviews[1] = new Review(2, "Not Satisfied Product!");
allReviews[2] = new Review(3, "Satisfied!");
allReviews[3] = new Review(4, "Good Product!");
allReviews[4] = new Review(5, "Great Product!");
}
public double getAverageRating()
{
double result = 0.0; // Set an initial value for the result
for(int i = 0; i < allReviews.length; i++) // Loop over elements of allReviews
{
result += allReviews[i].getRating(); // Adding current elements rating to the sum
}
return result / allReviews.length; // Divide sum by # of elements (average)
}
public ArrayList<String> collectComments()
{
ArrayList<String> results = new ArrayList(); // initializes result list of Strings
for(int i = 0; i < allReviews.length; i++)
{
String c = allReviews[i].getComment();
if(c.indexOf("!") >= 0)
{
String result = i + "-" + c;
String last = c.substring(c.length()-1);
if("!.".indexOf(last) < 0)
{
result += ".";
}
results.add(result); // Add result into the test
}
}
return results;
}
}
public class Main
{
public static void main(String args[])
{
ReviewAnalysis reviewAnalysis = new ReviewAnalysis();
double averageRating = reviewAnalysis.getAverageRating();
System.out.println("Average Rating: " + averageRating);
}
}
Main.main(null)
Average Rating: 3.0
Part 3a
import java.util.Scanner; //library for user input
public class Review // Code provided by AP Exam
{
private int rating;
private String comment;
/** Precondition: r >= 0 * c is not null. */
public Review(int r, String c) // Constructor for the Review class
{
rating = r; // Defines parameters
comment = c;
}
public int getRating()
{
return rating;
}
public String getComment()
{
return comment;
}
// There may be instance variables, constructors, and methods that are not shown.
}
import java.util.Math; //library for user input
public double getAverageRating()
{
double totalRating = 0;
int numReviews = allReviews.length;
for (Review review : allReviews)
{
totalRating += review.getRating();
}
return totalRating / numReviews;
}
// getAverageRating.main(null);
| import java.util.Math;
cannot find symbol
symbol: class Math
Checking Answers
I got the public double getAverageRating() and then I put the integer which was set to the value of the rating. From this, I got the first point, and I also accumalated the sum. I did access every element of the allReviews Functions, but I don’t think that I actually did it right. For the third point, I was not able to compute and return the double average because I had so much trouble with Java.
Points: 1/3
public double getAverageRating() // initializes and accumalates the sum
{
int sum = 0;
for (Review r : allReviews) // Used to access every element of allReviews
{
sum += r.getRating();
}
return (double)sum / allReviews.length; // This will compute the double average rating based on the values on getRating
}
// getAverageRating.main(null);
Part 3b (Draft)
public ArrayList<String> collectComments() {
ArrayList<String> formattedComments = new ArrayList<>();
// for (int i = 0; i < allReviews.length; i++)
{
// String comment = allReviews[i].getComment();
if (comment.contains("!"))
{
formattedComments.add(i + "-" + comment);
if (!(comment.endsWith(".") || comment.endsWith("!")))
{
formattedComments.set(formattedComments.size() - 1, formattedComments.get(formattedComments.size() - 1) + ".");
}
}
}
}
| if (comment.contains("!"))
cannot find symbol
symbol: variable comment
| formattedComments.add(i + "-" + comment);
cannot find symbol
symbol: variable i
| formattedComments.add(i + "-" + comment);
cannot find symbol
symbol: variable comment
| if (!(comment.endsWith(".") || comment.endsWith("!")))
cannot find symbol
symbol: variable comment
| if (!(comment.endsWith(".") || comment.endsWith("!")))
cannot find symbol
symbol: variable comment
| public ArrayList<String> collectComments() {
| ArrayList<String> formattedComments = new ArrayList<>();
|
| // for (int i = 0; i < allReviews.length; i++)
| {
| // String comment = allReviews[i].getComment();
| if (comment.contains("!"))
| {
| formattedComments.add(i + "-" + comment);
|
| if (!(comment.endsWith(".") || comment.endsWith("!")))
| {
| formattedComments.set(formattedComments.size() - 1, formattedComments.get(formattedComments.size() - 1) + ".");
| }
| }
| }
| }
missing return statement
Checking Answers
I did use ChatGPT to help me develop the ArrayList and I made the string a parameter in order to do it. Points: 1/9
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;
}
Main.main(null)
| if !(last.equals("!") && !last.equals("."))
'(' expected
| if !(last.equals("!") && !last.equals("."))
')' expected
Major Takeaways
- This APCSA exam is very different thant the APCSP AP Exam and that I am going to have to learn a lot more about java in order to really understand the fundamentals
- I am going to have to really explore Java more and I should probably start LeetCode as a beginner for Java because I am not sure I quite understand Java to the extent I need it
- I should really use ChatGPT and ask people in my group what exactly I need to do
-
From my collaboration with Toby, he really taught me how the Array List works and how it works, like how it pulls the objects defined under a specific class.
-
What a constructor is
- What an ArrayList is
- What the [] signify in
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(5);
numbers.add(10);
numbers.add(20);
int firstNumber = numbers.get(0);
int secondNumber = numbers.get(1);
numbers.set(1, 15);
numbers.remove(0);
int size = numbers.size();
Finn 0.93/1
- I thought he knew what he was doing and that he was very proficient in Java
Justin 0.9/1
- He seemed to know what he was talking about during the presentation
Mati 0.85/1
- He seemed to know what he was talking about during the presentation
- I thought he knew what he was doing in Python and mentioned that he could not figure out Java
James 0.88/1
- Explained code very well
- He put it into one cell
- It’s a lot of work but we got through most of it