Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and...
-
Upload
marcus-harris -
Category
Documents
-
view
226 -
download
0
Transcript of Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and...
![Page 1: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/1.jpg)
Object-based Scanning
• Redo the scanning solutions
• Monolithic, single class solutions– does UI and scanning
• No reuse of code
![Page 2: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/2.jpg)
Upper case printingpackage 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'); }}
![Page 3: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/3.jpg)
Forward and reverse printingpackage 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();
}
![Page 4: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/4.jpg)
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]);}
}}
![Page 5: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/5.jpg)
No reuse in Monolithic Solutions
int 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++;}
int 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++;}
![Page 6: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/6.jpg)
Class Decomposition?
Main Class
![Page 7: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/7.jpg)
Division of Labor in Radio Scanning
![Page 8: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/8.jpg)
Division of Labor in Radio Scanning
![Page 9: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/9.jpg)
Division of Labor in Radio Scanning
![Page 10: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/10.jpg)
Division of Labor in Radio Scanning
![Page 11: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/11.jpg)
Division of Labor in Radio Scanning
![Page 12: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/12.jpg)
Division of Labor in Radio Scanning
![Page 13: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/13.jpg)
Division of Labor in Radio Scanning
![Page 14: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/14.jpg)
?
Division of Labor in Radio Scanning
![Page 15: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/15.jpg)
Division of Labor in Radio Scanning
![Page 16: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/16.jpg)
Scanner Object
calls
Scanner User
Main Class(Input & Output)
Class Decomposition
Scanner Class
instantiate
![Page 17: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/17.jpg)
BufferedReader Instance
readLine()
Scanner User
Main Class(Input & Output)
Class Decomposition
BufferedReader
instantiate
![Page 18: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/18.jpg)
Scanner User-Scanner Object Interaction
BufferedReader 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 = product*num;
}System.out.println (product);
![Page 19: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/19.jpg)
Constructor parameters
• InputStreamReader takes System.in as parameter.
• BufferedReader takes InputStreamReader as parameter.
• In general, scanner takes object producing scanned stream as parameter.
![Page 20: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/20.jpg)
BufferedReader Operations
Line 1 Line 2
Multi-lineinput stream
Linestream
dataIn.readLine() Line 1
dataIn.readLine() Line 2
dataIn.readLine() IOException
![Page 21: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/21.jpg)
Scanner Interface?
scanner.nextElement() token 1
scanner.nextElement() token 2
scanner.nextElement() ScannerException
token 1 token 2
Inputstream
TokenStream
![Page 22: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/22.jpg)
Scanner Interface?
scanner.nextElement() token 1
scanner.nextElement() token 2
scanner.hasMoreElements() false
Inputstream
TokenStream
token 1 token 2
scanner.nextElement() ???
![Page 23: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/23.jpg)
Uppercase Scanner Interface?
scanner.nextElement() ‘J’
scanner.nextElement() ‘F’
scanner.hasMoreElements() false
J o h n F . K e n n d ye
token 1 token 2 token 3
scanner.nextElement() ‘K’
scanner.nextElement() ???
![Page 24: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/24.jpg)
Enumeration Interfacespackage enums;public interface CharEnumeration {
public char nextElement(); public boolean hasMoreElements();
}
package enums;public interface StringEnumeration {
public String nextElement(); public boolean hasMoreElements();
}
package <P>;public interface <Type>Enumeration {
public <Type> nextElement(); public boolean hasMoreElements();
}
![Page 25: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/25.jpg)
Using an Enumeration Interface
public static void printChars (CharEnumeration charEnumeration) { while (charEnumeration.hasMoreElements()) System.out.print(charEnumeration.nextElement()); }
J o h n F . K e n n d ye
token 1 token 2 token 3
![Page 26: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/26.jpg)
Using an Enumeration Interfacepackage 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()); } }
![Page 27: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/27.jpg)
Implementing Scanner
package enums;public class AnUpperCaseEnumeration implements CharEnumeration {
… public AnUpperCaseEnumeration(String theString) { ... } public boolean hasMoreElements() { ... } public char nextElement() { ...; } …
}
![Page 28: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/28.jpg)
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() { ...; } …
}
![Page 29: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/29.jpg)
Data Structure: marker
J o h n F . K e n n d ye
nextElementPos
string
scanned part Unscanned part
![Page 30: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/30.jpg)
hasMoreElements()
J o h n F . K e n n d ye
string
nextElementPos
public boolean hasMoreElements() { ...
}
true
![Page 31: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/31.jpg)
hasMoreElements()
J o h n F . K e n n d ye
string
nextElementPos
public boolean hasMoreElements() { ...
}
true
![Page 32: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/32.jpg)
hasMoreElements()
J o h n F . K e n n d ye
string
nextElementPos
public boolean hasMoreElements() { ...
}
false
![Page 33: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/33.jpg)
hasMoreElements()
J o h n F . K e n n d ye
string
nextElementPos
//return true if nextElementPos is beyond end of string; false otherwisepublic boolean hasMoreElements() {
...}
![Page 34: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/34.jpg)
hasMoreElements()
J o h n F . K e n n d ye
string
nextElementPos
//return false if nextElementPos is beyond end of string; true otherwisepublic boolean hasMoreElements() {
return nextElementPos < string.length();}
![Page 35: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/35.jpg)
nextElement()
J o h n F . K e n n d ye
string
nextElementPos
public char nextElement() {
}
![Page 36: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/36.jpg)
nextElement()
J o h n F . K e n n d ye
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 existspublic char nextElements() {
}
![Page 37: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/37.jpg)
nextElement()
J o h n F . K e n n d ye
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 existspublic char nextElement() {
}
char retVal = string.charAt(nextElemPos)
return retVal;
while (!isUpperCase(string.charAt(nextElementPos)))
nextElemPos++;
Unexecuted Loop
![Page 38: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/38.jpg)
nextElement()
J o h n F . K e n n d ye
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 existspublic char nextElements() {
}
movePastCurrentToken();char retVal = extractToken();
return retVal;skipNonTokenCharacters();
![Page 39: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/39.jpg)
nextElement()
J o h n F . K e n n d ye
string
nextElementPos
//Assume nextElemPos is at start of next token or end of string when //method is called. //returns next token if one existspublic char extractToken() {
}return string.charAt(nextElementPos);
![Page 40: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/40.jpg)
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++;
J o h n F . K e n n d ye
string
nextElementPos
![Page 41: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/41.jpg)
skipNonTokenCharacters()
J o h n F . K e n n d ye
string
nextElementPos
// keep advancing nextElementPos until we hit the next upper case public void skipNonTokenCharacters() {
}
while (! isUpperCase(string.charAt(nextElementPos)))
nextElemPos++;
StringIndexOutOfBounds
![Page 42: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/42.jpg)
skipNonTokenCharacters()
J o h n F . K e n n d ye
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(nextElementPos)))
nextElemPos++;
StringIndexOutOfBounds?short-circuit
![Page 43: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/43.jpg)
Initialization
j o h n F . K e n n d ye
string
nextElementPos
String string;int nextElementPos = 0;public AnUpperCaseEnumeration(String theString) {
}
string = theString;
skipNonTokenCharacters();
![Page 44: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/44.jpg)
Complete Scannerpackage 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
![Page 45: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/45.jpg)
Scanner Patternpackage <P>;public class <Scanner Name> implements <T>Enumeration { String string; int nextElementStart = 0; int nextElementEnd = ???; public AnUpperCaseEnumeration(String theString) { string = theString; skipNonTokenCharacters(); } public boolean hasMoreElements() { return nextElementStart < string.length();} public <T> nextElement() { <T> retVal = extractToken(tokenLength); movePastCurrentToken(tokenLength); skipNonTokenCharacters(); return retVal; } void movePastCurrentToken() {…}; void skipNonTokenCharacters() {…}; char extractToken() { …} }
![Page 46: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/46.jpg)
AnUpperCaseEnumeration instance
Class Decomposition
AnUpperCaseEnumeration
instantiate
AnUppercasePrinter
![Page 47: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/47.jpg)
AnUpperCaseEnumeration instance
Class Decomposition
AnUpperCaseEnumeration
instantiate
AReverseUpperCasePrinter
![Page 48: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/48.jpg)
Reuse of Enumeration
while (charEnumeration.hasMoreElements()) {char nextChar = charEnumeration.nextElement()); System.out.print(nextChar);
storeChar(nextChar);}
while (charEnumeration.hasMoreElements()) System.out.print(charEnumeration.nextElement());
![Page 49: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/49.jpg)
Enumeration Vs Scanning
public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements();
}
J o h n F . K e n n d ye
token 1 token 2 token 3
![Page 50: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/50.jpg)
Enumeration without Scanning
public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements();
}
AllUppercaseLettersInOrder
implements
‘A’ ‘B’ ... ‘Z’
![Page 51: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/51.jpg)
Enumerating all Uppercase Letters
public boolean hasMoreElements() { ????}
public char nextElement() { ???
}
//instance variables
???
![Page 52: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/52.jpg)
Enumerating all Uppercase Letterspackage 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; } }
![Page 53: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/53.jpg)
Comparing Two Implementations
public interface CharEnumeration { public char nextElement(); public boolean hasMoreElements();
}
J o h n F . K e n n d ye
token 1 token 2 token 3
AnUpperCaseEnumeration
implements
‘J’ ‘F’‘K’
![Page 54: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/54.jpg)
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!
![Page 55: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/55.jpg)
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();
}
![Page 56: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/56.jpg)
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(); }
![Page 57: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/57.jpg)
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]);
}}
}
![Page 58: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/58.jpg)
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();
}
![Page 59: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/59.jpg)
Remaining Problems
• Main class does storing
• Non UI code
• Array, size, and max size closely coupled to each other
• Should be in separate class
![Page 60: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/60.jpg)
3 J
F
K
size array
Variable-Size Collection
filled part
unfilled part
current size
maximum size
![Page 61: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/61.jpg)
Variable-Size Collectionpublic class <ClassNeedingVariableSizeTCollection> { …
final static T A_MAX_SIZE = 50;T[] a = new T [MAX_SIZE];int aSize = 0;…//process afor (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 …}
![Page 62: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/62.jpg)
Special Typepublic class <ClassNeedingVariableSizeCollection> { ... 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
![Page 63: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/63.jpg)
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
![Page 64: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/64.jpg)
Supporting Encapsulation
package collections;public interface CharHistory {
}
public void addElement (char element);
public int size();
public char elementAt (int index);
![Page 65: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/65.jpg)
Implementing the Historypackage 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++;}
} }
![Page 66: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/66.jpg)
Using the Historypackage 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();
}
![Page 67: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/67.jpg)
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(); }
![Page 68: Object-based Scanning Redo the scanning solutions Monolithic, single class solutions –does UI and scanning No reuse of code.](https://reader036.fdocuments.in/reader036/viewer/2022062301/5697c0071a28abf838cc61e4/html5/thumbnails/68.jpg)
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));}
}}