= 'A') && (c <= 'Z'); }"> = 'A') && (c <= 'Z'); }">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.

Similar presentations


Presentation on theme: "Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code."— Presentation transcript:

1

2 Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code

3 Upper case printing package warmup; public class AnUpperCasePrinter { public static void main(String[] args){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } System.out.println("Upper Case Letters:"); int index = 0; while (index < args[0].length()) { if (isUpperCase(args[0].charAt(index))) System.out.print(args[0].charAt(index)); index++; } System.out.println(); } public static boolean isUpperCase(char c) { return (c >= 'A') && (c <= 'Z'); }

4 Forward and reverse printing package 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){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } 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(); }

5 Forward and reverse printing (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]); }

6 No reuse in Monolithic Solutions int index = 0; System.out.println("Upper Case Letters :");//token processing while (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 ++; } int index = 0; System.out.println("Upper Case Letters :");//token processing while (index < args[0].length()) { if (Character.isUpperCase(args[0].charAt(index);)) System.out.print(args[0].charAt(index)); // token processing index ++; }

7 Class Decomposition? Main Class

8 Division of Labor in Radio Scanning

9

10

11

12

13

14

15 ?

16

17 Scanner Object calls Scanner User Main Class (Input & Output) Class Decomposition Scanner Class instantiate

18 BufferedReader Instance readLine() Scanner User Main Class (Input & Output) Class Decomposition BufferedReader instantiate

19 Scanner User-Scanner Object Interaction BufferedReader dataIn = new BufferedReader (new InputStreamReader( System.in)); int product = 1; // add input list terminated by a negative number while (true) { int num = Integer.parseInt (dataIn.readLine()); if (num < 0) break; product = product*num; } System.out.println (product);

20 Constructor parameters InputStreamReader takes System.in as parameter. BufferedReader takes InputStreamReader as parameter. In general, scanner takes object producing scanned stream as parameter.

21 BufferedReader Operations Line 1Line 2 Multi-line input stream Line stream dataIn.readLine()Line 1 dataIn.readLine()Line 2 dataIn.readLine()IOException

22 Scanner Interface? scanner.nextElement()token 1 scanner.nextElement()token 2 scanner.nextElement()ScannerException token 1token 2 Input stream Token Stream

23 Scanner Interface? scanner.nextElement()token 1 scanner.nextElement()token 2 scanner.hasMoreElements()false Input stream Token Stream token 1token 2 scanner.nextElement()???

24 Uppercase Scanner Interface? scanner.nextElement()‘J’ scanner.nextElement()‘F’ scanner.hasMoreElements()false JohnF.Kenndye token 1token 2token 3 scanner.nextElement()‘K’ scanner.nextElement()???

25 Enumeration Interfaces package enums; public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements(); } package enums; public interface StringEnumeration { public String nextElement(); public boolean hasMoreElements(); } package ; public interface Enumeration { public nextElement(); public boolean hasMoreElements(); }

26 Using an Enumeration Interface public static void printChars (CharEnumeration charEnumeration) { while (charEnumeration.hasMoreElements()) System.out.print(charEnumeration.nextElement()); } JohnF.Kenndye token 1token 2token 3

27 Using an Enumeration Interface package main; import enums.CharEnumeration; import enums.AnUpperCaseEnumeration; public class UpperCasePrinter { public static void main (String args[]) { if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } printUpperCase(args[0]); } public static void printUpperCase(String s) { System.out.println("Upper Case Letters:"); printChars (new AnUpperCaseEnumeration(s)); } public static void printChars (CharEnumeration charEnumeration) { while (charEnumeration.hasMoreElements()) System.out.print(charEnumeration.nextElement()); }

28 Implementing Scanner package enums; public class AnUpperCaseEnumeration implements CharEnumeration { … public AnUpperCaseEnumeration(String theString) {... } public boolean hasMoreElements() {... } public char nextElement() {...; } … }

29 Data Structure: Scanned String package enums; public class AnUpperCaseEnumeration implements CharEnumeration { String string; … public AnUpperCaseEnumeration(String theString) { string = theString;... } public boolean hasMoreElements() {... } public char nextElement() {...; } … }

30 Data Structure: marker JohnF.Kenndye nextElementPos string scanned partUnscanned part

31 hasMoreElements() JohnF.Kenndye string nextElementPos public boolean hasMoreElements() {... } true

32 hasMoreElements() JohnF.Kenndye string nextElementPos public boolean hasMoreElements() {... } true

33 hasMoreElements() JohnF.Kenndye string nextElementPos public boolean hasMoreElements() {... } false

