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

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

IT101. File Input and Output

I/O Streams. COMP 202 File Access. Standard I/O. I/O Stream Categories

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP File Access 1

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture 11.1 I/O Streams

Dining philosophers (cont)

Simple Java Input/Output

Objec&ves. Review. Standard Error Streams

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

COMP-202: Foundations of Programming. Lecture 11: ArrayList, and Linked List Sandeep Manjanna, Summer 2015

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

HST 952. Computing for Biomedical Scientists Lecture 8

Streams and File I/O

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Java Input/Output. 11 April 2013 OSU CSE 1

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

1.00 Lecture 30. Sending information to a Java program

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

CS 251 Intermediate Programming Java I/O Streams

Programming Languages and Techniques (CIS120)

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

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

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

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Introduction Unit 4: Input, output and exceptions

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

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

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

COMP Streams and File I/O. Yi Hong June 10, 2015

Java Programming Lecture 9

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

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

Streams and File I/O

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

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

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

Unit 10: exception handling and file I/O

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

I/O Streams. Object-oriented programming

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. File Input and Output

PIC 20A Streams and I/O

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

Files and Streams

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

CS193j, Stanford Handout #26. Files and Streams

Input-Output and Exception Handling

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

Darshan Institute of Engineering & Technology for Diploma Studies

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

File Processing in Java

Programming Languages and Techniques (CIS120)

CSC 1214: Object-Oriented Programming

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

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Programming Languages and Techniques (CIS120)

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

I/O STREAM (REQUIRED IN THE FINAL)

Programming Languages and Techniques (CIS120)

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Example: Copying the contents of a file

More on Java. Object-Oriented Programming

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

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

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

Programming Languages and Techniques (CIS120)

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

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

When we reach the line "z = x / y" the program crashes with the message:

Character Stream : It provides a convenient means for handling input and output of characters.

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

CS 200 File Input and Output Jim Williams, PhD

Object Oriented Software Design

Fundamental language mechanisms

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

Object Oriented Software Design

Input from Files. Buffered Reader

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

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

COMP-202 Unit 9: Exceptions

Java file manipulations

Sri Vidya College of Engineering & Technology Question Bank

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

File I/O Introduction to File I/O Text Files The File Class Binary Files 614

ITI Introduction to Computer Science II

Data Structures. 03 Streams & File I/O

File I/O Array Basics For-each loop

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

Building a social network

C17: File I/O and Exception Handling

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

Streams and File I/O

Transcription:

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

Announcements Assignment 4 is posted and Due on 29 th of June at 11:30 pm. Course Evaluations due date is 29 th June. Final exams : 26 th of June (2pm to 5pm) @ MAASS 112

This Lecture Linked List File Input / Output Review on Passing References to a Method One step at a time.

Review: ArrayList

Arrays vs. ArrayLists Array Fixed Length ArrayList Variable Length.length.size( ) Can hold both Primitive and Reference types Need to know the size before creating. Does not support Generic type Only Reference types No need to specify size for creating an instance. Supports Generic types

Linked List

Linked Lists Pictorial representation of a Linked list of int values. Linked List Start = Data = 3 Data = 2 Data = 5 Data = 1 Data = 7 Next = Next = Next = Next = Next = null Node Node Node Node Node

Building our Linked List We need to define a class for each element in the list: the Node class The Node class should have: A pointer to some data. We will call it its data. For simplicity, we will hold int numbers in our LinkedList. A pointer to the next element in the list. Unsurprisingly, we will call it next.

