Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015....

18
Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325

Transcript of Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015....

Page 1: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Input, Output, Operators, and Arithmetic

Dr. Eric Becker

CSE 1325

Page 2: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Streams

• Data in Java and Object-Oriented Program occur in streams:

– Bit Streams

– Byte Streams

– Character Streams

– I/O Streams

– File Streams

Page 3: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

For Java: byte

• For Java, streams are byte streams

• Java has a byte data type.

– For low-level data manipulation

• Classes for controlling and manipulating streams already exist.

Page 4: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

System Streams

• Three common streams:

– System.in

– System.out

– System.error

Page 5: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

System.in

• System.in

– Data stream from the keyboard

– Common Components:

• System.in.available()

• System.in.read()

• System.in.read(byte b[])

Page 6: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

System.out

• System.out

– Data stream to the terminal screen

– Common Components:

• System.out.append(arg0);

• System.out.flush()

• System.out.format(format, args);

• System.out.print(b);

• System.out.println();

• System.out.printf(format, args)

Page 7: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

System.err

• System.err

– Data stream to the error stream

– Common Components:

• System.err.append(arg0);

• System.err.flush()

• System.err.format(format, args);

• System.err.print(b);

• System.err.println();

• System.err.printf(format, args)

Page 8: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Acronyms

• OEM-Original Equipment Manufacter

• DK-Development Kit

• SDK-Software Development Kit

• DDK-Driver Development Kit

• DLL-Dynamic Link Library

• LIB-Library

• API-Application Programming Interface

Page 9: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

More Classes: java.util in the API

• Fortunately, there are additional classes

• Java has a huge API with pre-defined tools.

• Scanner and Formatter

• Must be imported from their respective libraries.

• import java.util.Scanner

• import java.util.Formatter

Page 10: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Scanner

• Scanner takes any input stream, and then allows values to be removed from it.

• For example:

• Scanner myScanner=new Scanner(System.in);

• And when complete

• myScanner.close() ends the stream.

Page 11: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Scanner

• Now, values can be taken from the Scanner.

• Scanner.next() - fetches the next String

• Scanner.nextInt() - reads an integer

• Scanner.nextLong()-reads the next long

• Scanner.next ...

Page 12: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Scanner

• And Scanner has Utility functions

• A delimiter is a symbol that separates values.

• "A,B,C" is delimited by a ','

• "Lovely.Rita.Metermaid" is delimited by a '.'

• Scanner.useDeliminter(String)

• Scanner.delimiter()

Page 13: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Formatter

• Formatter is like scanner

• Formatter output=new Formatter(System.out)

• And now the Formatter class provides various functions for making clean output

• output.close(); will close this data stream handler.

Page 14: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Formatter Functions

• Formatter has the format function

• output.format("%d, %e, %s\n\t\r")

• All the formatting codes work with Formatter

• Adds those formats to whatever data stream is being used.

Page 15: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Operators and Arithmetic

• Assign or Assignment, A=B

• Addition, A+B

• Subtraction, A-B

• Multiplication, A*B

• Division, A/B

• Remainder, …what?

– A%B Modulus or mod

Page 16: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Operators and Comparison

• Is equal to, equivalent ==

• Is not equal to, !=

• Greater than, >

• Less than, <

• Greater than or equal to, >=

• Less than or equal to, <=

Page 17: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Operators and Order

• Parenthesis first: ()

• Multipliers second: *,/,%

• Additive: +, -

• Relational: <,>,<=,>=

• Equality: ==,!=

• Assignment: =

Page 18: Input, Output, Operators, and Arithmeticcrystal.uta.edu/~becker/Lecture02-Input, Output... · 2015. 1. 23. · Input, Output, Operators, and Arithmetic Dr. Eric Becker CSE 1325. Streams

Time for Demonstrations