Oops U3
Oops U3
Syntax
return type function name() throws Exception_name
Built-in exceptions
Exceptions Description
ArithmeticException It is thrown when an exceptional condition has
occurred in an arithmetic operation.
ArrayIndexOutOfBound It is thrown to indicate that an array has been
Exception accessed with an illegal index.
ClassNotFoundException This Exception is raised when we try to access a
class whose definition is not found.
FileNotFoundException This Exception is raised when a file is not
accessible or does not open.
IOException It is thrown when an input-output operation failed
or interrupted.
Built-in exceptions
Exceptions Description
InterruptedException It is thrown when a thread is waiting, sleeping, or
doing some processing, and it is interrupted.
NoSuchFieldException It is thrown when a class does not contain the
field (or variable) specified.
NoSuchMethodException It is thrown when accessing a method which is
not found.
NullPointerException This exception is raised when referring to the
members of a null object. Null represents nothing.
Built-in exceptions
Exceptions Description
IOException It is thrown when an input-output operation failed
or interrupted.
NumberFormatException This exception is raised when a method could not
convert a string into a numeric format.
RuntimeException This represents any exception which occurs
during runtime.
StringIndexOutOfBounds It is thrown by String class methods to indicate
Exception that an index is either negative than the size of the
string
this keyword
• this is a reference variable that refers to the current object.
• this can be used to refer current class instance variable.
• this can be used to invoke current class method (implicitly)
• this() can be used to invoke current class constructor.
• this can be passed as an argument in the method call.
• this can be passed as argument in the constructor call.
• this can be used to return the current class instance from the method.
Creating own Exceptions
• To create own exceptions in Java, the following points should be
followed when writing our own exception classes −
• All exceptions must be a child of Throwable.
• If we want to write a checked exception that is automatically enforced
by the Handle or Declare Rule, we need to extend the Exception class.
• If we want to write a runtime exception, we need to extend the
RuntimeException class.
Creating own Exceptions
Syntax
class MyException extends Exception
{
}
Class declaration
• public class PrintWriter extends Writer
PrintWriter
Method Description
void println(boolean x) It is used to print the boolean value.
void println(char[] x) It is used to print an array of characters.
void println(int x) It is used to print an integer.
PrintWriter append(char c) It is used to append the specified character to
the writer.
PrintWriter It is used to append the specified character
append(CharSequence ch) sequence to the writer.
PrintWriter
Method Description
PrintWriter It is used to append a subsequence of specified
append(CharSequence ch, int character to the writer.
start, int end)
boolean checkError() It is used to flushes the stream and check its
error state.
protected void setError() It is used to indicate that an error occurs.
protected void clearError() It is used to clear the error state of a stream.
PrintWriter
Method Description
PrintWriter format(String It is used to write a formatted string to the
format, Object... args) writer using specified arguments and format
string.
void print(Object obj) It is used to print an object.
void flush() It is used to flushes the stream.
void close() It is used to close the stream.
Reading in Files
• Java FileInputStream class obtains input bytes from a file. It is used
for reading byte-oriented data (streams of raw bytes) such as image
data, audio, video etc.
class declaration
• public class FileInputStream extends InputStream
Reading in Files
Method Description
It is used to return the estimated number of
int available()
bytes that can be read from the input stream.
It is used to read the byte of data from the
int read()
input stream.
It is used to read up to b.length bytes of data
int read(byte[] b)
from the input stream.
It is used to read up to len bytes of data from
int read(byte[] b, int off, int len)
the input stream.
Reading in Files
Method Description
It is used to skip over and discards x bytes of data
long skip(long x)
from the input stream.
FileChannel It is used to return the unique FileChannel object
getChannel() associated with the file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor object.
It is used to ensure that the close method is call when
protected void finalize()
there is no more reference to the file input stream.
void close() It is used to closes the stream.
Writing in Files
• Java FileOutputStream is an output stream used for writing data to a
file.
• If we have to write primitive values into a file, use FileOutputStream
class.
• We can write byte-oriented as well as character-oriented data through
FileOutputStream class.
Writing in Files
Method Description
protected void finalize() It is used to clean up the connection with the
file output stream.
void write(byte[] ary) It is used to write array.length bytes from the
byte array to the file output stream.
void write(byte[] ary, int off, It is used to write len bytes from the byte array
int len) starting at offset off to the file output stream.
void write(int b) It is used to write the specified byte to the file
output stream.
Writing in Files
Method Description
FileChannel getChannel() It is used to return the file channel object
associated with the file output stream.
FileDescriptor getFD() It is used to return the file descriptor associated
with the stream.
void close() It is used to closes the file output stream.
• Develop a Java application to generate Electricity bill. Create a class with the following members:
Consumer no., consumer name, previous month reading, current month reading, type of EB
connection (i.e domestic or commercial). Compute the bill amount using the following tariff.
• If the type of the EB connection is domestic, calculate the amount to be paid as follows:
• First 100 units - Rs. 1 per unit
• 101-200 units - Rs. 2.50 per unit
• 201 -500 units - Rs. 4 per unit
• > 501 units - Rs. 6 per unit
• If the type of the EB connection is commercial, calculate the amount to be paid as follows:
• First 100 units - Rs. 2 per unit
• 101-200 units - Rs. 4.50 per unit
• 201 -500 units - Rs. 6 per unit
• > 501 units - Rs. 7 per unit
• Develop a java application to implement currency converter (Dollar to
INR, EURO to INR, Yen to INR and vice versa), distance converter
(meter to KM, miles to KM and vice versa) , time converter (hours to
minutes, seconds and vice versa) using packages.
•
Develop a Java application with Employee class with Emp_name, Emp_id,
Address, Mail_id, Mobile_no as members. Inherit the classes, Programmer,
Assistant Professor, Associate Professor and Professor from employee class.
Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP
as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club fund.
Generate pay slips for the employees with their gross and net salary.
• Write a program to perform string operations using ArrayList. Write
functions for the following
• a. Append - add at end
• b. Insert – add at particular index
• c. Search
• d. List all string starts with given letter
Thank you
Reading in console
• Java Reader is an abstract class for reading character streams. The
only methods that a subclass must implement are read(char[], int, int)
and close().
• Some of the implementation class are BufferedReader,
CharArrayReader, FilterReader, InputStreamReader, PipedReader,
StringReader.
Reading in console
Method Description
abstract void close() It closes the stream and releases any
system resources associated with it.
void mark(int readAheadLimit) It marks the present position in the
stream.
boolean markSupported() It tells whether this stream supports the
mark() operation.
int read() It reads a single character.
Reading in console
Method Description
int read() It reads a single character.
abstract int read(char[] cbuf) It reads characters into an array.
int read(char[] cbuf, int off, int len) It reads characters into a portion of an
array.
boolean read(CharBuffer target) It attempts to read characters into the
specified character buffer.
void reset() It resets the stream.
Writing in console
Method Description
append(char c) It appends the specified character to this writer.
append(CharSequence csq) It appends the specified character sequence to
this writer
append(CharSequence csq, It appends a subsequence of the specified
int start, int end) character sequence to this writer.
void close() It closes the stream, flushing it first.
void flush() It flushes the stream.
Writing in console
Method Description
write(char[] cbuf) It writes an array of characters.
write(char[] cbuf, int off, int len) It writes a portion of an array of
characters.
write(int c) It writes a single character.
write(String str) It writes a string.
write(String str, int off, int len) It writes a portion