Object Oriented Software Design

Similar documents
Object Oriented Software Design

Strings, Strings and characters, String class methods. JAVA Standard Edition

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Java Input/Output. 11 April 2013 OSU CSE 1

Software 1. Java I/O

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

Object Oriented Software Design

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Introduction to Programming Using Java (98-388)

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Byte and Character Streams. Reading and Writing Console input and output

Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Lecture 11.1 I/O Streams

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO

Software 1 with Java. The java.io package. Streams. Streams. Streams. InputStreams

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Dining philosophers (cont)

Files and Streams

Object Oriented Software Design

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

Example: Copying the contents of a file

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

Exceptions and Working with Files

COMP-202: Foundations of Programming. Lecture 12: Linked List, and File I/O Sandeep Manjanna, Summer 2015

Today s plan Discuss the Bb quiz 1 Clarify Lab 1 Review basic Java materials Classes, Objects, Interfaces Strings Java IO. Understanding Board

Simple Java Input/Output

Programmierpraktikum

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 12, 2011

Special error return Constructors do not have a return value What if method uses the full range of the return type?

Darshan Institute of Engineering & Technology for Diploma Studies

Input-Output and Exception Handling

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

HST 952. Computing for Biomedical Scientists Lecture 8

Object Oriented Software Design

Creating Strings. String Length

Introduction Unit 4: Input, output and exceptions

Chapter 10. IO Streams

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

OOSD. Introduction to JAVA. Giuseppe Lipari Scuola Superiore Sant Anna Pisa. September 29, 2010

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

CS 251 Intermediate Programming Java I/O Streams

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

More on Java. Object-Oriented Programming

Java Intro 3. Java Intro 3. Class Libraries and the Java API. Outline

needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

PIC 20A Streams and I/O

