scanning n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Scanning PowerPoint Presentation
Download Presentation
Scanning

Loading in 2 Seconds...

play fullscreen
1 / 87

Scanning - PowerPoint PPT Presentation


  • 125 Views
  • Uploaded on

Scanning. Scanning image for text. Scanning frequencies for radio stations. Finding words in a sentence Finding identifiers, operators, in a program char String Variable-size type Indexing Enumeration. Primitive Types. int, double, boolean, long, short, float, byte char.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Scanning


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
scanning
Scanning
  • Scanning image for text.
  • Scanning frequencies for radio stations.
  • Finding words in a sentence
  • Finding identifiers, operators, in a program
  • char
  • String
  • Variable-size type
    • Indexing
    • Enumeration
primitive types
Primitive Types
  • int, double, boolean, long, short, float, byte
  • char
primitive types1
Primitive Types
  • Constants (Literals & Named Constants)
  • Operations with Invocation Syntax
char constants

16 bits

char

Escape sequence

newline

char Constants

{letters, digits, operators ...}

‘\n’

‘a’

‘1’

‘’’

‘\’

‘ ‘

‘’

‘\’’

‘A‘

‘< ‘

‘\\’

ordering characters

ordinal number (integer code)

Ordering Characters

‘’

….

‘a’

….

position in ordered character list

ordering characters1

‘’

….

‘a’

‘b’

‘c’

….

‘z’

….

‘’

‘’

….

….

‘0’

‘A’

‘B’

‘1’

‘2’

‘C’

….

….

‘Z’

‘3’

….

….

Ordering Characters

‘a’ > ‘b’

 false

‘a’ > ‘A’

 ???

‘a’ > ‘0’

 ???

‘B’ > ‘A’

 true

‘4’ > ‘0’

 true

‘0’ > ‘’

 true

converting between characters and their ordinal numbers

Implicit cast to wider type

Converting between Characters and their Ordinal Numbers

(int) ‘a’

 ordinal number of ’a’

(char) 55

 character whose ordinal number is 55

(int) ‘c’ - (int) ‘a’

 2

(int) ‘’

 0

‘c’ - ‘a’

 2

(char) 0

 ‘’

(int) ‘d’

 ???

(char) (‘c’ - 2)

 ‘a’

(char) 1

 ???

(char) (‘A’ + 2)

 ‘C’

(char) -1

(char) (‘C’ - ‘A’ + ‘a’)

 ‘c’

a useful character operation
A Useful Character Operation

Character.isLetter(c)

 true if c is a letter

Character.isLetter(‘a’)

 true

Character.isLetter(‘A’)

 true

Character.isLetter(‘1’)

 false

Character.isLetter(‘ ’)

 false

string constants

variable size

Object Type

String constants

String

{sequences of characters}

“hello\n\n123”

“hello”

“hello 123”

‘a’

“”

“123”

“a”

“\”

“\\”

accessing string components

index

StringIndexBounds exceptiom

Accessing String Components

String s = “hello world”;

s.getFirstChar()

s.charAt(0)

 ‘h’

s.charAt(1)

 ‘e’

s.getSecondChar()

s.charAt(-1)

...

s.charAt(11)

s.length()

 11

 1

“ ”.length()

“”.length()

 0

accessing substring

StringIndexBounds exceptiom

Accessing SubString

public String substring (int beginIndex, int endIndex)

s.substring(beginIndex, endIndex)

 s.charAt(beginIndex) .. s.charAt(endIndex-1)

“hello world”.substring(4,7)

 “o w”

“hello world”.substring(4,4)

 “”

“hello world”.substring(7,4)

changing strings

three different instances

Changing Strings?

Strings are read-only (immutable)

“hello” + “world”

 “hello world”

useful string operations
Useful String Operations

s.toLowerCase()

 copy of s with letters converted to lower case

s.toUpperCase()

 copy of s with letters converted to upper case

