Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -

Similar presentations


Presentation on theme: "OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -"— Presentation transcript:

1 OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -

2 OOP&M - theory lectures2 OOP&M – do you remember? inputoutput -joystick -mouse -keyboard -microph. human based input -floppy -HD -CD -ZIP/JAZZ -… prev. rec. infor. as input -joystick -screen -speakers -lights human based output -floppy -HD -CD -ZIP/JAZZ -… infor. as output

3 OOP&M - theory lectures3 monitor  object System.out is the predefined reference to the monitor in the PrintStream class displayread programmonitor Bjorn System.out.println(“ In winter Bjorn and Lisa eat potatoes.”) println(“ In winter Bjorn and Lisa eat potatoes.”) behaviordetails reference to the receiver message OOP&M – do you remember?

4 OOP&M - theory lectures4 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 file  object Java program FileOutputStream SHOPPING potatis mjolk agg … shopping.lst bytes of data ewsc24rfds53Hej20Hur20m76ar20du20?rsf OOP&M – do you remember?

5 OOP&M - theory lectures5 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(“16:30 Met at Triangeln …”); log.println(“16:30 Met at Triangeln …”); … } one LogMyDay example OOP&M – do you remember?

6 OOP&M - theory lectures6 OOP&M – the second day “… And God saw that the light was good; and God separated the light from the darkness …” - the Hollybook -

7 OOP&M - theory lectures7 OOP&M – input / output to programs Java program keyboard monitor files network ? ?? System.out FileOutputStream Java program ? ?

8 OOP&M - theory lectures8 OOP&M – 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 OOP&M - theory lectures9 OOP&M – input … an overview Example: we want to delete a file, then we need a code that would look like the following: Filef; f = new File(“delete_me”); f.delete(); delete But with this we can only delete a file called: “delete_me” Wouldn’t 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 OOP&M - theory lectures10 OOP&M – input … an overview Java program InputStream bytes of data ewsc24rfds53Hej20Hur20m76ar20du20?rsf Input source We must build a bridge between the Java Program and the input source!!

11 OOP&M - theory lectures11 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 OOP&M – input … an overview Input – whether coming from the keyboard, from a file, from the network or from another program – requires a similar sequence of stages

12 OOP&M - theory lectures12 Stages OOP&M – input … an overview Construct a reference to an InputStream object, and with it Construct an InputStreamReader object, and with it Construct a BufferedReader object This references could be of two types FileInputStreamReader for files BufferedInputStream for keyboard and the network Note that: InputStream inputs sequences of bytes into the program InputStreamReader models the stream of input as characters, but doesn’t 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 OOP&M - theory lectures13 OOP&M – 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 [existing] InputStream new InputStreamReader new BufferedReader keyb isr System.in

14 OOP&M - theory lectures14 OOP&M – a board with keys the constructor for InputStreamReader accepts the keyboard (a BufferedInputStream reference) as its argument new InputStreamReader ( System.in ) Java provides System.in contained into the BufferedInputStream class the constructor for BufferedReader accepts a InputStreamReader reference as its argument new BufferedReader ( ISR )

15 OOP&M - theory lectures15 OOP&M – 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 OOP&M - theory lectures16 OOP&M – 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(inputline); System.out.println(“s”); }

17 OOP&M - theory lectures17 OOP&M – 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 [Y]es, in other case, pray something!”

18 OOP&M - theory lectures18 OOP&M – 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); System.out.println(“s”); …

19 OOP&M - theory lectures19 OOP&M – 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 – Murphy’s 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 OOP&M - theory lectures20 OOP&M – 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? Bjorn will cook as many potatoes as possible at once, because in other case he would be wasting his time!! Bjorn oven potatoes

21 OOP&M - theory lectures21 OOP&M – 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. But, there is a method that forces the strings to be shown on the screen. *.flush() forces the computer to empty the buffer. buffer string1 string2 string3

22 OOP&M - theory lectures22 OOP&M – 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(); inputline = keyb.readLine(); System.out.print(inputline); System.out.println(“s”); …

23 OOP&M - theory lectures23 OOP&M – 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 new FileInputStream new InputStreamReader new BufferedReader bsr isr 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

24 OOP&M - theory lectures24 OOP&M – come IN PUT files the constructor for InputStreamReader accepts a stream from a file (a FileInputStream reference) as its argument new InputStreamReader ( FileInputStream ) the constructor for FileInputStream accepts a file (a File reference) as its argument the constructor for BufferedReader accepts a InputStreamReader reference as its argument new BufferedReader ( ISR ) new FileInputStream ( file )

25 OOP&M - theory lectures25 OOP&M – come IN PUT files The total declaration would be: File f; FileInputStream fsr; InputStreamReader isr; BufferedReader bsr; f = new File(“Big_parties_many_potatis.txt”); fsr = new FileInputStream(f); isr = new InputStreamReader(fsr); bsr = new BufferedReader(isr);

26 OOP&M - theory lectures26 OOP&M – come IN PUT files Let’s 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); inputline = bsr.readLine(); System.out.println(inputline);

27 OOP&M - theory lectures27 OOP&M – 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 OOP&M - theory lectures28 FileInputStream InputStreamReader BufferedReader Some InputStream InputStreamReader BufferedReader CopyFile program PrintStream FileOutputStream OOP&M – mixin’ it up

29 OOP&M - theory lectures29 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; BufferedReader 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(); psCopy.println(s); s = rdrOrig.readLine(); psCopy.println(s); … } models keyboard models file input models output to file access keyboard read name access original arrange to write to output read lines and write OOP&M – mixin’ it up


Download ppt "OOP&M - theory lectures1 OOP&M – the second day “… In the beginning God created the heavens and the earth …” - the Hollybook -"

Similar presentations


Ads by Google