Académique Documents
Professionnel Documents
Culture Documents
INPUT/OUTPUT/
INPUT DEVICE/OUTPUT
DEVICE
OBJECTIVES
Define input and
output
Define input device and output
device
Differentiate between input and input
device
Differentiate between output and
output device 1
INPUT & INPUT DEVICE
Input
any information needed by program to
complete execution
character input
mouse click
graphic input
audio input
file input, etc.
Input
computer peripherals used to accept input
device
keyboard
mouse
scanner
microphone
2
disks, etc.
OUTPUT & OUTPUT
DEVICE
Output
any information program conveys to user
display
printouts
engineering drawings
audio output
file output, etc.
Output
device
computer peripherals used to convey
output
monitor
printer
plotter
speaker
3
disks, etc.
INPUT/OUTPUT/
INPUT DEVICE/OUTPUT
DEVICE - Questions
1. Define input.
2. Define output.
3. What is input device.
4. What is output device.
5. Differentiate between -
a) Input and output
b) Input device and output
c) device
Input and input device
d) Output and output device
4
SESSION - 2
INPUT AND OUTPUT IN JAVA
OBJECTIVES
Define Stream
State advantages of
Stream 5
JAVA PROGRAM
Java
mainly used for writing programs for the
Internet
Real-life applications of
Java
not text based console programs
graphically oriented
applets
rely upon Javas Abstract Window Toolkit -
(AWT)
for interaction with user
6
INPUT AND OUTPUT IN
JAVA
Regarded as stream of data
c d
ab
d
a
b
c
a dc
b
Output stream
Input stream
7
ADVANTAGES OF STREAM
All streams behave in same
manner
even if linked physical devices
differ
Keyboard or mouse or scanner
OBJECTIVES
Defines package
18
STREAM VARIABLES OF System
CLASS
System class
contains three predefined stream variables
Store
input/outpu
t stream of
characters
in out err
automatically created
can be used
anywhere in the program ( public )
b) static 20
SESSION - 6
System . in
OBJECTIVES
State significance of
System . in
21
in
VARIABLE
Inputting data also called reading
data
managed by stream variable -
in
refers to standard input stream
in
variable
data
INPUT DEVICE 22
PROCESSOR
System .
in
System . in
used to create object of InputStream classes or
Scanner class
created to copy data from System . in
data
INPUT DEVICE 23
PROCESSOR
System . in - Questions
1. What is the utility of stream variable in?
OBJECTIVES
25
out
VARIABLE
Outputting data writing/printing data
out
variable
data 26
PROCESSOR OUTPUT DEVICE
System .
out
System .
out
can be used with methods like -
print( ), println
()
to display data
out
variable
27
PROCESSOR OUTPUT DEVICE
System . out - Questions
1. What is the utility of stream variable out?
State significance of
System . err
29
err
VARIABLE
Run-time errors are sent to output
devices
managed by stream variable -
err
refers to standard output stream for error
messages
by default sends data to console or
monitor
err
variable
data 30
PROCESSOR OUTPUT DEVICE
System .
err
System .
err
can be used -
run-time errors
err
variable
31
PROCESSOR OUTPUT DEVICE
System . err - Questions
1. What is the utility of stream variable err?
Command Run
line time
Given while calling main Given while the
method program is executed
Use in programs 42
COMMAND LINE INPUT
Information passed into a method
during method call.
Arguments directly follow the
method name on the command line
method call
line
Multiple arguments can be passed
to a method.
With comma in between
arguments
Argument
s Should be included within double
quotes
Stored as string values in a String43
COMMAND LINE INPUT -
STATEMENTS
A String array should be declared
in main method declaration
as
line
e.g.- public static void main ( String args [] )
parameter
String inputs can be directly
assigned to variables
e.g.- String name = args [ 0 ] ;
Numeric inputs can be converted to
numbers from string
using methods of corresponding wrapper
class
contains methods to handle primitive data
e.g.- int roll = Integer . parseInt ( args [ 1 ] ) ;
type
float salary = Float . parseFloat 44( args [ 2 ] ) ;
long empno = Long . parseLong ( args [ 3 ] ) ;
COMMAND LINE INPUT
VALUES
Should
be
typed when method is called
enclosed in double quotes
Multiple inputs can be
typed
should be separated by a
comma
Inputs typed are
serially stored in array defined as parameter
Care should be taken
values typed in command line should match
arguments used in
45
numbe type order
COMMAND LINE INPUT
e.g.
Accept name and rate of a chocolate
and number of packets bought.
Calculate the amount to be paid.
Display all the details.
Class CommandLineInput
{ public static void main ( String args [ ] )
{ String name = args [ 0 ] ;
float rate = Float . parseFloat ( args [ 1 ] ) ;
int packets = Integer . parseInt ( args [ 2 ] ;
float amount = rate * packets ;
System . out . println
(Name\t\tRate\t\tQuantity\t\tAmount ) ;
System . out . println ( name + \t\t
46 + rate +
\t\t + packets + \t\t + amount ) ; } }
COMMAND LINE INPUT
VALUES
While calling the main method the
values should be typed as-
(assuming-
chocolate name Cadburys Fruit and
Nuts, rate Rs. 25,
no. of packets bought 5 )
main() { Cadburys Fruit and Nut , 25 , 5 }
If the above sequence is changed to -
1. main() { 25 , Cadburys Fruit and Nut , 5 }
Program will give run time error as name cannot be stored in
variable float rate.
OBJECTIVES
Define
49
RUN TIME INPUT
Inputs
typed
during program execution
on the output screen
one at a time
preceded by a prompt
Use -
a java package to use library classes
to handle inputs
object of Scanner/BufferedReader class
50
CLASSES HANDLING
RUN-TIME INPUT
Two types
Byte stream Character stream
handled by handled by
State use
version 1.5.0
as a teaching tool for beginners
to take keyboard input
without having to deal with handling
checked exceptions
some exceptions handled by
class
without programmers intervention
56
SCANNER CLASS
Utilise java . util package in the
program
using import statement
before class declaration
These methods
returntrue fals
e
if data type of next if data type of next token
token in scanner's in scanner's input does
input matches not match
hasNextInt ( ) hasNextBoolean ( )
Basic : 25906
CCA : 6580.123901176453
HRA : 30000
--------------------------------------------
Gross : 62486.123901176456
PF : 2720.13
--------------------------------------------
Net : 59765.99390117646
--------------------------------------------
66
SESSION - 14
BufferedReader CLASS
OBJECTIVES
Define
BufferedReader class -
buffers (stores) input send by user
improve efficiency
inputs can be send continuously
internal processing using each data goes on side by side
Define
during
compilation
at run time
entered data
is valid
catch
throw
throws
finally 87
KEYWORDS try AND catch
try and catch statements work as a
group
try
statements to be monitored for
exceptions
written within try block
exception occurring involving code in try
block
trapped and thrown by interpreter
remaining statements in try block
bypassed
catch
exception thrown by interpreter
caught using catch blocks
handled by the programmer in rational
manner 88
one catch block written for each exception type
KEYWORD finally
Keyword
finally
used to include code
that absolutely must be executed
before program execution
stops
passes to next part of
program
usually statements to close all open files
written in this block
91
OUTPUT OF PROGRAM USING
try, catch, finally
1. Enter a number : 200
Enter another number : 10
Quotient -> 20
This is to try out finally block.
Numbers are :200 and 10
Sum is : 210
2. Enter a number : 25
Enter another number : 0
Sorry! No. 2 should not be zero.
This is to try out finally block.
Numbers are :25 and 0
Sum is : 25
3. Enter a number : m
Sorry! You need to type a no.
This is to try out finally block.
Numbers are :0 and 0
Sum is : 0 92
KEYWORDS throw AND
throws
throw -
used to throw errors explicitly
throws -
throws error out of the method
Define
Define
pages
may be rendered multiple
times
out of sequence
Advantages
Optimises usage of printer memory
by printing only a part at a time
Pages may also be printed in reverse
104 order
PAGE RENDERING MODEL
Two models of rendering pages to
a print job-
Printables
Pageables
105
PRINTABLES
Printables
simpler of the two printing
models
uses one PagePainter for entire document
pages rendered in sequence
starting at page
zero
if a few pages rendered (say 5 through 7)
print subsystem asks for all pages up to last
page (here upto seventh page)
prints only pages five, six and seven
total pages to be printed not displayed
Implemented through Printables106
PAGEABLES
Pageables
offer flexibility
each page can feature a different layout
mostly used with Books class
handles a collection of pages having
different formats
characteristics
each page can have its own painter or
a painter for cover page, another painter for table of
format
contents, third for entire document
mix of portrait and landscape pages can be
used
print pages out of sequence, pages may
be skipped
does not need to know no. of pages
107 in
Implemented
document through Pageables
PRINTER CLASS
Java also has a library class named
Printer
represents an IBM AS/400 printer
subclass of
java . lang . Object 108
OUTPUT IN JAVA
Questions
1. Which object handles standard output
stream?
2. Which class declares methods to display
on screen?
3. What are the two methods used for
display?
4. Write the difference between two display
methods.
5. What are escape sequence
characters?
6. Name two of the most frequently used
escape sequence characters. How are
7. they
Writedenoted?
a statement to display -
109
The commentator shouted, G O A L
ASSIGNMENT
1. A courier company delivers parcels at the
following rate
Within the city
Upto 5 gms. Rs. 25/=
Above 5 gms. - Rs. 40/= for every
10 gms.
Outside the city
The rates are double
Write a program in Java to accept from
the user whether the destination address
is within city or not and also the weight
of the parcel in gms. Calculate110the
ASSIGNMENT
2. A company has employees who are divided into
four grades as follows:
Grade Basic DA HRA (Rs.
per month) (% of Basic) (% of Basic)
1 10,000 or more 40
30 2 5,000 - < 10,000 40
25
3 < 5,000 but > 2,000 30
20
4 2,000 or less 30 15
116
BYTE STREAM
Byte stream
Has two main
classes
InputStream OutputStream
Takes byte oriented input stream Creates byte oriented output
stream
117
CHARACTER
STREAM
Character stream
Has two main
classes
Reader Writer
Takes character oriented input Takes byte oriented input
stream stream
Changes to byte oriented Changes to character oriented
output stream output stream
handle different
devices
memory buffers
119
STREAMS OF JAVA -
Questions
1. What are the two types of streams?
Define
Variab
129 le4
BYTE STREAM INPUT e.g.
import java . io . * ;
public class ByteStreamInput
{ public static void main () throws IOException
{ DataInputStream input = new
DataInputStream (System . in ) ;
System . out . print (Enter your
name : ) ; String name = input . readLine();
System . out . print (Enter your age :
); int age =
Integer.parseInt(input.readLine() ) ; System . out .
print (Are you Indian (Y/N) ) ; char ans = (char)
input . read () ; if ( age >= 18 &&
ans == Y || ans == y )
System.out.println(name+! You can vote.); else
stream
POINTS TO NOTE
Prior to JDK 1.1, the input and output
classes (mostly found in the java.io
package) only supported 8-bit byte streams.
The concept of 16-bit Unicode character
streams was introduced in JDK 1.1. While
byte streams were supported via the
java.io.InputStream and
java.io.OutputStream classes and their
subclasses, character streams are
implemented by the java.io.Reader and
java.io.Writer classes and their subclasses.
132
hasNextInt()
Most files contain a great deal of data. Practical programs must process this
data using a loop of some sort. Here is a program that reads a text file that
contains many integers and writes the square of each one. The loop reads
integers from the file one by one until the end of file is reached or non-
integer input is encountered.
import java.util.Scanner; import java.io.*; class ManySquares { public static
void main (String[] args) throws IOException { File file = new
File("myData.txt"); // create a File object Scanner scan = new Scanner( file );
// connect a Scanner to the file int num, square; while( scan.hasNextInt() ) //
is there more data to process? { num = scan.nextInt(); square = num * num ;
System.out.println("The square of " + num + " is " + square); } } } The
hasNextInt() method returns true if the next set of characters in the input
stream can be read in as an int. If they can't be read as an int, or if the end
of the file has been reached, then it returns false.
133
java . awt . print PACKAGE
java . awt . print package has -
three interfaces
Printable
Pageable
PrinterGraphic
s
four classes
PageFormat
PrinterJob
Paper
Book 134
EXCEPTION HANDLING
All exception types are handled by
code in pre-written class Throwable
has two main
subclasses
Error
Exceptio
When an
n exception
occurs
JVM stores error in object of Exception class
can be utilised by programmer
to -
display a relevant error message
135
continue with next part of program