34 hasMoreElements() JohnF.Kenndye string nextElementPos //return true if nextElementPos is beyond end of string; false otherwise public boolean hasMoreElements() {... }

35 hasMoreElements() JohnF.Kenndye string nextElementPos //return false if nextElementPos is beyond end of string; true otherwise public boolean hasMoreElements() { return nextElementPos < string.length(); }

36 nextElement() JohnF.Kenndye string nextElementPos public char nextElement() { }

37 nextElement() JohnF.Kenndye string nextElementPos //Assume nextElemPos is at start of next token or end of string when //method is called. //Method makes sure this is also true before returning. //returns next element if one exists public char nextElements() { }

38 nextElement() JohnF.Kenndye string nextElementPos //Assume nextElemPos is at start of next token or end of string when //method is called. //Method makes sure this is also true before returning. //returns next element if one exists public char nextElement() { } char retVal = string.charAt(nextElemPos) return retVal; while (!isUpperCase(string.charAt(nextElementPos))) nextElemPos++; Unexecuted Loop

39 nextElement() JohnF.Kenndye string nextElementPos //Assume nextElemPos is at start of next token or end of string when //method is called. //Method makes sure this is also true before returning. //returns next element if one exists public char nextElements() { } movePastCurrentToken(); char retVal = extractToken(); return retVal; skipNonTokenCharacters();

40 nextElement() JohnF.Kenndye string nextElementPos //Assume nextElemPos is at start of next token or end of string when //method is called. //returns next token if one exists public char extractToken() { } return string.charAt(nextElementPos);

41 movePastCurrentToken() //Assume nextElemPos is at start of next token or end of string when //method is called. //Moves past current token. public void movePastCurrentToken() { } nextElemPos++; JohnF.Kenndye string nextElementPos

42 skipNonTokenCharacters() JohnF.Kenndye string nextElementPos // keep advancing nextElementPos until we hit the next upper case public void skipNonTokenCharacters() { } while (! isUpperCase(string.charAt(nextElementPos))) nextElemPos++; StringIndexOutOfBounds

43 skipNonTokenCharacters() JohnF.Kenndye string nextElementPos // keep advancing nextElementPos until we hit the next upper case or go // beyond the end of the string. public void skipNonTokenCharacters() { } while (nextElementPos < string.length() && !Character.isUpperCase(string.charAt(nextElement Pos))) nextElemPos++; StringIndexOutOfBounds?short-circuit

44 Initialization johnF.Kenndye string nextElementPos String string; int nextElementPos = 0; public AnUpperCaseEnumeration(String theString) { } string = theString; skipNonTokenCharacters();

45 Complete Scanner package enums; public class AnUpperCaseEnumeration implements CharEnumeration { String string; int nextElementPos = 0; public AnUpperCaseEnumeration(String theString) { string = theString; skipNonTokenCharacters(); } public boolean hasMoreElements() { return nextElementPos < string.length();} public char nextElement() { char retVal = extractToken(); movePastCurrentToken(); skipNonTokenCharacters(); return retVal; } void movePastCurrentToken() {nextElementPos++;} void skipNonTokenCharacters() { while (nextElementPos < string.length() && !isUpperCase(string.charAt(nextElementPos))) nextElementPos++; } char extractToken() { return string.charAt(nextElementPos);} boolean isUpperCase (char c) { return c >= ‘A’ && c <= ‘Z’;} } constructor

46 Scanner Pattern package ; public class implements Enumeration { String string; int nextElementStart = 0; int nextElementEnd = ???; public AnUpperCaseEnumeration(String theString) { string = theString; skipNonTokenCharacters(); } public boolean hasMoreElements() { return nextElementStart < string.length();} public nextElement() { retVal = extractToken(tokenLength); movePastCurrentToken(tokenLength); skipNonTokenCharacters(); return retVal; } void movePastCurrentToken() {…}; void skipNonTokenCharacters() {…}; char extractToken() { …} }

47 AnUpperCaseEnumeration instance Class Decomposition AnUpperCaseEnumeration instantiate AnUppercasePrinter

48 AnUpperCaseEnumeration instance Class Decomposition AnUpperCaseEnumeration instantiate AReverseUpperCasePrinter

49 Reuse of Enumeration while (charEnumeration.hasMoreElements()) { char nextChar = charEnumeration.nextElement()); System.out.print(nextChar); storeChar(nextChar); } while (charEnumeration.hasMoreElements()) System.out.print(charEnumeration.nextElement());

50 Enumeration Vs Scanning public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements(); } JohnF.Kenndye token 1token 2token 3