“Hello World”.toLowerCase()

 “hello world”

“Hello World”.toUpperCase()

 “HELLO WORLD”

string processing
String Processing
  • int i = 0;
  • while (i < s.length()) {
  • System.out.println (s.charAt(i));
  • i++;
  • }

prints each character on separate line

dissecting a loop

loop condition

loop body

Dissecting a Loop
  • int i = 0;
  • while (i < s.length()) {
  • System.out.println (s.charAt(i));
  • i++;
  • }
finer grained dissection

initalizing loop variables

loop condition

real body

Resetting loop variable

Finer-grained Dissection
  • int i = 0;
  • while (i < s.length()) {
  • System.out.println (s.charAt(i));
  • i++;
  • }
  • for (int i=0; i<s.length(); i++)
  • System.out.println(s.charAt(i));
meaning of for loop
Meaning of For Loop

for (S1; E; S2)

S3

  • for (; E; S2)
    • S3
  • for (; E; )
    • S3
  • for (; ; )
    • S3

S1;

while ( E) {

S3;

S2;

}

while ( E) {

S3;

S2;

}

while ( E) {

S3;

}

while ( true)

S3;

scanning1

token

token

Multi-line

input stream

Line

stream

Line 1

Line 2

J

o

h

n

F

.

K

e

n

n

e

d

y

token

token

token

Scanning

Input

stream

Token

Stream

solution
Solution
  • Monolithic Solution
  • Class Decomposition
algorithm
Algorithm

String inputLine

algorithm1

marker

0

Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

Output: J

algorithm2
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

1

Output: J

algorithm3
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

2

Output: J

algorithm4
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

5

Output: JF

algorithm5
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

6

Output: JF

algorithm6
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

8

Output: JFK

algorithm7
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

9

Output: JFK

algorithm8
Algorithm

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

14

Output: JFK

monolithic solution

public class UpperCasePrinter {

  • publicstaticvoid main (String args[]) {
  • String input = getInput();
  • int index = 0;
  • System.out.println("Upper Case Letters :");
  • while (index < input.length()) {
  • char nextChar = input.charAt(index);
  • if (Character.isUpperCase(nextChar))
  • System.out.print(nextChar); // token processing
  • index++;
  • }
  • }
  • publicstatic String getInput() {
  • System.out.println("Please enter a string");
  • return Keyboard.readLine();
  • }
  • }
Monolithic Solution
storing instead of printing tokens
Storing instead of printing tokens

String inputLine

J

o

h

n

F

.

K

e

n

n

e

d

y

marker

14

String s = “JFK”;

no reuse in monolithic solutions
No reuse in Monolithic Solutions

String s = ""; // token processing

int index = 0;

while (index < input.length()) {

char nextChar = input.charAt(index);

if (Character.isUpperCase(nextChar))

s += nextChar; // token processing

index++;

}

int index = 0;

System.out.println("Upper Case Letters :");//token processing

while (index < input.length()) {

char nextChar = input.charAt(index);

if (Character.isUpperCase(nextChar))

System.out.print(nextChar); // token processing

index++;

}

slide44

Class Decomposition

instantiate

Scanner Class

Scanner Object

calls

Scanner User

Main Class

(Input & Output)

slide45

Class Decomposition

instantiate

DataInputStream Instance

DataInputStream

readLine()

Scanner User

Main Class

(Input & Output)

scanner user scanner object interaction
Scanner User-Scanner Object Interaction
  • DataInputStream dataIn = new DataInputStream (System.in);
  • int product = 1;
  • while (true) {
    • int num = Integer.parseInt (dataIn.readLine());
    • if (num < 0) break;
    • product = product*num;
  • }
  • System.out.println (product);
datainputstream operations
DataInputStream Operations

Multi-line

input stream

Line

stream

Line 1

Line 2

dataIn.readLine()

Line 1

dataIn.readLine()

Line 2

dataIn.readLine()

