AP CSA HW
- 2D Array
- Unit1 Primitive Type / Unit2 Using Object
- Unit2 Using Object
- Unit 3 If Statement
- Unit 4 Iteration
- Unit 5 Classes
- Unit5 part2 HW
- Unit 5 QS 33 FRQ Part a
- Unit 5 QS 33 FRQ Part b
- Unit 5 QS 34 FRQ Part a
- Unit 5 QS 34 FRQ Part b
- Unit6
- Answer: E
- Answer: E, reason: 如果没有负数,最后return的是一个正数
- Answer: E, reason: i++不在结尾,最后会out of range
- Answer: C
- Answer: C, reason: 改变b也会改变arr 因为 arr储存的是address
- Answer: D, 一个Array储存,class object。
- Answer: B, Reason: 没有toString method所以III不行
- Answer: C
- Answer: E, stuArr储存的都是student object所以III是对的
- Answer: B
- Answer: D. 但不清楚为什么,只是其他四个选项都是确定是错的所以选的D
- Answer: C, reason: for-each loop 在这种情况下不能修改card
- Answer: D
- Answer: D
- Unit6 part2 HW
- Answer: A
- Answer: B
- Answer: C
- Answer: A
- Answer: B
- Answer: D
- Answer: C
- Answer: D
- Answer: A
- Answer: B
- Answer: A
- Answer: D
- Answer: D
- Answer: C
- Answer: A
- Answer: C
- Answer: B
- Answer: B
- Answer: D
- Answer: C
- Answer: E
- Answer: A
- Answer: C
- Answer: A
- Answer: A
- Answer: B
- Answer: C
- Answer: A
- Answer: C
- Answer: A
- Answer: B
- Answer: B
- Unit6 FRQ 1-3
- Unit7 HW
- Answer: D, 更详细
- Answer: A
- Answer: B, ArrayList 是 List 的子类
- Answer: E
- Answer: C
- Answer: A
- Answer: A, 左边的Object没有用,但右边的set还是执行了
- Answer: E
- Answer: D, getValue出来的是double1.而double可以直接用==
- Answer: E
- Answer: E
- Answer: C
- Answer: C, II从右往左所以remove不会改变element的index顺序。只有I会skip一些element
- Answer: B
- Answer: E
- Answer: A
- Answer: E
- Answer: D
- Answer: E
- Answer: B
- Answer: D
- Answer: C
- Answer: A
- Answer: C
- FRQ 29-30
- Unit 9 MCQ HW
- Answer: E, "."并不会分割,所以ob.compareTo(intOb)是一个整体
- Answer: A
- Answer: A
- Answer: E
- Answer: D
- Answer: C
- Answer: D
- Answer: E super有时不是必要的
- Answer: D,当两个class中有相同名字的method,不加super默认使用子类class的method
- Answer: B
- Answer: E, SavingsAccount 中的 addInterest method 不能被父类BankAccount class引用。
- Answer: D
- Answer: C
- Answer: D
- Answer: B, private method 不能被其他class引用。即使有extends也不行
- Answer: A, data特指variable
- Answer: E
- Answer: D, 同名函数调用的时候会先调用后面的也就是Thing2中的cal method
- Answer: B
- Answer: B
- Answer: B
- Answer: A
- Answer: B
- Answer: D
- Answer: B, 同名函数调用的时候会先调用后面的也就是Jersey中的speak method
- Answer: A
- Unit 9 HW
//part (a.)
int check = 0;
private void fillBlock(String str){
String [][] letterBlock = new String [numRows][numCols];
for (r = 0; r < letterBlock.length ; r++){
for (c = 0; c < letterBlock[r].length; c++){
if (check > str.length()){
letterBlock[r][c] = "A";
}
else{
letterBlock[r][c] = str.substring(check, check + 1);
}
check ++;
}
}
}
System.out.println(7+8+(7+8)+"Hellow"+7+8+(7+8));
String s = "holy";
String t = s.substring(4);
if (s.equals(t)){
System.out.println("1");
}
public static String scrambleWord(String word){
for (i=0; i < word.length(); i++){
if (word.substring(i, i+1) == "A" && word.substring(i+1, i+2) != "A"){
swap(word, i, i+1);
}
}
return word;
}
// part (a)
public LogMessage (String message){
this.machineID = message.substring(0, message.indexOf(":"));
this.description = message.substring(message.indexOf(":") + 1, message.length());
}
//part (b)
public boolean containsWord(String keyword){
boolean contain = true;
if (description.indexOf(keyword) == -1){
contain = false;
}
return contain;
}
// part (a)
public void replaceNthOccurrence(String str, int n, String repl){
}
public class FrogSimulation{
private int goalDistance;
private int maxHops;
private int hopDistance();
public boolean simulate(){
boolean reach = false;
for (i=0; i<maxHops && reach == false; i++){
goalDistance -= hopDistance();
if (goalDistance < 0){
reach = true;
}
}
return reach;
}
public double runSimulations (int num)
}
public double runSimulations (int num){
int count = 0;
for (i=0; i < num; i++){
if (simulate()){
count ++;
}
}
return (double)count/num;
}
public static int numberOfLeapYears(int year1, int year2){
int count = 0;
for (i = year1; i <= year2 ; i++){
if (isLeapYear(i)){
System.out.println("leap year: " + i);
count++
}
}
return count;
}
public static int dayOfWeek(int month, int day, int year){
return (firstDayOfYear(year) + dayOfYear(month, day, year)-1)%7
}
public boolean isLevelTrailSegment(int start, int end){
int max = markers[start];
int min = markers[start];
for (i = start; i <= end; i++){
if(markers[i] < min){
min = markers[i];
}
else if (marker[i] > max){
max = markers[i];
}
}
return (max - min) <= 10;
}
public boolean isDifficult(){
int counter = 0;
for (i = 1, i < markers.length; i++){
if (math.abs(markers[i]-markers[i-1]) >= 30){
counter ++;
}
}
return counter >= 3;
}
public int limitAmplitude(int limit){
int counter = 0;
for (i = 0; i < samples.length; i++){
if (samples[i] > limit){
samples[i] = limit;
counter++;
}
else if (samples[i] < -1*limit){
samples[i] = -1*limit;
counter++;
}
}
return counter;
}
public void trimSilenceFromBeginning(){
int counter = 0;
for (i=0; i < samples.length && (samples[i] == 0); i++){
counter++;
}
int[] secondArray = new int[samples.length - counter];
for (k=0; k < secondArray.length; k++){
secondArray[k] = samples[counter + k];
}
samples = secondArray;
}
public TokenPass(int playerCount){
board = new int[playerCount];
for (i=0, i<board.length; i++){
board[i] = (int) (math.random() * 10) + 1;
}
currentPlayer = (int) (math.random() * playerCount);
}
public void distributeCurrentPlayerTokens(){
int counter = currentPlayer;
for (i=1, i<=board[currentPlayer], i++){
if ((counter)%(board.length -1) != 0){
board[currentPlayer] -= 1;
counter ++;
board[counter] += 1;
}
else if ((counter)%(board.length -1) == 0){
board[currentPlayer] -= 1;
counter = 0;
board[counter] += 1;
}
}
}
private int getIndexForFit(NumberTile tile){
if (board.size() == 0){
return 0;
}
for (i=0; i<board.size(); i++){
if (((board.get(i)).getRight()).equals(tile.getLeft()) && ((board.get(i+1)).getLeft()).equals(tile.getRight())){
return i+1;
}
else if (i == board.size()-1){
if (((board.get(i)).getRight()).equals(tile.getLeft())){
return i;
}
}
else{
return -1;
}
}
}
public boolean insertTile(NumberTile tile){
int index = board.getIndexForFit(tile);
return board.add(index, tile);
}
public int getTotalBoxes(){
int total = 0;
for (CookieOrder order : orders){
total += order.getNumBoxes();
}
return total;
}
public int removeVariety(String cookieVar){
int total = 0;
for (i=0; i < goodies.size(); i++){
if (((goodies.get(i)).getVariety()).equals(cookieVar) > -1){
total += (goodies.get(i)).getNumBoxes();
goodies.remove(i);
i--;
}
}
return total;
}
ArrayList<Integer> numList = new ArrayList<Integer>(1, 2, 2, 3);
int val = 4;
int index = 0;
while(val > numList.get(index)){
index++;
}
numList.add(index, val)
System.out.println(numList);
//part a
public class RandomStringChooser{
private String[] wordArray;
public int num =0;
public RandomStringChooser(String[] str){
wordArray = str;
}
public String getNext(){
String a = "";
if (wordArray[num] != null){
a = wordArray[num];
num++;
}
else{
a = "None";
}
return a;
}
}
RandomStringChooser sChooser = new RandomStringChooser(wordArray);
// part b
public RandomLetterChooser(String str){
super.(getSingleLetters(str));
}
public class DiverseArray{
public static int arraySum(int[] arr){
int sum = 0;
for (int i = 0; i < arr.length;i++){
sum += arr[i];
}
return sum;
}
public static void main(String[] args) {
int[]array = {0,1,2,3,4};
System.out.println(arraySum(array));
}
}
DiverseArray.main(null);
public class DiverseArray{
public static int[] rowSums(int[][] arr2D){
int[] arr = new int[arr2D[0].length];
int sum = 0;
for (int r = 0; r < arr2D.length;r++){
sum = arraySum(arr2D[r]);
arr[r] = sum;
}
return arr;
}
public static void main(String[] args) {
int[][]array = {{0,1,2,3,4},{5,6,7,8,9},{0,1,2,3,4},{5,6,7,8,9},{0,1,2,3,4}};
for(int num : rowSums(array)){
System.out.println(num);
}
}
}
DiverseArray.main(null);
public class DiverseArray{
public static boolean isDiverse(int[][] arr2D){
int[] diverse = rowSums(arr2D);
for (int i = 0; i<diverse.length-1;i++){
for(int j =i; j <diverse.length-1; j++){
if (diverse[j] = diverse[j+1]){
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int[][]array = {{0,1,2,3,4},{5,6,7,8,9},{0,1,2,3,4},{5,6,7,8,9},{0,1,2,3,4}};
System.out.println(isDiverse(array));
}
}
DiverseArray.main(null);
public class HiddenWord{
private String guess;
public HiddenWord(String word){
guess = word;
}
public void getHint(String word){
if(guess.length() == word.length()){
for (int i = 0; i < word.length(); i++){
if (guess.substring(i, i+1).equals(word.substring(i, i+1))){
System.out.print(word.substring(i, i+1));
}
else if (guess.indexOf(word.substring(i, i+1)) != -1){
System.out.print("+");
}
else {
System.out.print("*");
}
}
}else{
System.out.println("the length doesn't match, please type a new guess");
}
}
public static void main(String[] args) {
HiddenWord puzzle = new HiddenWord("HARPS");
puzzle.getHint("HAABB");
}
}
HiddenWord.main(null);
public int getValueAt(int row, int col){
for (int i = 0; i < entries.size(); i++){
if(entries.get(i).getRow() == row && entries.get(i).getCol() == col){
return entries.get(i).getValue();
}
else{
return 0;
}
}
}
public void removeColumn(int col){
for (int i = 0; i < entries.size(); i++){
if(entries.get(i).getCol() == col){
entries.remove(i);
}
if (entries.get(i).getCol() > col){
int newRow = entries.get(i).getRow();
int newCol = entries.get(i).getCol()-1;
int newValue = entries.get(i).getValue();
entries.set(i, new SparseArrayEntry(newRow, newCol, newValue));
}
}
}
public class NumberGroup{
int[]num;
public NumberGroup(){
num = {0,1,2,3,4};
}
public NumberGroup(int[] Array){
num = Array;
}
public boolean contains(int value){
for (int element : num){
if(element == value){
return true;
}
else{
return false;
}
}
}
}
public class Range extends NumberGroup{
public Range(int min, int max){
int[]num = new int[(max-min)];
for(i = 0; i < (max-min); i++){
num[i] = (min+i);
}
super(num);
}
}
public boolean contains(int num){
for (NumberGroup group : groupList){
return group.contains(num);
}
}
public class StepTracker{
private int minimumstep = 0;
private ArrayList<Integer> days = new ArrayList<Integer>();
public StepTracker(int num){
minimumstep = num;
}
public void addDailySteps(int num){
days.add(num);
}
public int activeDays(){
int active = 0;
if (days.size() > 0){
for (int i = 0; i < days.size(); i++){
if(days.get(i) >= minimumstep){
active++;
}
}
}
return active;
}
public double averageSteps(){
double num = 0;
if(days.size() > 0){
for (int i = 0; i < days.size(); i++){
num += days.get(i);
}
num = (double)num/days.size();
}
return num;
}
}
StepTracker tr = new StepTracker(10000);
tr.activeDays();
tr.averageSteps();
tr.addDailySteps(9000);
tr.addDailySteps(5000);
tr.activeDays();
public class Level{
private static int count = 0;
private int id;
private boolean isGoalReached;
public Level(){
this.id = ++count;
this.isGoalReached = ((int)(Math.random() *10) >4); //equal possibility to return true or not
}
public boolean goalReached(){
return isGoalReached;
}
public int getID(){
return id;
}
public int getPoints(){
if (this.id == 1){
return 200;
}
else if (this.id == 2){
return 100;
}
else if (this.id == 3){
return 500;
}
else{
return -1;
}
}
public void reSet(){
this.isGoalReached = ((int)(Math.random() *10) >4);
}
public static void gameReSet(){
count = 0;
}
}
public class Game{
private Level levelOne;
private Level levelTwo;
private Level levelThree;
public Game(){
Level.gameReSet();
this.levelOne = new Level();
this.levelTwo = new Level();
this.levelThree = new Level();
}
public boolean isBonus(){
return (levelOne.goalReached() && levelTwo.goalReached() && levelThree.goalReached());
}
public void play(){
levelOne.reSet();
levelTwo.reSet();
levelThree.reSet();
}
public int getScore(){ //part a
int sum = 0;
if (levelOne.goalReached()){
sum = levelOne.getPoints();
if(levelTwo.goalReached()){
sum = levelOne.getPoints() + levelTwo.getPoints();
if(levelThree.goalReached()){
sum = levelOne.getPoints() + levelTwo.getPoints() + levelThree.getPoints();
}
}
}
if (isBonus()){
return (sum *= 3);
}
return sum;
}
public int playManyTimes(int num){ //part b
int highestScore = 0;
for (int i = 0; i < num; i++){
play();
if(getScore() > highestScore){
highestScore = getScore();
}
}
return highestScore;
}
public static void main(String[] str){
Game tester = new Game();
System.out.println(tester.getScore());
System.out.println(tester.playManyTimes(4));
}
}
Game.main(null);
public class Book
{
private String title;
private double price;
public Book(String bookTitle, double bookPrice){
this.title = bookTitle;
this.price = bookPrice;
}
public String getTitle(){
return title;
}
public String getBookInfo()
{
return title + "-" + price;
}
}
public class Textbook extends Book{
private int ediNumber;
public Textbook(String bookTitle, double bookPrice, int num){
super(bookTitle, bookPrice);
this.ediNumber = num;
}
public int getEdition(){
return ediNumber;
}
public String getBookInfo(){
return super.getBookInfo() + "-" + getEdition();
}
public boolean canSubstituteFor(Textbook book){
return (super.getTitle().equals(book.getTitle()) && (getEdition() >= book.getEdition()));
}
public static void main(String[] str){
Textbook bio2015 = new Textbook("Biology", 49.75, 2);
Textbook bio2019 = new Textbook("Biology", 39.75, 3);
System.out.println(bio2019.getEdition());
System.out.println(bio2019.getBookInfo());
System.out.println(bio2019.canSubstituteFor(bio2015));
System.out.println(bio2015.canSubstituteFor(bio2019));
Textbook math = new Textbook("Calculus", 45.25, 1);
System.out.println(bio2015.canSubstituteFor(math));
}
}
Textbook.main(null);
public class Review{
private int rating;
private String comment;
public Review(int r, String c){
this.rating = r;
this.comment = c;
}
public int getRating(){
return rating;
}
public String getComment(){
return comment;
}
}
public class ReviewAnalysis{
private Review[] allReviews = {new Review(4, "Good! Thx"), new Review(3, "OK site"),new Review(5, "Great!"),new Review(2, "Poor! Bad."),new Review(3, "")};
public ReviewAnalysis(){
this.allReviews = allReviews;
}
public double getAverageRating(){
double sum = 0.0;
for(int i = 0; i < allReviews.length; i++){
sum += allReviews[i].getRating();
}
return (sum/allReviews.length);
}
public ArrayList<String> collectComments(){
ArrayList<String> comments = new ArrayList<String>();
for(int i = 0; i < allReviews.length; i++){
String userCommend = allReviews[i].getComment();
if (userCommend.indexOf("!") != -1){
if(userCommend.substring(userCommend.length() -1, userCommend.length()).equals("!") || userCommend.substring(userCommend.length() -1, userCommend.length()).equals(".")){
comments.add(i + "-" + userCommend);
}
else {
comments.add(i + "-" + userCommend + ".");
}
}
}
return comments;
}
public static void main(String[] str){
ReviewAnalysis tester = new ReviewAnalysis();
System.out.println(tester.getAverageRating());
System.out.println(tester.collectComments());
}
}
ReviewAnalysis.main(null);
public class Data{
public static final int MAX = (int)(Math.random() * (Integer.MAX_VALUE+1));;
private int[][] grid;
public Data(){
this.grid = new int[5][5];
}
public void repopulate(){
for(int r = 0; r < grid.length; r++){
for(int c = 0; c < grid[0].length; c++){
int random = (int)(Math.random() * (MAX+1));
while((random % 100) == 0){
random = (int)(Math.random() * (MAX+1));
}
if(random % 10 !=0){
random = (random/10) *10;
}
grid[r][c] = random;
}
}
}
public int countIncreasingCols(){
int counter = 0;
for(int c = 0; c < grid.length; c++){
boolean isIncreasing = true;
for(int r = 0; r < grid[0].length-1; r++){
if(grid[r][c] > grid[r+1][c]){
isIncreasing = false;
}
}
if(isIncreasing){
counter++;
}
}
return counter;
}
public void printGrid(){
for(int[] list : grid){
for(int num : list){
System.out.print(num);
}
System.out.println();
}
}
public static void main(String[] str){
Data tester = new Data();
tester.repopulate();
tester.printGrid();
System.out.println(tester.countIncreasingCols());
}
}
Data.main(null);