OOP - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

OOP

Description:

BufferedReader keyb; String inputline; isr = new InputStreamReader(System.in) ... keyb = new BufferedReader(isr); System.out.println('Type in a word to pluralize ' ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 30
Provided by: K3D
Category:
Tags: oop | keyb

less

Transcript and Presenter's Notes

Title: OOP


1
OOPM the second day
In the beginning God created the heavens and
the earth - the Hollybook -
2
OOPM do you remember?
input
output
-joystick -mouse -keyboard -microph.
-joystick -screen -speakers -lights
human based input
human based output
-floppy -HD -CD -ZIP/JAZZ -
prev. rec. infor. as input
-floppy -HD -CD -ZIP/JAZZ -
infor. as output
3
OOPM do you remember?
monitor ? object
  • System.out is the predefined reference to the
    monitor in the PrintStream class

println( In winter Bjorn and Lisa eat potatoes.)
behavior
details
System.out.println( In winter Bjorn and Lisa eat
potatoes.)
reference to the receiver
message
4
OOPM do you remember?
file ? object
  • Floppies, HardDrives, ZIP disks, CDs contain
    information in the form of files.
  • Attributes of files
  • contents
  • file name
  • Java provides a predefined class for modeling
    disk files, called File

shopping.lst
SHOPPING potatis mjolk agg
FileOutputStream
Java program
bytes of data
ewsc24rfds53Hej20Hur20m76ar20du20?rsf
5
OOPM do you remember?
one LogMyDay example
import java.io. class LogMyDay public
static void main(String arg) throws Exception
PrintStream log
FileOutputStream logfileStream File
logfile logfile new
File(Bjorn_Lisa.log) logfileStream
new FileOutputStream(logfile) log new
PrintStream(logfileStream)
System.out.println(1630 Met at Triangeln )
log.println(1630 Met at Triangeln
)
6
OOPM the second day
And God saw that the light was good and God
separated the light from the darkness - the
Hollybook -
7
OOPM input / output to programs
8
OOPM input an overview
We have not yet talked about how to use
information coming from a keyboard or stored in a
file THE INPUT
This forces us to place all the information into
the programs themselves
  • Because of this our programs are
  • inefficient
  • non reusable

9
OOPM input an overview
Example we want to delete a file, then we need a
code that would look like the following
But with this we can only delete a file called
delete_me
Wouldnt it be much better if the program could
have as an input the name of the file to be
deleted?
So we could call it with a line like delete
filename
10
OOPM input an overview
Input source
Java program
bytes of data
ewsc24rfds53Hej20Hur20m76ar20du20?rsf
We must build a bridge between the Java Program
and the input source!!
11
OOPM input an overview
  • While describing the output we saw that write
    data to a file involved several stages
  • Create a File object
  • Create a FileOutputStream object
  • Create a PrintStream object

Input whether coming from the keyboard, from a
file, from the network or from another program
requires a similar sequence of stages
12
OOPM input an overview
  • This references could be of two types
  • FileInputStreamReader for files
  • BufferedInputStream for keyboard and the network
  • Construct a reference to an InputStream object,
    and with it
  • Construct an InputStreamReader object, and with
    it
  • Construct a BufferedReader object

Stages
  • Note that
  • InputStream inputs sequences of bytes into the
    program
  • InputStreamReader models the stream of input as
    characters, but doesnt recognize ends of line
    a.o.
  • BufferedReader has got a collection of methods
    that allow us to work in a similar way as we did
    with PrintStream

13
OOPM a board with keys
Java provides a predefined object to represent
the stream of input that comes from the keyboard.
System.in is a reference to this object contained
into the BufferedInputStream class
Unlike System.out, which refers to a PrintStream
object and therefore can be used right away to
write Strings to the monitor, System.in, a
reference to a BufferedInputStream object, cannot
be readily used to read Strings
keyboard
System.in
isr


existing InputStream
keyb


new InputStreamReader

new BufferedReader
14
OOPM a board with keys
Java provides System.in contained into the
BufferedInputStream class
the constructor for InputStreamReader accepts the
keyboard (a BufferedInputStream reference) as its
argument
new InputStreamReader (System.in)
the constructor for BufferedReader accepts a
InputStreamReader reference as its argument
new BufferedReader (ISR)
15
OOPM a board with keys
The total declaration would be
InputStreamReader isr BufferedReader
keyb isr new InputStreamReader(System.in)
keyb new BufferedReader(isr)
And now, for reading a line
InputStreamReader isr BufferedReader
keyb String inputline isr
new InputStreamReader(System.in) keyb new
BufferedReader(isr) inputline keyb.readLine()
.readLine() is a method from the BufferedReader
class
16
OOPM a board with keys
Example a program that writes the plural of a
word
import java.io. / This program writes the
plural of the word typed in the keyboard it
just adds an s / class plural public
static void main(String arg) throws Exception
InputStreamReader isr
BufferedReader keyb String
inputline isr new
InputStreamReader(System.in) keyb new
BufferedReader(isr) inputline
keyb.readLine() System.out.print(inputlin
e) System.out.println(s)
17
OOPM a problem interactivity
The keyboard involves directly to a human being,
often termed an ENDUSER
ENDUSERs are almost never the authors of the
programs that they use
ENDUSERs cannot be expected to automatically know
what to type on the keyboard and when to type it
A program that expects input from a keyboard must
provide some information to the users as it runs
It must display messages such as If you want me
to delete all your hard-drive, press Yes, in
other case, pray something!
18
OOPM a problem interactivity
This kind of messages that ask the user to do
something are called prompts
In order to improve the interactivity of our
plurals program, we should add a prompt like
isr new InputStreamReader(System.in) keyb
new BufferedReader(isr) System.out.println(Type
in a word to pluralize ) inputline
keyb.readLine() System.out.print(inputline) Syst
em.out.println(s)
19
OOPM a problem interactivity
But did you really think that it was going to
be so easy?!?
OF COURSE, IF OUR AIM WAS TO IMPROVE
INTERACTIVITY, THIS IS NOT GOING TO WORK
Murphys principle -
The reason resides in the behavior for the
System.out as it is defined in the PrintStream
class
A PrintStream object receives print and println
messages and will EVENTUALLY display the Strings
requested
20
OOPM a problem interactivity
But how does PrintStream work?!?
Imagine Bjorn having to cook a dinner for
friends. He wants to prepare some backed
potatoes. Does it have a sense to peel one potato
and back it and then continue with other one?
potatoes
oven
Bjorn will cook as many potatoes as possible at
once, because in other case he would be wasting
his time!!
21
OOPM a problem interactivity
But how does PrintStream work?!?
With the computer happens exactly the same
problem. There is a part of the memory that is
allocated for streaming operations. It is called
a buffer. There the computer stores as many
information as it cans, and shows it at once,
when it is full.
string3
string2
string1
buffer
But, there is a method that forces the strings to
be shown on the screen. .flush() forces the
computer to empty the buffer.
22
OOPM a problem interactivity
The use of this method will allow us the
improvement of interactivity
The buffer shall be flushed in order to show
the prompt to the user
isr new InputStreamReader(System.in) keyb
new BufferedReader(isr) System.out.println(Type
in a word to pluralize ) System.out.flush() inp
utline keyb.readLine() System.out.print(inputli
ne) System.out.println(s)
23
OOPM come IN PUT files
Obtaining input from disk files is only a little
bit more complicated than from the keyboard. Our
starting point must be to find some sort of
InputStream object. As we said before, both
BufferedInputStream and FileInputStream belong to
that class
System.in is already defined into the class
BufferedInputStream, the problem is that the
FileInputStream class does not have defined
objects like that. Therefore we need to first
define an object that makes reference to a file.
fsr

files
isr


new FileInputStream
bsr


new InputStreamReader

new BufferedReader
24
OOPM come IN PUT files
the constructor for FileInputStream accepts a
file (a File reference) as its argument
new FileInputStream (file)
the constructor for InputStreamReader accepts a
stream from a file (a FileInputStream reference)
as its argument
new InputStreamReader (FileInputStream)
the constructor for BufferedReader accepts a
InputStreamReader reference as its argument
new BufferedReader (ISR)
25
OOPM come IN PUT files
The total declaration would be
File f FileInputStream
fsr InputStreamReader isr BufferedReader
bsr f new File(Big_parties_many_potat
is.txt) fsr new FileInputStream(f) isr new
InputStreamReader(fsr) bsr new
BufferedReader(isr)
26
OOPM come IN PUT files
Lets read a couple of lines from the file and
print them
File f FileInputStream
fsr InputStreamReader isr BufferedReader
bsr String inputline f
new File(Big_parties_many_potatis.txt) fsr
new FileInputStream(f) isr new
InputStreamReader(fsr) bsr new
BufferedReader(isr) inputline
bsr.readLine() System.out.println(inputline) inp
utline bsr.readLine() System.out.println(inputl
ine)
27
OOPM mixin it up
  • Exercise
  • write a program that makes a copy of a file
  • the name of the copy will be the same than the
    original plus .copy
  • Hint consider the following flow of information
    for the program

Hint2 it is obvious that you need to read the
name of the file to be copied from the keyboard
28
OOPM mixin it up
FileInputStream
Some InputStream
InputStreamReader
InputStreamReader
BufferedReader
BufferedReader
CopyFile program
PrintStream
FileOutputStream
29
OOPM mixin it up
import java.io. class CopyFile public static
void main(String arg) throws Exception
InputStreamReader isrKeyboard BufferedReader
keyboard String fileNameOrig File
fOrig FileInputStream
fisOrig InputStreamReader isrOrig BufferedReade
r rdrOrig String fileNameCopy File
fCopy FileOutputStream
fosCopy PrintStream psCopy String
s isrKeyboard new InputStreamReader(System.in)
keyboard new BufferedReader(isrKeyboard)
System.out.print(name of file to copy
) System.out.flush() fileNameOrig
keyboard.readLine() fileNameCopy
fileNameOrig.concat(.copy) fOrig new
File(fileNameOrig) fisOrig new
FileInputStream(fOrig) isrOrig new
InputStreamReader(fisOrig) rdrOrig new
BufferedReader(isrOrig) fCopy new
File(fileNameCopy) fosCopy new
FileOutputStream(fCopy) psCopy new
PrintStream(fosCopy) s rdrOrig.readLine() psC
opy.println(s) s rdrOrig.readLine() psCopy.pri
ntln(s)
read name
models keyboard
access original
models file input
arrange to write to output
models output to file
read lines and write
access keyboard
Write a Comment
User Comments (0)
About PowerShow.com