Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input.

Similar presentations


Presentation on theme: "OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input."— Presentation transcript:

1 OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input

2 OOP&M - laboratory lectures2 OOP&M – LAB2 – connection types output input Program file monitor We have studied the standard connections of our computer to the rest of the world During LAB1 we learned how to show information on the screen or how to save it into a file Today we will learn in which way the information can be taken from files or from the keyboard in order to improve the features of our programs Finally we will see how to get information from the HTML files from the internet... we will program our first browser file keyboard internet

3 OOP&M - laboratory lectures3 OOP&M – LAB2 – do you remember? Connections between Java programs and the rest of the world are made using streams Streams are like “bridges” of information that link Java programs and the screen, the keyboard, a file or the net Streams are just sequences of bytes... numbers that we cannot understand at a first sight Therefore we need some classes which help us to transform the information into something that we could read First we transform the numbers into long sequences of characters Second we transform the characters into strings At the end, the way in which we can handle the information is very easy. Methods like print, println, readLine and others make these processes easier

4 OOP&M - laboratory lectures4 OOP&M – LAB2 LABzwei: the Input Keyboard

5 OOP&M - laboratory lectures5 OOP&M – LAB2 – keyboard The use of the keyboard will improve the features of the programs that we made during the first lab-lesson Now we can ask to the user which files should be deleted, or which line should be printed to the screen An easy example is: import java.io.*; public class read { public static void main(String arg[]) throws Exception { InputStreamReaderisr; BufferedReaderkeyb; Stringline; isr = new InputStreamReader(System.in); keyb = new BufferedReader(isr); line = keyb.readLine(); System.out.println(line); }

6 OOP&M - laboratory lectures6 OOP&M – LAB2 – keyboard EXERCISE A.1 - Improving DeleteFile Take the following program (a version from “ DeleteFile.java ”) and make it ask the user which file should be deleted: import java.io.*; public class DeleteFile { public static void main(String arg[]) throws Exception { File f; f = new File("to_be_removed.java"); f.delete(); System.out.println("File deleted"); } [Hint: use *.flush() for showing the information]

7 OOP&M - laboratory lectures7 EXERCISE A.2 - Counting things Implement a program that reads a line from the keyboard and answers how many “o’s” are in it [Hint: use methods like *.equals(), *.toUpperCase() or *.substring() from the String class for searching] OOP&M – LAB2 – monitor

8 OOP&M - laboratory lectures8 EXERCISE A.3 - Non-stop counting Improve program A.2 in order to make it loop until the user enters a blank line [Hint: use the while loop with a structure like: s = keyb.readLine(); while (s.length() > 0) {... s = keyb.readLine(); } ] OOP&M – LAB2 – monitor This checks if the string had a length of at least one character

9 OOP&M - laboratory lectures9 OOP&M – LAB2 LABzwei: the Input Files

10 OOP&M - laboratory lectures10 OOP&M – LAB2 – files Now we are going to try to read information from files During the theory lecture we saw that files communicate with programs using sequences of bytes, called Streams After that we saw that we could use other structures for reading text from the files in a very easy way The constructors were: Strings; File fOrig; FileInputStream fisOrig; InputStreamReader isrOrig; BufferedReader rdrOrig; fOrig = new File(“data.input”); fisOrig = new FileInputStream(fOrig); isrOrig = new InputStreamReader(fisOrig); rdrOrig = new BufferedReader(isrOrig); s = rdrOrig.readLine();

11 OOP&M - laboratory lectures11 OOP&M – LAB2 – files EXERCISE B.1 – Files, how to print them Create a text-file with the Notepad and save it in the same directory where you are working Write a program called “PrintFile” that prints a couple of lines of the text file on the screen [Hint: remember that the way in which we print is with System.out.println() ]

12 OOP&M - laboratory lectures12 OOP&M – LAB2 – files EXERCISE B.2 – Files, how to copy them Create a text-file with the Notepad and save it in the same directory where you are working Write a program called “CopyFile” that makes a copy of it called “*.copy” (the same name and extension plus “.copy”) After copying the file write a message on the screen that indicates the end of the action [Hint: notice that here we work with two files. We need two file objects, one with an InputStream and the other one with an OutputStream] [Hint2: use a while loop like: s = rdrOrig.readLine(); while (s != null) {... s = rdrOrig.readLine(); } ]

13 OOP&M - laboratory lectures13 OOP&M – LAB2 – files EXERCISE B.3 – Files, how to search through them Write a program called “LookFor” that asks for the user the name of a file Afterwards, it will ask for a string to search As a result the program will answer in how many lines it found the string in the file and print the lines [Hint: this exercise is a mixture of A.2 and B.2... It is difficult, but not so much!!] [Hint2: you need to use *.indexOf(string_to_search ) that answers “-1” if the string_to_search is not found]

14 OOP&M - laboratory lectures14 OOP&M – LAB2 LABzwei: the Input Internet

15 OOP&M - laboratory lectures15 OOP&M – LAB2 – internet Now we are going to try to read information from the Web The Net as other communication media for our programs can be read using Streams There is a difference between what we saw until now and what will happen with the Net: files are in HTML code The constructors are: Strings; URL u; InputStream ins; InputStreamReader isr; BufferedReader link; u = new URL("http://www.domain.ext/"); ins = u.openStream(); isr = new InputStreamReader(ins); link = new BufferedReader(isr); s = link.readLine(); As you see here, the structure is similar to the one that we saw for the files. The difference is at the constructor for the InputStream object, which is special!! But the method for reading the information is the same: *.readline()

16 OOP&M - laboratory lectures16 OOP&M – LAB2 – internet EXERCISE C.1 – Just reading a file Implement the next program and answer: A)how does it work? B)how does the information look like? C)where is the difference/s to a Graphical Browser? D)what would you do for improving your text browser and update it to “MySecondBrowser”? (do not be too ambitious, please)

17 OOP&M - laboratory lectures17 import java.net.*; import java.io.*; class MyFirstBrowser { public static void main(String[] arg) throws Exception { URL uNet; InputStream insNet; InputStreamReader isrNet; BufferedReader linkNet; String s; uNet = new URL("http://webzone.k3.mah.se/k3dacu/oopm/week5/index1.html"); insNet = uNet.openStream(); isrNet = new InputStreamReader(insNet); linkNet = new BufferedReader(isrNet); s = linkNet.readLine(); System.out.println(s); s = linkNet.readLine(); System.out.println(s); System.out.println("connection closed"); } OOP&M – LAB2 – internet EXERCISE C.1 – Just reading a file

18 OOP&M - laboratory lectures18 OOP&M – LAB2 – internet EXERCISE C.2 – Look for the title and the body Write the program called “Browser” that asks for the user the name of URL As a result the program will answer with the title of the file and will print the text contained into the “body” part of the HTML file [Hint: the title is to be found between the and TAGs in a HTML file, and the body between and ]

19 OOP&M - laboratory lectures19 Do it yourself: try to finish your own browser, add features like: jump to the next line with and underline a text if it is a link ( ) show the name of the images ( ) try to show tables (...) think how to ask to the user for the next jump to do... [NOTE: this exercise is for doing it at home, start if you have the time during the LAB, but continue as far as you can... Look for your preferred solution to all this problems] OOP&M – LAB2 – internet EXERCISE C.3 – Extra work


Download ppt "OOP&M - laboratory lectures1 OOP&M – LAB2 LABzwei: the Input."

Similar presentations


Ads by Google