AP FRQ Practice
int pts;
if (levelOne.goalReached()) {
if (this.isBonus()) {
pts = pts + (levelOne.getPoints() * 3);
}
else {
pts += levelOne.getPoints();
}
}
if (levelTwo.goalReached() && levelOne.goalReached()) {
if (this.isBonus()) {
pts = pts + (levelTwo.getPoints() * 3);
}
else {
pts += levelTwo.getPoints();
}
}
if (levelThree.goalReached() && levelTwo.goalReached() && levelOne.goalReached()) {
if (this.isBonus()) {
pts = pts + (levelThree.getPoints() * 3);
}
else {
pts += levelThree.getPoints();
}
}
return pts;
Comments:
- misread the problem. read bonus levels instead of bonus game.
int highest = Integer.MIN_VALUE;
int temp;
int pts;
if (num > 0) {
for (i=0; i < num; i++) {
this.play();
temp = this.getScore();
if (temp > highest) {
highest = temp;
}
}
}
return highest;
- play() not this.play()
- if (num>0) not needed
public class Book {
private String title;
private double price;
public Book(String bookTitle, double bookPrice) {
title = bookTitle;
price = bookPrice;
}
public String getTitle() {
return title;
}
public String getBookInfo() {
return title + "-" + price;
}
}
public class Textbook extends Book {
public int edition;
public Textbook(String bookTitle, double bookPrice, int editionnum) {
super(String bookTitle, double bookPrice);
edition = editionnum;
}
public int getEdition() {
return edition;
}
public String getBookInfo() {
super.getBookInfo() + "-" + edition;
// return title + "-" + price + "-" + editionnum;
}
public boolean canSubsituteFor(Textbook two) {
if (two.getTitle().equals(getTitle()) && two.getEdition() <= getEditon()) {
return true;
}
else {
return false;
}
}
}
- edition needs to be private
- the super in the constructor should not redefine bookTItle as String, etc
- forgot the return in getBookInfo()
- if else not needed
- misspelled getEdition()
Corrected code (essential parts)
public class Textbook extends Book {
private int edition;
public Textbook(String bookTitle, double bookPrice, int editionnum) {
super(bookTitle, bookPrice);
edition = editionnum;
}
public int getEdition() {
return edition;
}
public String getBookInfo() {
return super.getBookInfo() + "-" + edition;
// return title + "-" + price + "-" + editionnum;
}
public boolean canSubsituteFor(Textbook two) {
if (two.getTitle().equals(getTitle()) && two.getEdition() <= this.getEdition()) {
return true;
}
else {
return false;
}
}
public static void main(String[] args) {
Textbook test = new Textbook("Harry Potter", 5.0, 3);
System.out.println(test.getBookInfo());
}
}
Textbook.main(null);
public 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;
}
}
public class ReviewAnalysis {
private Review[] allReviews;
int sum;
double average;
String comment;
String end;
public ReviewAnalysis(Review[] reviews) {
allReviews = reviews;
}
public double getAverageRating() {
//part a
for (int i=0; i<allReviews.length; i++) {
sum += allReviews[i].getRating();
}
average = sum/allReviews.length;
return average;
}
public ArrayList<String> collectComments() {
//part b
ArrayList<String> array = new ArrayList<String>();
for (int i=0; i<allReviews.length; i++) {
comment = allReviews[i].getComment();
if (comment.indexOf("!") != -1) {
end = comment.substring(comment.length - 1);
if (!end.equals(".") && !end.equals("!")) {
end += ".";
}
array.add(i + "-" + comment);
}
}
return array;
}
}
Corrections
- .length() used to get length of string. .length used to get length of array
Corrected
public class ReviewAnalysis {
private Review[] allReviews;
int sum;
double average;
String comment;
String end;
public ReviewAnalysis(Review[] reviews) {
allReviews = reviews;
}
public double getAverageRating() {
//part a
for (int i=0; i<allReviews.length; i++) {
sum += allReviews[i].getRating();
}
average = sum/allReviews.length;
return average;
}
public ArrayList<String> collectComments() {
//part b
ArrayList<String> array = new ArrayList<String>();
for (int i=0; i<allReviews.length; i++) {
comment = allReviews[i].getComment();
if (comment.indexOf("!") != -1) {
end = comment.substring(comment.length() - 1);
if (!end.equals(".") && !end.equals("!")) {
end += ".";
}
array.add(i + "-" + comment);
}
}
return array;
}
public static void main(String[] args) {
Review[] reviews = {
new Review(3, "This is a review."),
new Review(4, "This is another review!"),
new Review(5, "This is a! third review")
};
ReviewAnalysis analysis = new ReviewAnalysis(reviews);
System.out.println("Average rating: " + analysis.getAverageRating());
ArrayList<String> comments = analysis.collectComments();
System.out.println("Comments with exclamation marks:");
for (String comment : comments) {
System.out.println(comment);
}
}
}
ReviewAnalysis.main(null);
indexOf review
- finds and returns the index of the character(s) it's looking for
- return -1 if it can't find it
- thus checkign to see if it's greater or equal to 0 basically checks if it exists
- only used for strings
substring review
- remember strings start at index 0
- only goes from starting pt to end-1
- for ex: substring(2,4) only gives index 2 and 3
- substring(2) would give everythign from index 2 and beyond
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[j].length; j++) {
int random = (int) (Math.random() * (MAX-1)) + 1;
while (random%10 != 0 && random%100 == 0) {
random = (int) (Math.random() * (MAX-1)) + 1;
}
grid[i][j] = random;
}
}
- oops use MAX not MAX -1 cause I'm adding 1 to the end
int count = 0;
for (int i = 0; i < grid[0].length; i++) {
boolean tof = true;
for (int j = 1; j < grid.length; j++) {
if (grid[i][j] < grid[i-1][j]) {
tof = false;
}
}
if (tof) {
count++;
}
return count;
}
noice
int freq;
int length = guess.length();
int j;
for (int i=0; i<secret.length; i=i+length) {
j = i+length+1;
if (secret.substring(i, j) == guess) {
freq++;
}
}
return freq * length * length;
- I once again forgot to use .equals :')
- forgot parenthesis for .legnth in for loop
- <= in for loop
int a = game.scoreGuess(guess1);
int b = game.scoreGuess(guess2);
int num1;
int num2;
if (a>b) {
return a;
}
if (b>a) {
return b;
}
else {
num1 =
//idk what to do lmao unless I create a huge array which I aint doing
}
- didn't know you could use compareTo like that
compareTo review
- It compares strings in dictionary (lexicographical) order:
- If string1.compareTo(string2) < 0, then string1 precedes string2 in the dictionary.
- If string1.compareTo(string2) > 0, then string1 follows string2 in the dictionary.
- If string1.compareTo(string2) == 0, then string1 and string2 are identical. (This test is an alternative to string1.equals(string2).)