51 Enumeration without Scanning public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements(); } AllUppercaseLettersInOrder implements ‘A’‘B’...‘Z’

52 Enumerating all Uppercase Letters public boolean hasMoreElements() { ???? } public char nextElement() { ??? } //instance variables ???

53 Enumerating all Uppercase Letters package enumerations; public class AllUpperCaseLettersInOrder implements CharEnumeration { char nextLetter = 'A'; public boolean hasMoreElements() { return nextLetter <= 'Z'; } public char nextElement() { char retVal = nextLetter; nextLetter = (char) (nextLetter + 1); return retVal; }

54 Comparing Two Implementations public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements(); } JohnF.Kenndye token 1token 2token 3 AnUpperCaseEnumeration implements ‘J’‘F’‘K’

55 Radically Different Behaviors public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements(); } AllUppercaseLettersInOrder implements ‘A’‘B’...‘Z’ print (new AnUppercaseEnumeration(s)); print (new AllUppercaseLettersInOrder()); Polymorphism Syntactic Specification!

56 Forward and reverse printing package main; import enums.CharEnumeration; import enums.AnUpperCaseEnumeration; 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){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } printAndStore(args[0]); printReverse(); }

57 Forward and reverse printing public static void printAndStore (String s) { System.out.println("Upper Case Letters:"); printAndStore (new AnUpperCaseEnumeration(s)); } public static void printAndStore (CharEnumeration charEnumeration) { while (charEnumeration.hasMoreElements()) { char inputChar = charEnumeration.nextElement(); System.out.print (inputChar); storeChar(inputChar); } System.out.println(); }

58 Forward and reverse printing (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]); }

59 Remaining problems package main; import enums.CharEnumeration; import enums.AnUpperCaseEnumeration; 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){ if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } printAndStore(args[0]); printReverse(); }

60 Remaining Problems Main class does storing Non UI code Array, size, and max size closely coupled to each other Should be in separate class

61 3 J F K sizearray Variable-Size Collection filled part unfilled part current size maximum size

62 Variable-Size Collection public class { … final static T A_MAX_SIZE = 50; T[] a = new T [MAX_SIZE]; int aSize = 0; … //process a for (int index = 0; index < aSize; index++) System.out.println(a[index]); … final int B_MAX_SIZE = 50; T[] b = new T [MAX_SIZE]; int bSize = 0; //process b … }

63 Special Type public class {... AVariableSizeTCollection a, b = new AVariableSizeTCollection();... for (int index = 0; index < a.size; index++) System.out.println(a.contents[index]); … AVariableSizeTCollection b = new AVariableSizeTCollection();... } public class AVariableSizeTCollection { public static final int MAX_SIZE = ???; public T[] contents = new T [MAX_SIZE]; public int size = 0; } No encapsulation! a.size = 0; Constraints violated

64 Supporting Encapsulation package collections; public interface …. { public static final int MAX_SIZE = 50; } public void addElement (char element); public void printReverse (); Implementation specific User specific

65 Supporting Encapsulation package collections; public interface CharHistory { } public void addElement (char element); public int size(); public char elementAt (int index);

66 Implementing the History package collections; public class ACharHistory implements CharHistory { public final int MAX_SIZE = 5; char[] contents = new char[MAX_SIZE]; int size = 0; public int size() { return size;} public char elementAt (int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(char element) { if (isFull()) { System.out.println("Adding item to a full history. Terminating program."); System.exit(-1); } else { contents[size] = element; size++; }

67 Using the History package main; import enums.CharEnumeration; import enums.AnUpperCaseEnumeration; import collections.ACharHistory; import collections.CharHistory; public class AModularReverseUpperCasePrinter { static CharHistory upperCaseLetters = new ACharHistory(); public static void main(String[] args) { if (args.length != 1) { System.out.println("Illegal number of arguments:" + args.length + ". Terminating program."); System.exit(-1); } printAndStore(args[0]); printReverse(); }

68 Using the History public static void printAndStore (String s) { System.out.println("Upper Case Letters:"); printAndStore (new AnUpperCaseEnumeration(s)); } public static void printAndStore (CharEnumeration charEnumeration) { while (charEnumeration.hasMoreElements()) { char inputChar = charEnumeration.nextElement(); System.out.print (inputChar); upperCaseLetters.addElement(inputChar); } System.out.println(); }

69 Forward and reverse printing (contd.) public static void printReverse() { System.out.println("Upper Case Letters in Reverse:"); for (int index =upperCaseLetters.size() - 1; index >= 0; index--) { System.out.print(upperCaseLetters.elementAt(index)); }


Download ppt "Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code."

Similar presentations


Ads by Google