I/O in java and Revision. 2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.:

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Today’s topics: I/O (Input/Output). Scribbler Inputs & Outputs  What are the Scribbler’s inputs and outputs?  reset button  motors/wheel  light sensor.
Java review and more. Class Header  Public class Welcome  Case sensitive  Body of the class must be enclosed by braces.
Using Processing Stream. Predefined Streams System.in InputStream used to read bytes from the keyboard System.out PrintStream used to write bytes to the.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
Java Review Rem Collier. A Statement I have been asked to do these Lectures at YOUR request. They are to help YOU with any Java question that you have.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Io package as Java’s basic I/O system continue’d.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
1 Course Lectures Available on line:
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Java SE 8 for Programmers, Third Edition
CS61B L02 Using Objects (1)Garcia / Yelick Fall 2003 © UCB Kathy Yelick Handout for today: These lecture notes Computer Science 61B Lecture 2 – Using Objects.
I/O in Java Dennis Burford
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
File and Stream I/O, Redirection Privacy and Super
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
CS 11 java track: lecture 2 This week: more on object-oriented programming (OOP) objects vs. primitive types creating new objects with new calling methods.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Department of Computer Engineering Using Objects Computer Programming for International Engineers.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
1 Input/Output. 2 In Java input and output take place through I/O streams – An I/O stream represents an input source or output destination – Streams support.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.6 Reading Input with java.io Produced by Harvey.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Keyboard and Screen I/O (Input/Output). Screen Output  System.out is an object that is part of the Java language. (Don’t worry yet about why there is.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Object Oriented Programming Lecture 2: BallWorld.
CSC111 Quick Revision.
Introduction to programming in java
Input/Output.
Java Review Rem Collier.
Interactive Standard Input/output
Objects, Classes, Program Constructs
12: The Java I/O System stream model.
I/O Basics.
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
MSIS 655 Advanced Business Applications Programming
Introduction to Computing Using Java
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

I/O in java and Revision

2 I/O in Java Many packages and libraries associated with Java provide sophisticated ways to input and output information, e.g.: – GUI-based approaches: input from low-level keyboard and mouse events, or higher level semantic operations on buttons, menus, etc; output through arbitrarily sophisticated graphics (Swing, AWT, Java2d, etc). – (Server-side) Web-based approaches: input from HTML forms (or via client-side Javascript); output through dynamically generated Web pages (Java Servlets, Java Server Pages, etc). – XML-based: input through XML parsing; output through XML serialization (DOMParser, SAXParser, etc). Nevertheless, we often need to resort to more traditional methods to read and write information from terminal keyboard and screen, or ordinary files. – This section discusses “traditional I/O”.

3 Standard Input/Output The System class in java.lang provides the “standard” I/O streams System.in, System.out, and System.err. System.in is an instance of InputStream. System.out and System.err are instances of PrintStream.

4 Simple Output We already saw many examples of simple output: e.g. float x ; System.out.println(“The value of x is: ” + x) ; – Here we use the string concatenation operator, + (in conjunction with an implicit “string conversion” operation, which converts x to a suitable String representation). – (Note implicit string conversion only works in a + operation). The prefix System.out is a reference to a static field of the System class. – System is a class in the implicitly imported java.lang package. The field out has type PrintStream. It represents the “standard output” of the program. – Typically the terminal screen, unless it has been redirected.

5 Simple keyboard input import java.io.* ; class BufferedReaderTest { public static void main(String [] args) throws IOException { // Create a BufferedReader wrapping standard input: BufferedReader in = new BufferedReader(new InputStreamReader(System.in)) ; String s ; s = in.readLine() ; // Reads any string terminated by new- line. System.out.println("s = " + s) ; // Print what was read. }

Q1 Write a program to keep inputting integer values until –1 is entered.

Ans1 public class E_2_2 { private void doComputation ( ) { final Input in = new Input ( ) ; int value = 0 ; do { System.out.print ( "Type an int: " ) ; value = in.nextInt ( ) ; } while ( value != -1 ) ; } public static void main ( final String[] args ) { E_2_2 object = new E_2_2 ( ) ; object.doComputation ( ) ; }

q2 Write a program to input 10 integers, add them up and display their sum and average.

Ans2 public class E_2_7 { private void doComputation ( ) { final Input in = new Input ( ) ; int sum = 0 ; for ( int n = 0 ; n < 10 ; ++n ) { System.out.print ( "Enter int " + ( n+1 ) +": " ) ; int val = in.nextInt ( ) ; sum = sum + val ; } System.out.println ( "Sum = " + sum ) ; System.out.println ( "Average = " + sum/10 ) ; } public static void main ( final String[] args ) { E_2_7 object = new E_2_7 ( ) ; object.doComputation ( ) ; }

Q3 Write a program to input 10 words and then display the words that are first and last in alphabetical order.

Ans3 public class E_2_11 { private void doComputation ( ) { final Input in = new Input ( ) ; String first = "" ; String last = "" ; for ( int i = 0 ; i < 10 ; ++i ) { System.out.print ( "Enter string " + ( i+1 ) + ": " ) ; final String s = in.nextLine ( ) ; if ( first.equals ( "" ) || ( first.compareTo ( s ) > 0 ) ) { first = s ; } if ( last.compareTo ( s ) < 0 ) { last = s ; } } System.out.println ( "First word in alphabetical order is: " + first ) ; System.out.println ( "Last word in alphabetical order is: " + last ) ; } public static void main ( final String[] args ) { E_2_11 object = new E_2_11 ( ) ; object.doComputation ( ) ; }

Q4 Write a program to read in 10 integers and store them in an array. Then display the contents of the array.

Ans4 public class E_4_1 { private void execute ( ) { final int[] data = new int [10] ; final Input input = new Input ( ) ; System.out.print ( "Enter " + data.length + " int values : " ) ; for ( int i = 0 ; i < data.length ; ++i ) { data[i] = input.nextInt ( ) ; } System.out.print ( "Values input were : " ) ; for ( int v : data ) { System.out.print ( v + " " ) ; } System.out.println ( ) ; } public static void main ( final String[] args ) { ( new E_4_1 ( ) ).execute ( ) ; }

Q5 Write a program to read in a sequence of integers until ‘stop’ is entered. Store the integers in an array. Then display the average of the numbers entered.

Ans5 public class E_4_2 { private int[] doInput ( ) { final Input in = new Input ( ) ; int[] buffer = new int[2000] ; System.out.println ( "Type in up to " + buffer.length + " integers, and the input with the word stop." ) ; for ( int i = 0 ; i < buffer.length ; ++i ) { final String s = in.next ( ) ; if ( s.compareTo ( "stop" ) == 0 ) { // buffer is too large for the amount of data input so create a new array of the right // size for the number of items input and replace the current buffer. final int[] data = new int[i] ; System.arraycopy ( buffer, 0, data, 0, i ) ; buffer = data ; break ; } buffer[i] = Integer.parseInt ( s ) ; } return buffer ; }

Ans5 private double calculateMean ( final int[] data ) { int total = 0 ; for ( int i = 0 ; i < data.length ; ++i ) { total += data[i] ; } return ( (double) total ) / data.length ; } public static void main ( final String[] args ) { final E_4_2 object = new E_4_2 ( ) ; System.out.println ( "Mean of entered data is " + object.calculateMean ( object.doInput ( ) ) ) ; }

Q6 Repeat the previous but use an ArrayList instead of an array.

Ans6 import java.util.ArrayList ; public class E_4_3 { private ArrayList doInput ( ) { final Input in = new Input ( ) ; final ArrayList buffer = new ArrayList ( ) ; System.out.println ( "Type in integers and the input with the word stop." ) ; while ( true ) { final String s = in.next ( ) ; if ( s.compareTo ( "stop" ) == 0 ) { break ; } buffer.add ( Integer.parseInt ( s ) ) ; } return buffer ; } private double calculateMean ( final ArrayList data ) { int total = 0 ; for ( int i = 0 ; i < data.size ( ) ; ++i ) { total += data.get ( i ) ; } return ( (double) total ) / data.size ( ) ; } public static void main ( final String[] args ) { final E_4_3 object = new E_4_3 ( ) ; System.out.println ( "Mean of entered data is " + object.calculateMean ( object.doInput ( ) ) ) ; }

Q7 Write a method that takes two character array parameters and returns true if the sequence of characters stored in the second array is a subsequence of those characters stored in the first array.

Ans7 import java.util.Arrays ; public class E_4_11 { private boolean containsSequence ( final char[] a, final char[] b ) { if ( a.length >= b.length ) { for ( int i = 0 ; i <= a.length - b.length ; ++i ) { boolean areTheSame = true ; for ( int j = 0 ; j < b.length ; ++j ) { if ( a[ i + j ] != b[ j ] ) { areTheSame = false ; break ; } } if ( areTheSame ) { return true ; } } return false ; }

Ans7 private void assertTrue ( final boolean b, final String message ) { if ( ! b ) { System.out.println ( "Test failed: " + message ) ; } } private void runTest ( ) { char[] a = { ’a’, ’b’, ’c’, ’d’, ’e’, ’f’ } ; char[] b = { ’a’, ’b’, ’c’, ’d’ } ; assertTrue ( containsSequence ( a, a ), "a == a" ) ; assertTrue ( containsSequence ( b, b ), "b == b" ) ; assertTrue ( containsSequence ( a, b ), "b is in a" ) ; assertTrue ( ! containsSequence ( b, a ), "a is longer than b" ) ; char[] c = { ’c’, ’d’ } ; assertTrue ( containsSequence ( c, c ), "c == c" ) ; assertTrue ( containsSequence ( a, c ), "c is in a" ) ; assertTrue ( ! containsSequence ( c, a ), "a is longer than c" ) ; assertTrue ( containsSequence ( b, c ), "c is in b" ) ; assertTrue ( ! containsSequence ( c, b ), "b is longer than c" ) ; char[] d = { ’b’, ’z’ } ; assertTrue ( containsSequence ( d, d ), "d == d" ) ; assertTrue ( ! containsSequence ( a, d ), "d is not in a" ) ; assertTrue ( ! containsSequence ( b, d ), "d is not in b" ) ; assertTrue ( ! containsSequence ( c, d ), "d is not in c" ) ; } public static void main ( final String[] args ) { ( new E_4_11 ( ) ).runTest ( ) ; }