IOException

scanner interface
Scanner Interface?

Input

stream

Token

Stream

token 1

token 2

scanner.nextElement()

token 1

scanner.nextElement()

token 2

scanner.nextElement()

ScannerException

scanner interface1
Scanner Interface?

Input

stream

Token

Stream

token 1

token 2

scanner.nextElement()

token 1

scanner.nextElement()

token 2

scanner.hasMoreElements()

false

scanner.nextElement()

???

uppercase scanner interface

J

o

h

n

F

.

K

e

n

n

e

d

y

token 1

token 2

token 3

Uppercase Scanner Interface?

scanner.nextElement()

‘J’

scanner.nextElement()

‘F’

scanner.nextElement()

‘K’

scanner.hasMoreElements()

false

scanner.nextElement()

???

enumeration interfaces
Enumeration Interfaces
  • public interface CharEnumeration {
    • publicchar nextElement();
    • publicboolean hasMoreElements();
  • }
  • public interface StringEnumeration {
    • publicString nextElement();
    • publicboolean hasMoreElements();
  • }
  • public interface <Type>Enumeration {
    • public <Type> nextElement();
    • publicboolean hasMoreElements();
  • }
using an enumeration interface

J

o

h

n

F

.

K

e

n

n

e

d

y

token 1

token 2

token 3

Using an Enumeration Interface

publicstaticvoid printChars (CharEnumeration charEnumeration) {

while (charEnumeration.hasMoreElements())

System.out.print(charEnumeration.nextElement());

}

using an enumeration interface1
Using an Enumeration Interface

public class UpperCasePrinter {

publicstaticvoid main (String args[]) {

String input = getInput();

printUpperCase(input);

}

publicstatic String getInput() {

System.out.println("Please enter a string");

return Keyboard.readLine();

}

publicstaticvoid printUpperCase(String s) {

System.out.println("Upper Case Letters:");

printChars (new AnUpperCaseEnumeration(s));

}

publicstaticvoid printChars (CharEnumeration charEnumeration) {

while (charEnumeration.hasMoreElements())

System.out.print(charEnumeration.nextElement());

}

}

implementing scanner
Implementing Scanner
  • public class AnUpperCaseEnumeration implements CharEnumeration {
      • public AnUpperCaseEnumeration(String theString) {
      • ...
      • }
      • publicboolean hasMoreElements() {
      • ...
      • }
      • publicchar nextElement() {
      • ...;
      • }
  • }
data structure scanned string
Data Structure: Scanned String
  • public class AnUpperCaseEnumeration implements CharEnumeration {
      • String string;
      • public AnUpperCaseEnumeration(String theString) {
      • string = theString;
      • ...
      • }
      • publicboolean hasMoreElements() {
      • ...
      • }
      • publicchar nextElement() {
      • ...;
      • }
  • }
data structure marker
Data Structure: marker

string

J

o

h

n

F

.

K

e

n

n

e

d

y

scanned part

Unscanned part

nextElementPos

hasmoreelements

true

hasMoreElements()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

  • publicboolean hasMoreElements() {
      • ...
  • }
hasmoreelements1

true

hasMoreElements()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

  • publicboolean hasMoreElements() {
      • ...
  • }
hasmoreelements2

false

hasMoreElements()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

  • publicboolean hasMoreElements() {
      • ...
  • }
hasmoreelements3
hasMoreElements()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

  • //return true if nextElementPos is beyond end of string; false otherwise
  • publicboolean hasMoreElements() {
      • ...
  • }
hasmoreelements edited
hasMoreElements() (edited)

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

  • //return false if nextElementPos is beyond end of string; true otherwise
  • publicboolean hasMoreElements() {
      • return string.length() > nextElementPos;
  • }
hasmoreelements4
hasMoreElements()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

  • //return false if nextElementPos is beyond end of string; true otherwise
  • publicboolean hasMoreElements() {
      • return nextElementPos < string.length();
  • }
