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

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

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

IT101. File Input and Output

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

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

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

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

Lecture 11.1 I/O Streams

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

CS 251 Intermediate Programming Java I/O Streams

Introduction Unit 4: Input, output and exceptions

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.

Object Oriented Software Design

Java Input/Output. 11 April 2013 OSU CSE 1

Objec&ves. Review. Standard Error Streams

Object-Oriented Programming Design. Topic : Streams and Files

Simple Java Input/Output

Classes Basic Overview

5/24/2006. Last Time. Announcements. Today. Method Overloading. Method Overloading - Cont. Method Overloading - Cont. (Midterm Exam!

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

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

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

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

HST 952. Computing for Biomedical Scientists Lecture 8

AP Computer Science A

Chapter 5 Some useful classes

CSE 21 Intro to Computing II. JAVA Objects: String & Scanner

PIC 20A Streams and I/O

USING LIBRARY CLASSES

Software 1. Java I/O

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

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

Full file at

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Core Java Contents. Duration: 25 Hours (1 Month)

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

Project 1. Java Data types and input/output 1/17/2014. Primitive data types (2) Primitive data types in Java

Introduction to Programming Using Java (98-388)

This Week s Agenda (Part A) CS121: Computer Programming I. The Games People Play. Data Types & Structures. The Array in Java.

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

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

WOSO Source Code (Java)

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

1.00 Lecture 30. Sending information to a Java program

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

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

COMP String and Console I/O. Yi Hong May 18, 2015

Exceptions and Working with Files

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

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

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

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

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

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

Core Java Syllabus. Overview

Dining philosophers (cont)

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

More on Java. Object-Oriented Programming

Strings in Java String Methods. The only operator that can be applied to String objects is concatenation (+) for combining one or more strings.

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

CS 106A Midterm Review. Rishi Bedi, adapted from slides by Kate Rydberg and Nick Troccoli Summer 2017

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Programming Languages and Techniques (CIS120)

Notes from the Boards Set BN19 Page

First Java Program - Output to the Screen

Fall 2017 CISC124 10/1/2017

CS 211: Existing Classes in the Java Library

Software Practice 1 - File I/O

ECE 122 Engineering Problem Solving with Java

Active Learning: Streams

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

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

CSC 1214: Object-Oriented Programming

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

Case Study: Savings Account Interest

DM550 Introduction to Programming part 2. Jan Baumbach.

C17: File I/O and Exception Handling

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

String related classes

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Lecture Set 2: Starting Java

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

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

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

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

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Lecture Set 2: Starting Java

Input from Files. Buffered Reader

Chettinad College of Engineering & Technology Department of Information Technology Internal Examination II (Answer Key)

Programming Languages and Techniques (CIS120)

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Streams and File I/O

Example: Copying the contents of a file

COMP6700/2140 Std. Lib., I/O

Chapter 2: Data and Expressions

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

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

Transcription:

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO Ananda Gunawardena Objects and Methods working together to solve a problem Object Oriented Programming Paradigm is about Java Application Programmer interface (API) (supports OOP) Java APIs classes Packages Java API Abstract classes 1

Abstract Implementing multiple extends implements Classe can be extended multiple levels, but only one at a time. Abstract Class versus interface Grand parent extends Abstract class parent extends Some methods may have already being implemented All methods must be implemented by class Abstract methods in Abstract Abstract class Abstract method Java APIs Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs Download APIs to your local computer from http://java.oracle.com/j2se/1.5/download.html Treat it as a dictionary for reference, instead of a book from front to back Declared w/o an implementation 2

Strings a b b c d String Methods Examples Construct a string String s = "Hello world"; Get the length of the string int length = s.length();//length = 11 Access a part of the string String sub = s.substring(6);//"world" char c = s.charat(1); //'e' Concatenate strings String t = " of java"; String s1 = s + t; //"Hello world of java Covert Character c to String Character.toString(c); Do a search Java String API to find all of the methods String is immutable. Once created, it cannot be changed. There is no such a method setchar(char c) Why? Security reasons. (The detailed explanation is complicated and not useful in this course. Doing the following exercises is more helpful in understanding the concept.) Q: What are the values of chararray1 and chararray1 at the end of the code char[] chararray1 = {'a', 'b', 'c'; char[] chararray2 = chararray1; chararray1[1] = 'i'; Q: What are the values of s1 and s2 at the end of the code String s1 = "abc"; String s2 = s1 ; s1 = s1 + "def"; Comparing Strings Comparing strings Q: Are word and work equal? Which is bigger? String s1 = "word"; String s2 = "work"; compareto equals word work 3

Prefix and postfix Check if two strings have the same prefix and postfix Does hello world he starts and ends with he? String s = "hello world he"; String Main Usage Finding the index of a substring I want to know the position of o sin the string String s = "Hello world"; Convert cases I want to turn hello world he into all upper cases. String main usage Special characters Use \ to proceed the special characters Tab \t New line \n Carriage return \r Single quote \ Double quote \ Backslash \\ Q: What is output? System.out.println("a\nb\tc ); String Advanced Usage breaking words apart I want to break "Hello world of java into tokens "Hello world of java String s = "Hello world of java"; StringTokenizer st = new StringTokenizer(s); while (st.hasmoretokens()) { System.out.println(st.nextToken()); String Advanced Usage breaking words apart Q: I want to break "Hello, world of java into "Hello world of java (there is a comma in the string) Concatenate strings repetitively Q: Suppose you have a list of 100 strings. You want to concatenate them into one string? Solution 1 List stringlist = new ArrayList(); for (int i = 0; i < 10; i++) { stringlist.add("string" + i); String finalstring = ""; for (Iterator iter = stringlist.iterator(); iter.hasnext(); ) { String s = (String) iter.next(); finalstring += s; //work but inefficient for large strings and large amount of concatenation because String is immutable. Intuitively, Java needs to maintain an old string, a new string, and a concatenate string in each iteration. 4

String Advanced Usage Concatenate strings repetitively Q: Suppose you have a list of 100 strings. You want to concatenate them into one string? Solution 2 List stringlist = new ArrayList(); for (int i = 0; i < 10; i++) { stringlist.add("string" + i); StringBuffer sb = new StringBuffer(); for (Iterator iter = stringlist.iterator(); iter.hasnext(); ) { String s = (String) iter.next(); sb.append(s); String Advanced Usage Strings and OOP I have just written a class. How can I represent the class with a string? public class Point { double x; double y; Point(double x, double y) { this.x = x; this.y = y; Point p = new Point(2,3); System.out.println(p); //Point@360be0 String Advanced Usage Convert between strings to primitives Q: I want to convert an integer 123 to a string and convert a string 123 to an integer int i = 123; String s = i + ; String t = String.valueOf(i); String u = Integer.toString(i); //s = t = u = 123 Java IO int j = Integer.parseInt(s); // j = 123 //Extension Double.parseDouble(); Java IO Most programs need to interact with the outside world by reading and writing files from/to a hard disk Microsoft Office Matlab Eclipse Your future programs Java provides a comprehensive package to deal with file input and output. Java 5 Scanner Class java.util Class Scanner How to read an integer from stdin Scanner sc = new Scanner(System.in); int i = sc.nextint(); How to read a set of long integers from a file Scanner sc = new Scanner(new FileReader("file.txt")); while (sc.hasnextlong()) { System.out.println(sc.nextLong()); 5

Scanner How to read a set of strings(words) from a file Scanner sc = new Scanner(new FileReader("file.txt")); while (sc.hasnext()) { System.out.println(sc.next()); Accessing Scanner object Scanner objects already comes with an Iterator boolean hasnext() Returns true if this scanner has another token in its input. boolean hasnext(string pattern) Returns true if the next token matches the pattern constructed from the specified string. boolean hasnextdouble() Returns true if the next token in this scanner's input can be interpreted as a double value using the nextdouble() method. STDOUT Other IO methods Q: How do I output something to the screen? To give user the result, or debug the code By using System.out.println() Object anobject = new Object(); String myanswer = "no"; int i = 42; System.out.println("Hello, World of Java"); System.out.println("An object is " + anobject); System.out.println("The answer is " + myanswer + " at this time."); System.out.println("The answer is " + i + '.'); //Hello, World of Java //An object is java.lang.object@18d107f //The answer is no at this time. //The answer is 42. STDIN How do I read input from the keyboard? To get input from the user from a consol instead of a GUI int b = 0; try { b = System.in.read();//returns an integer catch (Exception e) { System.out.println("Caught " + e); System.out.println("Read this data: " + (char)b); // Read this data: 1 (after I typed 1) // Read this data: 1 (after I typed 123) // Read this data: a (after I typed a) //This gives you the ability to read one byte at a time. STDIN Using Scanner class Scanner Sc = new Scanner(System.in); Sc.nextInt(); or Sc.next(); or Other types (see Scanner class) 6

Java IO There are two kinds of files Files of characters (texts) Files of bytes (primitives, arrays, objects) java.io package contains Character streams Input Reader BufferedReader InputStreamReader FileReader Byte streams InputStream Output Writer BufferedWriter FileWriter PrintWriter OutputStream PrintStream Summary Java API is a dictionary of the Java language, and the interface for programmers to manipulate Java classes as black boxes. String is a class, and immutable. Construct a string String s = "Hello world"; Get its length s.length(); Get a substring s.substring(6) Concatenate strings s + t or StringBuffer Compare strings s1.equals(s2), s1.compareto(s2) s.startswith("hell"), s.endswith("world") Convert cases s.touppercase(); Finding the index of a substring s.indexof("o") Break a string apart StringTokenizer st = new StringTokenizer(s); Convert a string to primitives Integer.parseInt(s); Convert a primitive to a string 4+ Abstract classes are shown in italics. Summary Data can be in the form of characters or bytes. IO is comprehensive Output a message to screen System.out.println("Hello, World of Java"); Output text data to a file PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("c://output.txt"))); Read an integer from the consol int b = System.in.read(); Read a line from the consol BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); or use Scanner class Read a file BufferedReader is = new BufferedReader(new FileReader("c://data.txt")); Or use Scanner class To Do List Read Lab 2 and Start Salon2 assignment due on Tuesday midnight What to do First read the assignment and identify important, incorrect and incomplete facts Secondly, respond to questions, if there are breadcrumbs (that is places that can be highlighted to support your response, select them) 7