The Java I/O System. Binary I/O streams (ASCII, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

I/O Streams. Object-oriented programming

Tirgul 1. Course Guidelines. Packages. Special requests. Inner classes. Inner classes - Example & Syntax

Techniques of Java Programming: Streams in Java

1.00 Lecture 30. Sending information to a Java program

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Programming with Java

Software 1. תרגול 9 Java I/O

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Overloaded Methods. Sending Messages. Overloaded Constructors. Sending Parameters

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Lesson 4 Utility classes: Math, String, I/O

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Array. Prepared By - Rifat Shahriyar

IT101. File Input and Output

This exam is open book. Each question is worth 3 points.

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Full file at

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Pace University. Fundamental Concepts of CS121 1

File Processing in Java

WOSO Source Code (Java)

Software 1. The java.io package. Streams. Streams. Streams. InputStreams

CS193j, Stanford Handout #26. Files and Streams

Getting Started in Java. Bill Pugh Dept. of Computer Science Univ. of Maryland, College Park

CN208 Introduction to Computer Programming

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

TEXT-BASED APPLICATIONS

Each command-line argument is placed in the args array that is passed to the static main method as below :

Computational Expression

Lesson 3: Accepting User Input and Using Different Methods for Output

CSC 1214: Object-Oriented Programming

USING LIBRARY CLASSES

Object-oriented programming in...

pre-emptive non pre-emptive

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

Language Features. 1. The primitive types int, double, and boolean are part of the AP

COMP-202: Foundations of Programming. Lecture 22: File I/O Jackie Cheung, Winter 2015

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Input from Files. Buffered Reader

Object-Oriented Programming Design. Topic : Streams and Files

Internet Technology 2/7/2013

Transcription:

Object Oriented Software Design I/O subsystem API Giuseppe Lipari http://retis.sssup.it/~lipari Scuola Superiore Sant Anna Pisa October 10, 2011 G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 1 / 24

Outline 1 String 2 I/O and files 3 ArrayList 4 Exercises G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 2 / 24

Outline 1 String 2 I/O and files 3 ArrayList 4 Exercises G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 3 / 24

Utilities for manipulating strings Since you will have to analyse strings to implement your program, let s have a closer look at class String The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation. G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 4 / 24

Utilities for manipulating strings Usually, methods that are used to read the value of some property of a class are called accessor methods method length() returns the number of characters in a string To read the character at position i, we can call charat(i) You can copy a substring of a string into an array of characters as follows: String mystring = "This is a lecture"; char[] temp = new char[5]; mystring.getchars(5, 10, temp, 0); It means: copy from the 5 th character (included) to the 10 th character (excluded) into temp starting at position 0 After the copy, temp contains "is a " If you want to obtain another string, it is possible to use substring: String mystring = "This is a lecture"; String sub = mystring.substring(5,10); G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 5 / 24

Concatenating strings The String class includes a method for concatenating two strings: string1.concat(string2); This returns a new string that is string1 with string2 added to it at the end. You can also use the concat() method with string literals, as in: "My name is ".concat("rumplestiltskin"); Strings are more commonly concatenated with the + operator, as in "Hello," + " world" + "!" G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 6 / 24

Formatted printing In Java, you can output formatted printing using System.out.printf(), as follows: System.out.printf("A float %f, and an integer %d", 3.754, 20); You can also use the format() method of class String: String fs; fs = String.format("The value of the float variable is %f, " + "while the value of the integer variable " + "is %d, and the string is %s", floatvar, intvar, stringvar); System.out.println(fs); G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 7 / 24

Converting strings into numbers The Number subclasses that wrap primitive numeric types (Byte, Integer, Double, Float, Long, and Short) each provide a class method named valueof that converts a string to an object of that type public class ValueOfDemo { public static void main(string[] args) { if (args.length == 2) { Float a = Float.valueOf(args[0]); Float b = Float.valueOf(args[1]); float c = Float.parseFloat(args[0]); float d = Float.parseFloat(args[1]); System.out.printf("a = %f", a.floatvalue()); System.out.printf("b = %f", b.floatvalue()); System.out.printf("c = %f", c); System.out.printf("d = %f", d); } else { System.out.println("Insert two command-line arguments"); } } } G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 8 / 24

Other methods The String class has a large amount of different methods for manipulating strings: searching character, replacing substrings, etc. Please refer to the Java 6 API to get a complete documentation for String G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 9 / 24

Outline 1 String 2 I/O and files 3 ArrayList 4 Exercises G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 10 / 24

The File class The File class does not represent a file, but one or more file names. It is used to get the list of files in a directory, as in./examples/08.java-examples/dirlist.java Explanation: the list() method of class File needs as argument an object of type FilenameFilter, which is a very simple interface: public interface FilenameFilter { boolean accept(file dir, String name); } The list() will call the accept() on every file contained in the directory, to see if a file name is acceptable, if accept() returns true the name is inserted in the list, otherwise it is not This technique is called callback G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 11 / 24

Making directories It is possible to use the File class to create directories, see if a file exist, get the file type, etc../examples/08.java-examples/makedirectories.java G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 12 / 24

Input and output The Java library classes for I/O are divided by input and output everything derived from the InputStream or Reader classes have basic methods called read() for reading a single byte or array of bytes everything derived from OutputStream or Writer classes have basic methods called write() for writing a single byte or array of bytes However, you won t generally use these methods; they exist so that other classes can use them - these other classes provide a more useful interface Thus, you ll rarely create your stream object by using a single class, but instead will layer multiple objects together to provide your desired functionality. The fact that you create more than one object to create a single resulting stream is the primary reason that Java s stream library is confusing. The rest of the slides are just descriptions of examples G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 13 / 24

Reading input by line BufferedReader in = new BufferedReader( new FileReader("IOStreamDemo.java")); String s, s2 = new String(); while((s = in.readline())!= null) s2 += s + "\n"; in.close(); Comment in represents the file handle, we open file IOStreamDemo.java we read one string at time, and append all strings in s2. The reading is done using buffering i.e. a block of data is read in an internal buffer, and then we read line by line from the buffer This technique is called decorator pattern or wrapper pattern G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 14 / 24

Reading from std input BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a line:"); System.out.println(stdin.readLine()); Comment: In this case, we read one line from the standard input (the keyboard), represented by object System.in This same code can be used in the assignment G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 15 / 24

Reading from a string StringReader in2 = new StringReader(s2); int c; while((c = in2.read())!= -1) System.out.print((char)c); It is possible to treat a string as a file In the example, we read one character at time from the string It is also possible to unread one character, as follows: String s = "This is a string!"; PushbackReader r = new PushbackReader(new StringReader(s)); char c = (char)r.read(); // read character T r.unread( P ); System.out.println((char)r.read()); // prints P In some cases, this may be useful for low-level parsing of strings G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 16 / 24

Writing onto a file try { BufferedReader in4 = new BufferedReader( new StringReader(s2)); PrintWriter out1 = new PrintWriter( new BufferedWriter(new FileWriter("IODemo.out"))); int linecount = 1; while((s = in4.readline())!= null ) out1.println(linecount++ + ": " + s); out1.close(); } catch(eofexception e) { System.err.println("End of stream"); } The previous example reads one line at time from a string, and writes on a output file PrinterWriter is used to output text files, it wraps a BufferedWriter which wraps an output file writer If you need to write a binary file, you only need to remove the PrinterWriter class, and only use the BufferedWriter. The explicit close() for out1 is needed otherwise the buffers don t get flushed, so they re incomplete G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 17 / 24

Outline 1 String 2 I/O and files 3 ArrayList 4 Exercises G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 18 / 24

Arrays We have seen arrays Arrays have fixed length: once you create an array, it is not possible to add further elements to it Variable-length arrays are supported by class ArrayList Three constructors: default constructor (an empty array), a constructor that takes an integer (the initial capacity, but the array is still empty) a constructor that takes a Collection of object ArrayList may only contain references to Objects API: http://download.oracle.com/javase/1.4.2/docs/api/jav G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 19 / 24

Example ArrayListExample.java import java.util.*; class ArrayListExample { public static void main(string args[]) { ArrayList al = new ArrayList(); for (int i=0; i<args.length; i++) al.add(args[i]); } } System.out.println("al.size() = " + al.size()); for (int i=0; i < al.size(); i++) { System.out.println(al.get(i)); } al.add("this is the last one"); al.add(0, "this is the first one"); System.out.println("al.size() = " + al.size()); for (int i=0; i < al.size(); i++) { System.out.println(al.get(i)); } G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 20 / 24

Outline 1 String 2 I/O and files 3 ArrayList 4 Exercises G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 21 / 24

Exercises Parenthesis matching Write a class that provides a static method to find matching parenthesis in a String The method takes as input a string, and the position of the first open parenthesis and returns the position of the closing parenthesis String s = "5 * (4 + 2) / 2"; Parenthesis.get(s, 4); // returns 10 In fact: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 5 * ( 4 + 2 ) / 2 Pay attention to nested parenthesis: String s = "5 * (4 / (2-1) - 2) / 2"; Parenthesis.get(s, 4); // returns 20, not 15 G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 22 / 24

Errors The function must: raise an exception called UnmatchedParenthesisException if it cannot find a matching closing parenthesis raise an exception called NotAParenthesisException if the initial position does not contain a left parenthesis symbol ( Write the function a set of at least 5 tests that check the correctness of your implementation Two tests must check that the exceptions are correctly raised One test checks for simple parenthesis Another one checks for 3 levels of parenthesis nesting The last one checks for a matching parenthesis as last character G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 23 / 24

Use your utility Now you should use your utility on a text file Read the file line by line For every line: Print the line on screen below, the number of outer groups of parenthesis, and for every group, and the number of contained groups If there is an error, prints the line number and the error message, and continues with the next line Example: (a + (b+c) + (a + (g+h))) >>> 1 groups "a + (b+c) + (a + (g+h))" >>> 2 groups "b+c", "a + (g+h)" >>> 0 groups >>> 1 group "g+h" >>> 0 groups G. Lipari (Scuola Superiore Sant Anna) Introduction to Java October 10, 2011 24 / 24