반응형
AP Computer Science A [FRQ] Practice (4)
*FRQ 2012 1번 (ArrayList 관련 문제 => 현재의 3번 문제와 관련)
문제는 아래 "더보기" 클릭
FRQ 2012 1번 정답은 아래 "더보기" 클릭
더보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | //Question 1: Climbing Club // Part (a): public void addClimb(String peakName, int climbTime) { this.climbList.add(new ClimbInfo(peakName, climbTime)); } // Part (b): public void addClimb(String peakName, int climbTime) { for (int i = 0; i < this.climbList.size(); i++) { if (peakName.compareTo(this.climbList.get(i).getName()) <= 0) { this.climbList.add(i, new ClimbInfo(peakName, climbTime)); return; } } this.climbList.add(new ClimbInfo(peakName, climbTime)); } // Part (c): // NO // YES | cs |
*FRQ 2010 1번 (ArrayList 관련 문제 => 현재의 3번 문제와 관련)
문제는 아래 "더보기" 클릭
FRQ 2010 1번 정답은 아래 "더보기" 클릭
더보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | //Question 1: Master Order // Part (a): public int getTotalBoxes() { int sum = 0; for (CookieOrder co : this.orders) { sum += co.getNumBoxes(); } return sum; } // Part (b): public int removeVariety(String cookieVar) { int numBoxesRemoved = 0; for (int i = this.orders.size() - 1; i >= 0; i--) { if (cookieVar.equals(this.orders.get(i).getVariety())) { numBoxesRemoved += this.orders.get(i).getNumBoxes(); this.orders.remove(i); } } return numBoxesRemoved; } // Alternative solution (forward traversal direction): public int removeVariety(String cookieVar) { int numBoxesRemoved = 0; int i = 0; while (i < this.orders.size()) { if (cookieVar.equals(this.orders.get(i).getVariety())) { numBoxesRemoved += this.orders.get(i).getNumBoxes(); this.orders.remove(i); } else { i++; } } return numBoxesRemoved; } | cs |
*FRQ 2016 4번 (ArrayList 관련 문제 => 현재의 3번 문제와 관련)
문제는 아래 "더보기" 클릭
FRQ 2016 4번 정답은 아래 "더보기" 클릭
더보기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // Question 4: String Formatter // Part (a): public static int totalLetters(List<String> wordList) { int total = 0; for (String word : wordList) { total += word.length(); } return total; } // Part (b): public static int basicGapWidth(List<String> wordList, int formattedLen) { return (formattedLen – totalLetters(wordList)) / (wordList.size()-1); } // Part (c): public static String format(List<String> wordList, int formattedLen) { String formatted = ""; int gapWidth = basicGapWidth(wordList, formattedLen); int leftovers = leftoverSpaces(wordList, formattedLen); for (int w = 0; w < wordList.size() - 1; w++) { formatted = formatted + wordList.get(w); for (int i = 0; i < gapWidth; i++) { formatted = formatted + " "; } if (leftovers > 0) { formatted = formatted + " "; leftovers--; } } formatted = formatted + wordList.get(wordList.size() - 1); return formatted; } | cs |
728x90
반응형
'자바(Java) > 자바 AP' 카테고리의 다른 글
AP Computer Science A [FRQ] Practice (6) (0) | 2024.04.04 |
---|---|
AP Computer Science A [FRQ] Practice (5) (0) | 2024.04.04 |
AP Computer Science A [FRQ] Practice (3) (0) | 2024.04.04 |
AP Computer Science A [FRQ] Practice (2) (0) | 2024.04.04 |
AP Computer Science A [FRQ] Practice (1) (0) | 2024.04.04 |
댓글