Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.:

Similar presentations


Presentation on theme: "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.:"— Presentation transcript:

1 I/O in java and Revision

2 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 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 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 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. }

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

7 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 ( ) ; }

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

9 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 ( ) ; }

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

11 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 ( ) ; }

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

13 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 ( ) ; }

14 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.

15 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 ; }

16 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 ( ) ) ) ; }

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

18 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 ( ) ) ) ; }

19 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.

20 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 ; }

21 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 ( ) ; }


Download ppt "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.:"

Similar presentations


Ads by Google