nextelement
nextElement()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

publicchar nextElement() {

}

nextelement1
nextElement()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

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

publicchar nextElements() {

}

nextelement2

Unexecuted Loop

char retVal = string.charAt(nextElemPos)

return retVal;

nextElement()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

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

publicchar nextElements() {

}

  • while (!Character.isUpperCase(string.charAt(nextElementPos)))

nextElemPos++;

nextelement3

char retVal = extractToken();

return retVal;

nextElement()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

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

publicchar nextElements() {

}

movePastCurrentToken();

skipNonTokenCharacters();

nextelement4
nextElement()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

//Assume nextElemPos is at start of next token or end of string when //method is called.

//returns next token if one exists

publicchar extractToken() {

}

return string.charAt(nextElementPos);

movepastcurrenttoken
movePastCurrentToken()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

//Assume nextElemPos is at start of next token or end of string when //method is called.

//Moves past current token.

publicvoid movePastCurrentToken() {

}

nextElemPos++;

skipnontokencharacters

StringIndexOutOfBounds

skipNonTokenCharacters()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

// keep advancing nextElementPos until we hit the next upper case

publicvoid skipNonTokenCharacters() {

}

  • while (!Character.isUpperCase(string.charAt(nextElementPos)))

nextElemPos++;

skipnontokencharacters1

short-circuit

StringIndexOutOfBounds?

skipNonTokenCharacters()

string

J

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

// keep advancing nextElementPos until we hit the next upper case or go

// beyond the end of the string.

publicvoid skipNonTokenCharacters() {

}

  • while (nextElementPos < string.length() && !Character.isUpperCase(string.charAt(nextElementPos)))

nextElemPos++;

initialization
Initialization

string

j

o

h

n

F

.

K

e

n

n

e

d

y

nextElementPos

String string;

int nextElementPos = 0;

public AnUpperCaseEnumeration(String theString) {

}

string = theString;

skipNonTokenCharacters();

complete scanner
Complete Scanner

public class AnUpperCaseEnumeration implements CharEnumeration {

String string;

int nextElementPos = 0;

public AnUpperCaseEnumeration(String theString) {

string = theString;

skipNonTokenCharacters();

}

publicboolean hasMoreElements() { return nextElementPos < string.length();}

publicchar nextElement() {

char retVal = extractToken();

movePastCurrentToken();

skipNonTokenCharacters();

return retVal;

}

void movePastCurrentToken() {nextElementPos++;}

void skipNonTokenCharacters() {

while (nextElementPos < string.length() && !Character.isUpperCase(string.charAt(nextElementPos)))

nextElementPos++;

}

char extractToken() { return string.charAt(nextElementPos);}

}

scanner pattern
Scanner Pattern

public class <Scanner Name> implements <T>Enumeration {

String string;

int nextElementStart = 0;

int nextElementEnd = ???;

public AnUpperCaseEnumeration(String theString) {

string = theString;

skipNonTokenCharacters();

}

publicboolean hasMoreElements() { return nextElementStart < string.length();}

public <T> nextElement() {

<T> retVal = extractToken(tokenLength);

movePastCurrentToken(tokenLength);

skipNonTokenCharacters();

return retVal;

}

void movePastCurrentToken() {…};

void skipNonTokenCharacters() {…};

char extractToken() { …}

}

scanner user again
Scanner User Again

public class UpperCasePrinter {

publicstaticvoid main (String args[]) {

String input = getInput();

printUpperCase(input);

}

publicstatic String getInput() {

System.out.println("Please enter a string");

return Keyboard.readLine();

}

publicstaticvoid printUpperCase(String s) {

System.out.println("Upper Case Letters:");

printChars (new AnUpperCaseEnumeration(s));

}

publicstaticvoid printChars (CharEnumeration charEnumeration) {

while (charEnumeration.hasMoreElements())

System.out.print(charEnumeration.nextElement());

}

}