Building our Linked List public class Node { private int data; private Node next; // add a constructor here // add getter and setter methods here }

Building our Linked List We need to define a class for doing operations on the list: the LinkedList class. The LinkedList class should have: A pointer to the first element in the list. We will call it start. All the methods with the operations we want to make on the list.

Building our Linked List public class LinkedList { private Node start; // add a constructor here // add Insertion, search and deletion methods here }

Methods for our Linked List Inserting a node at the beginning of the List. Linked List Start = Data = 9 Next = null new Node Data = 3 Data = 2 Data = 5 Data = 1 Data = 7 Next = Next = Next = Next = Next = null Node Node Node Node Node

Methods for our Linked List Inserting a node at the end of the List. Linked List Start = Data = 3 Data = 2 Data = 5 Data = 1 Data = 7 Next = Next = Next = Next = Next = null Node Node Node Node Node Data = 9 Next = null new Node

Methods for our Linked List Inserting a node in the middle of the List (Between 5 and 1). Linked List Start = Data = 9 Next = null new Node Data = 3 Data = 2 Data = 5 Data = 1 Data = 7 Next = Next = Next = Next = Next = null Node Node Node Node Node

Methods for our Linked List Removing a node from the List (Remove node with Data = 5). Linked List Start = Data = 3 Data = 2 Data = 5 Data = 1 Data = 7 Next = Next = Next = Next = Next = null Node Node Node Node Node

Methods for our Linked List Removing a node from the List (Remove node with Data = 5). Linked List Start = Data = 3 Data = 2 Data = 1 Data = 7 Next = Next = Next = Next = null Node Node Node Node

Doubly Linked Lists Pictorial representation of a Doubly Linked list of int values. You will be implementing doubly linked list in Assignment 4. Start = Doubly Linked List End = Data = 3 Data = 2 Data = 5 Data = 1 Data = 7 Next = Next = Next = Next = Next = null Prev = null Prev = Prev = Prev = Prev = Node Node Node Node Node

Linked Lists Advantages over arrays: 1) Dynamic size 2) Ease of insertion/deletion Drawbacks: 1) Random access is not possible. We have to access elements sequentially starting from the first node. So we cannot do binary search with linked lists. 2) Extra memory space for a pointer is required with each element of the list.

File Input and File Output

What are System.in, System.out? One of the remaining mysteries in the code we've been writing all semester System.out.println("Hello, world!"); Scanner sc = new Scanner(System.in);

I/O Streams Sequence of bytes that flow from a source to a destination InputStream get bytes from somewhere e.g., System.in gets bytes from the user (via the keyboard) OutputStream write bytes to somewhere else e.g., System.out, System.err write bytes to screen (after converting them to characters) There are many kinds of streams in java.io

Scanner The job of a Scanner is to read characters from some source, then convert it into a desired data type. Constructor: specify where to read from Scanner sc = new Scanner(System.in); methods like.nextint(),.nextdouble(): actually read the characters and convert to an int or double etc. int num = sc.nextint();

Reading Files Instead of passing System.in to Scanner, we can pass in a file as the source of characters! Option 1 FileReader reader = new FileReader("test.txt"); Scanner sc = new Scanner(reader); Option 2 Scanner sc = new Scanner(new File("test.txt"));

Potential Problems What can go wrong when you are reading a file? Cannot find file. File is corrupted. File is protected. File is in unreadable/unexpected format.

Checked Exceptions Java forces us to write try-catch blocks to handle (some of) these problems, because they're very common. try { Scanner sc = new Scanner(new File("test.txt")); // file reading operations here } catch (FileNotFoundException fnfe) { System.err.println("test.txt cannot be found."); } catch (Exception e) { System.err.println("Exception:" + e.getmessage()); }

How to Read Need to check that there is still text to read: while (sc.hasnextline()) { // do the actual reading here String line = sc.nextline(); }

Try it out!! Open your text editor and create a file with several lines. Now, read it and print it out, but append the line number before each line (and a tab '\t'). e.g., 1 The first line goes here. 2 The second line here. 3 The third line here.

Writing to a Text File Instead of writing to System.out, we write to a file with the PrintWriter class! PrintWriter out = new PrintWriter("output.txt"); out.println("hello, hard drive!"); You need to write a try-catch block for this too. Close the file when you're done: out.close();

Buffered Writers You can improve the speed by using buffered versions of the classes above: Input BufferedReader in = new BufferedReader(new FileReader("foo.in")); // feed in to a Scanner Output PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out"))); // can now call out.print(), out.println()

What is Buffering? Accessing the hard disk is slow, so you want to minimize how often you do it. Buffering means saving the data in an intermediate spot, so that the OS can schedule it to happen when it'd be convenient. results in speed-up

Reading and Writing Objects How do we read and write objects? For example, the Cat class which has the name and age of a Cat?

Let us Try it out!! Copy your Cat class from before into the current folder. Then, write two methods: public static void writecats(cat[] cats, String filename) This method writes an array of cats to the specified file public static Cat[] readcats(string filename) This method reads an array of Cats from the specified file

Other Options Access different points in a file, rather than read it sequentially (random access) Read and write bytes, not characters Read and write objects without having to define our own file format (serialization) If you're interested, have a look at: http://docs.oracle.com/javase/tutorial/essential/io/

Review: Passing Reference Type to Methods

Summary Linked List File I / O