stringformatter
StringFormatter 2016 FRQ 4
import java.util.ArrayList; import java.util.List; /** * AP 2016 FRQ 4 * */ public class StringFormatter { public static int totalLetters( List<String> wordList ) { // add code here for part (a) } public static int basicGapWidth( List<String> wordList, int formattedLen ) { // add code here for part (b) } public static String format( List<String> wordList, int formattedLen ) { // add code here for part (c) } /** * Implementation of leftoverSpaces */ public static int leftoverSpaces(List<String> wordList, int formattedLen) { int spaces = formattedLen-totalLetters(wordList); int gaps = wordList.size()-1; int gapWidth = basicGapWidth(wordList,formattedLen); return spaces - gaps*gapWidth; } public static void testItWith(String[] words, int wordCount, int gaps, int leftOvers) { ArrayList<String> list = new ArrayList<String>(); for (String str:words){ list.add(str); System.out.print(str+ " "); } System.out.print("\nPart A: Total number of letters in words:"+wordCount+"\nyour code:"); System.out.println(totalLetters(list)); System.out.print("Part B Basic gap width: "+gaps+"\nyour code:"); System.out.println(basicGapWidth(list,20 )); System.out.print("Leftover spaces: "+leftOvers+"\nyourcode:"); System.out.println(leftoverSpaces(list,20)); String f = format(list,20); System.out.print("Part C format should have length 20:\nyour code:"); System.out.println(f.length()); System.out.println("\"" +f+ "\""); } public static void main(String[] args) { String[] words1 = {"AP", "COMP", "SCI", "ROCKS"}; String[] words2 = {"GREEN", "EGGS", "AND", "HAM"}; String[] words3 = {"BEACH", "BALL"}; String[] words4 = {"IT","IS", "OK!"}; testItWith(words1, 14, 2, 0); testItWith(words2, 15, 1, 2); testItWith(words3, 9, 11, 0); testItWith(words4, 7, 6, 1); } }
stringformatter.txt · Last modified: 2022/05/23 16:42 by frchris