Upload
others
View
21
Download
0
Embed Size (px)
Citation preview
Input, Output, Operators, and Arithmetic
Dr. Eric Becker
CSE 1325
Streams
• Data in Java and Object-Oriented Program occur in streams:
– Bit Streams
– Byte Streams
– Character Streams
– I/O Streams
– File 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.
System Streams
• Three common streams:
– System.in
– System.out
– System.error
System.in
• System.in
– Data stream from the keyboard
– Common Components:
• System.in.available()
• System.in.read()
• System.in.read(byte b[])
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)
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)
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
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
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.
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 ...
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()
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.
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.
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
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, <=
Operators and Order
• Parenthesis first: ()
• Multipliers second: *,/,%
• Additive: +, -
• Relational: <,>,<=,>=
• Equality: ==,!=
• Assignment: =
Time for Demonstrations