Unformatted text preview:

Slide 1Object-based ScanningUpper case printingUpper case printingPrinting Scanned Characters Forwards and BackwardsSolutionSolution (contd.)Two Scanning ProblemsNo reuse in Monolithic SolutionsDivision of Labor in Character ScanningIndex-Based Interface?Index-based InterfaceImplementation with Array PropertyBetter Scanning Interface?Division of Labor in Radio ScanningSlide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Division of Labor in Character ScanningUsing Java Scanning LibrariesConstructor parametersDivision of Labor in I/OBufferedReader OperationsScanner Interface?Scanner Interface?Uppercase Scanner Interface?Multiple Iterator InterfacesUsing an Iterator InterfaceRedoing UpperCasePrinterImplementing ScannerSlide 37Slide 38Indexing vs. IterationThree Approaches to Reusable ScannerIndexing InterfaceStoring IteratorIterator that does not storeData Structure: Scanned StringData Structure: Markernext ()next ()next ()next ()next ()movePastCurrentToken()skipNonTokenCharacters()skipNonTokenCharacters()InitializationInitializationhasNext()hasNext()hasNext()hasNext()Complete ScannerScanner PatternMultiple Token TypesDivision of Labor in Character ScanningForward PrintingForward and Reverse PrintingUpperCasePrinterForward and reverse printingForward and reverse printingForward and reverse printing (contd.)Reuse of IteratorNo reuse in Monolithic SolutionsNeed Reuse?Iterator vs. ScanningIterator without ScanningIterating all Uppercase LettersIterating all Uppercase LettersComparing Two ImplementationsRadically Different BehaviorCan Write Code Reusing Polymorphic CodeNames Do Not MatterDefinition of IteratorDesign PatternSlide 83Architectural PatternScanning, Iterator, StreamsExtra SlidesSlide 87COMP 401ITERATOR DESIGN PATTERNInstructor: Prasun Dewan2OBJECT-BASED SCANNINGRedo the scanning solutionMonolithic, single class solutiondoes UI and scanningNo reuse of codeAnother scanning problem to show reuseIllustrate streams and iterator design pattern3UPPER CASE PRINTING4UPPER CASE PRINTINGpackage warmup;public class AnUpperCasePrinter { public static void main(String[] args){// assume user enters arguments correctlySystem.out.println("Upper Case Letters:");int index = 0;while (index < args[0].length()) {if (Character.isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index));index++;}System.out.println(); }5PRINTING SCANNED CHARACTERS FORWARDS AND BACKWARDSSolution cannot scan multiple timesSolution cannot scan multiple times6SOLUTIONpackage warmup;public class AReverseUpperCasePrinter {static final int MAX_CHARS = 5;static char[] upperCaseLetters = new char[MAX_CHARS];static int numberOfUpperCaseLetters = 0;public static void main(String[] args){int index = 0;System.out.println("Upper Case Letters:");while (index < args[0].length()) {if (isUpperCase(args[0].charAt(index))) {System.out.print(args[0].charAt(index));storeChar(args[0].charAt(index));}index++;}System.out.println();printReverse();}7SOLUTION (CONTD.)public static void storeChar(char c) {if (numberOfUpperCaseLetters == MAX_CHARS) {System.out.println("Too many upper case letters. Terminating program. ");System.exit(-1);}upperCaseLetters[numberOfUpperCaseLetters] = c;numberOfUpperCaseLetters++;}public static void printReverse() {System.out.println("Upper Case Letters in Reverse:");for (int index =numberOfUpperCaseLetters - 1; index >= 0; index--) {System.out.print(upperCaseLetters[index]);}System.out.println();}}8TWO SCANNING PROBLEMSOne UI is a subset of the otherOne UI is a subset of the otherWhat about the code?What about the code?9int index = 0;System.out.println("Upper Case Letters :");//token processingwhile (index < args[0].length()) { if (Character.isUpperCase(args[0].charAt(index);)) { System.out.print(args[0].charAt(index);); // token processing storeChar(args[0].charAt(index)); } index++;}NO REUSE IN MONOLITHIC SOLUTIONSint index = 0;System.out.println("Upper Case Letters :");//token processingwhile (index < args[0].length()) { if (Character.isUpperCase(args[0].charAt(index);)) System.out.print(args[0].charAt(index)); // token processing index++;}Only differenceOnly differenceHow to put it in separate class that only scans and does not store?How to put it in separate class that only scans and does not store?10DIVISION OF LABOR IN CHARACTER SCANNINGMain class that processes tokens (prints and/or stores tokens)Scanner class that produces tokens11INDEX-BASED INTERFACE?Main class that processes tokens (prints and/or stores tokens)AnIndexedBasedScanner12INDEX-BASED INTERFACEpublic interface IndexBasedScanner {public String[] getTokenArray () ;public int getNumberOfTokens () ;}13IMPLEMENTATION WITH ARRAY PROPERTYpublic class AnIndexBasedScanner implements IndexBasedScanner {static final int MAX_CHARS = 5;String[] upperCaseLetters;int numberOfUpperCaseLetters;public AnIndexBasedScanner(String theScannedString) {scan(theScannerString);}void scan (String theScannedString) { // store all scanned tokens in upperCaseLetters…}public String[] getTokenArray () {return upperCaseLetters;}public int getNumberOfTokens {return numberOfUpperCaseLetters;} } All tokens scanned even if only some may be used by code processing the tokens.14BETTER SCANNING INTERFACE?Main class that processes tokens (prints and/or stores tokens)Scanner class that produces tokensAnalogy with Division of Labor in Radio Station Scanning15 DIVISION OF LABOR IN RADIO SCANNING16 DIVISION OF LABOR IN RADIO SCANNING17 DIVISION OF LABOR IN RADIO SCANNING18 DIVISION OF LABOR IN RADIO SCANNING19 DIVISION OF LABOR IN RADIO SCANNING20 DIVISION OF LABOR IN RADIO SCANNING21 DIVISION OF LABOR IN RADIO SCANNING22 ?DIVISION OF LABOR IN RADIO SCANNING23 DIVISION OF LABOR IN RADIO SCANNING24 ANALOGOUS DIVISION OF LABORMain class that processes tokens (prints and/or stores tokens)Scanner class that produces tokens25DIVISION OF LABOR IN CHARACTER SCANNINGMain class that processes tokens (prints and/or stores tokens)Scanner class that produces tokensScanner objectInstance ofUses: Invokes methods inIntantiatesJava input libraries illustrate this division of labor in scanningJava input libraries illustrate this division of labor in scanning26USING JAVA SCANNING LIBRARIESBufferedReader dataIn = new BufferedReader (new InputStreamReader( System.in));int product = 1;// add input list terminated by a negative numberwhile (true) { int num = Integer.parseInt (dataIn.readLine()); if (num < 0) break; product =


View Full Document

UNC-Chapel Hill COMP 114 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?