Week 2 - L4-L6
Week 2 - L4-L6
Week 2 - L4-L6
Unit No:1
Unit Name: Introduction to OOP
Lecture 6 – Input and output functions in Java : Buffered reader class, Scanner class 39
2
Module No 1: Introduction to OOP
Lecture No: 4
Features of Java Language, JDK
environment
Introduction to JAVA
• Simple:
Easy to learn.
removed many confusing and rarely-used features
e.g., explicit pointers, operator overloading etc.
• Object-oriented:
Organize our software as a combination of different types of
objects that incorporates both data and behaviour.
• Platform Independent:
A platform is the hardware or software environment in which a
program runs.
It has two components:
Runtime Environment
API(Application Programming Interface)
“Write-Once Run-Anywhere”
Java code can be run on multiple platforms e.g. Windows,
Linux, Sun Solaris, Mac/OS etc.
• Robust:
Strong memory management.
No usage of pointers.
Exception handling and type checking mechanisms.
• Multi-threaded
A thread is like a separate program, executing concurrently.
Java programs deal with many tasks at once by defining
multiple threads.
Doesn't occupy memory for each thread.
Shares a common memory area.
Lecture No: 5
Basic Constructs of Java Programming
Simple Java Program
System.out.print (“Hello”);
}
Java compiler
javac
java
Constants
Variables
Data Types
Operators and Expression
Revision of Branching and Looping
These are the reserved words in JAVA which cannot be used as variable
names or identifiers.
abstract continue for new switch
assert*** default goto* package synchronized
boolean do if private this
break double implements protected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
Operators/Order of Precedence
Example Usage
Assignments
General Format: variable = expression ;
Where variable is a previously declared identifier and
expression is a valid combo of identifiers, operators,
and method (a.k.a. procedure or function) calls
Shortcuts:
var *= expr ; // Equivalent to var = var * (expr);
var /= expr ; // Equivalent to var = var / (expr);
var += expr ; // Equivalent to var = var + (expr);
var -= expr ; // Equivalent to var = var – (expr);
var %= expr ; // Equivalent to var = var % (expr);
var++; // Equivalent to var = var + 1;
var--; // Equivalent to var = var - 1;
1. Conditional Statements
if-else statements
switch case statements
2. Iterative Statements
for loop
while loop
do-while loop
“if” Statements
if (boolean_expr)
{
statements
}
if (boolean_expr)
statement;
◦ if-else
if (boolean_expr) if
true false
{
statements for true
} statements statements
else
{
statements for false
}
Boolean Expressions
Boolean expressions use conditional operators such that they result in a
value of true or false
Conditional Operators (Not by order of precedence)
Operators Operations
== or != Equality, not equal
int i1 = 1, i2 = 2, i3 = 3, i4 = 0;
boolean b1 = true, b2 = false, b3 = true;
i2 > i1 // true
(i3 – i2) > i1 // false
(i3 – i2) >= i1 // true
(i2 > i1) & (i2 < i3) // true
(i2 < i1) | (i2 > i1) // true
i2 != i1 // true
(i1 < i2) | ((i1/i4) > 1) // Divide by 0 exception
(i1 < i2) || ((i1/i4) > 1) // true
(i1 < i2) | (i1++ > 1) // true, i1 contains 2
(i1 < i2) || (i1++ > 1) // true, i1 contains 1
b1 && b2 && b3 // false
(b1 || b2) && b3 // true
b1 && (i1 == (i3 – i2)) // true
Example
int n = 5;
true
switch (n) if statements
{
false
case 1:
n = n + 1; true
if statements
break;
case 5: false
n = n + 2;
break; statements
default:
n = n – 1;
}
while loop
Exit condition evaluated at top
do loop
Exit condition evaluated at bottom
for loop
Exit condition evaluated at top
Includes a initialization statements
Includes a update statements for each iteration
while loop
false if
while (boolean_expr)
true
{
statements statements
}
do loop
statements
do
{
statements if true
}
false
while (boolean_expr)
for loop
false
if
true
statements update
class Example
{ static public void main(String args[])
{
int i = 0;
System.out.println("while loop");
while (i < 10)
{
System.out.println(i);
i++;
}
System.out.println("do loop");
do
{
System.out.println(i);
i--;
}
while (i > 0);
System.out.println("for loop");
for (i = 0; i < 10; i++)
{
System.out.println(i);
}
} // End main
} // End Example
Lecture No: 6
Input and output functions in Java : Buffered
reader class, Scanner class
Input/Output
• Java I/O (Input and Output) is used to process the input and produce the
output.
• The java.io package contains all the classes required for input and output
operations.
• In Java, 3 streams are created for us automatically. All these streams are
attached with the console.
42 Lecture 6- Input and output functions in Java : Buffered reader class, Scanner class
OutputStream class
Method Description
1) public void write(int)throws IOException is used to write a byte to the current output stream.
2) public void write(byte[])throws IOException is used to write an array of byte to the current
output stream.
4) public void close()throws IOException is used to close the current output stream.
43 Lecture 6- Input and output functions in Java : Buffered reader class, Scanner class
OutputStream class
• OutputStream Hierarchy
44 Lecture 6- Input and output functions in Java : Buffered reader class, Scanner class
InputStream class
Method Description
1) public abstract int read()throws reads the next byte of data from the input
IOException stream. It returns -1 at the end of the file.
2) public int available()throws IOException returns an estimate of the number of bytes that
can be read from the current input stream.
3) public void close()throws IOException is used to close the current input stream.
45 Lecture 6- Input and output functions in Java : Buffered reader class, Scanner class
InputStream class
• InputStream Hierarchy
46 Lecture 6- Input and output functions in Java : Buffered reader class, Scanner class
Classes used for Reading Information
• In the class:
public static void main(String args[ ]) throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
• The method:
String readLine( )
• Reading a String
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader(isr);
String sName;
• Example:
Scanner sc = new Scanner(System.in);
• Constructor
Scanner sc = new Scanner(System.in);
• Reading a String
String sc.nextLine( );
• Reading an int
int sc.nextInt( );
• Reading a double
double sc.nextDouble( );
Method Description
nextInt() reads an integer from the user
nextDouble() reads a double from the user
next() reads a one-word String from the user
nextLine() reads a one-line String from the user
• Reading a String
Scanner scan = new Scanner(System.in);
String str;
int iAge = 0;
Int iYear = 2023;
Buffer Memory Scanner has little buffer of 1 KB char BufferReader has large buffer of 8KB
2 buffer. byte Buffer as compared to Scanner.
Processing Scanner is bit slower as it need to parse BufferReader is faster than Scanner as it
3
Speed data as well. only reads a character stream.
Methods Scanner has methods like nextInt(), BufferReader has methods like
4
nextShort() etc. parseInt(), parseShort() etc.
Read Line Scanner has method nextLine() to read BufferReader has method readLine() to
5
a line. read a line.
59 Lecture 6- Input and output functions in Java : Buffered reader class, Scanner class
Thank You