monolithic solution1

public class UpperCasePrinter {

  • publicstaticvoid main (String args[]) {
  • String input = getInput();
  • int index = 0;
  • System.out.println("Upper Case Letters :");
  • while (index < input.length()) {
  • char nextChar = input.charAt(index);
  • if (Character.isUpperCase(nextChar))
  • System.out.print(nextChar); // token processing
  • index++;
  • }
  • }
  • publicstatic String getInput() {
  • System.out.println("Please enter a string");
  • return Keyboard.readLine();
  • }
  • }
Monolithic Solution
slide76

Class Decomposition

instantiate

AnUpperCaseEnumeration instance

AnUpperCaseEnumeration

AnUppercasePrinter

slide77

Class Decomposition

instantiate

AnUpperCaseEnumeration instance

AnUpperCaseEnumeration

AnUppercaseConcatenator

reuse of enumeration
Reuse of Enumeration
  • String s = "";
  • while (charEnumeration.hasMoreElements()) {
  • s += charEnumeration.nextElement();
  • }

while (charEnumeration.hasMoreElements())

System.out.print(charEnumeration.nextElement());

monolithic solution2
Monolithic Solution

String s = ""; // token processing

int index = 0;

while (index < input.length()) {

char nextChar = input.charAt(index);

if (Character.isUpperCase(nextChar))

s += nextChar; // token processing

index++;

}

int index = 0;

System.out.println("Upper Case Letters :");//token processing

while (index < input.length()) {

char nextChar = input.charAt(index);

if (Character.isUpperCase(nextChar))

System.out.print(nextChar); // token processing

index++;

}

monolithic solution to readline
Monolithic Solution to readLine()
  • initialize
  • while there is more input
  • set next line;
  • user code to process next line
  • ….
  • move next line markers
enumeration vs scanning

J

o

h

n

F

.

K

e

n

n

e

d

y

token 1

token 2

token 3

Enumeration Vs Scanning
  • public interface CharEnumeration {
    • publicchar nextElement();
    • publicboolean hasMoreElements();
  • }
enumeration without scanning
Enumeration without Scanning

‘A’

‘B’

...

‘Z’

AllUppercaseLettersInOrder

implements

  • public interface CharEnumeration {
    • publicchar nextElement();
    • publicboolean hasMoreElements();
  • }
enumerating all uppercase letters
Enumerating all Uppercase Letters

//instance variables

???

publicchar nextElement() {

???

}

publicboolean hasMoreElements() {

????

}

enumerating all uppercase letters edited
Enumerating all Uppercase Letters (edited)

//instance variables

???

publicchar nextElement() {

???

}

publicboolean hasMoreElements() {

????

}

enumerating all uppercase letters1
Enumerating all Uppercase Letters
  • publicclass AllUpperCaseLettersInOrder implements CharEnumeration {
  • char nextLetter = 'A';
  • publicboolean hasMoreElements() {
  • return nextLetter <= 'Z';
  • }
  • publicchar nextElement() {
  • char retVal = nextLetter;
  • nextLetter = (char) (nextLetter + 1);
  • return retVal;
  • }
  • }
comparing two implementations

J

o

h

n

F

.

K

e

n

n

e

d

y

token 1

token 2

token 3

Comparing Two Implementations

‘J’

‘F’

‘K’

AnUpperCaseEnumeration

implements

  • public interface CharEnumeration {
    • publicchar nextElement();
    • publicboolean hasMoreElements();
  • }
radically different behaviors

Polymorphism

print (new AnUppercaseEnumeration(s));

print (new AllUppercaseLettersInOrder());

Radically Different Behaviors

‘A’

‘B’

...

‘Z’

AllUppercaseLettersInOrder

implements

  • public interface CharEnumeration {
    • publicchar nextElement();
    • publicboolean hasMoreElements();
  • }

Syntactic Specification!