User Tools

Site Tools


stringformatter

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Last revisionBoth sides next revision
stringformatter [2022/05/23 15:29] frchrisstringformatter [2022/05/23 16:40] frchris
Line 1: Line 1:
 **StringFormatter** **StringFormatter**
 [[https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16_frq_computer_science_a.pdf|2016 FRQ 4]] [[https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap16_frq_computer_science_a.pdf|2016 FRQ 4]]
 +<code> 
 +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 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); 
 +    } 
 +    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+ "\"");  
 +         
 +    } 
 +
 +</code>
  
 [[https://mathorama.com/wiki/doku.php?id=starter_code_for_old_ap_questions|Back to AP Questions from other years]] [[https://mathorama.com/wiki/doku.php?id=starter_code_for_old_ap_questions|Back to AP Questions from other years]]
  
  
stringformatter.txt · Last modified: 2022/05/23 16:42 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki