Java Assignment2
Java Assignment2
ConsoS
vSib
A Muthlevel nhentane.
Creake
5 ho Packaees re
ep tion handling
ge Shve
88 cho vattex gtream
i r e a d d a l a
) total
Creating objects
from the
defined class.
s. Usinga
created
Objects are objects. All ated object
created o
number of
create any
class we can variables.
space
Memory space
Memory forsthe
for
instance
only durina ohi.
can u s e
the
allocated
variables will be
instance
creation.
O--O
Create memory space
(i)
After object declaration, we must allocate memory
sOace for the instance variables for each object. This is done
with the help of new operator. The general form is
objn= newclassname ()
Where
declared objects
obj1.. .objn- already
defined class
classname
-
already run
during
allocate memory
to
new key word
time memory
executed,
the
are stating
these
statements
created
and the
When
variables
will be space
memory
instance
space for the allocated
shows
this
the already
address is stored in
below
ng object decla
3.07
obj1
[ var1
var2
varn
instance
objn variables
var1
var2
varn
Example
i) class examplel a
int x;
int y
X ax
64 bit
y a.y 2
cxamplel a;
anew examplel ()
Command line input
08sed at
passed at thetime of running the program. The general form
is
C> java classname arguments to be passed
where
- key word
java
class nanme name of the main class
arguments to be passed - list of arguments separated by
blank space.
Execution procedure
The structure of main method is
key
class
String
- a r r a y name
args
string objects ans
of
and is em
String args [] is an array
command line is
ls
Any argument passed
We
through
can access the array elements and
stored
use thein t
and yso"
array.
the program.
Example
(i) C» java Sample God Loves you
args [1]->Loves
args [2] - - you
String s;
int sum;
intm arg.length;
for(i-0; im;it+)
3.34
sum + Integer.parselnt(arglil)
System.out.println("Sum = " +sum);
Running
sum 5 7 3 2 1
C:java
The output is
Sum= 18
narameters
h e Supet er( )must
Super) muSt match
matcn the
arguments in the constructor defined in the superciass.
Multilevel inheritance
uperclass
employee
empno
e m p n a m e
t e a c h e
deptname
totalwork
multilevel inheritanca
load
NSSofficer
NSSunitno.
t o t a l s t r e n g t h
alled
called teacher is derive
deriy
subclass
a
example inherits all te
In our
This sub class
from a super
class
employee.
c l a s s employee
(empno a
properties
of the super (deptname, total
won
propertiesS
its o w n NSS
empname) plus another sub class called
class
From this sub properties
and class). contains all the
This class N
officer is derived. plus
and teacher)
is properties of employee
teacher (that
unit no and total strength)
own properties (NSS
Where
Ciass, extends keyword
name2, name3 namen name of the sub classes
() public
(i) protecteed
public access
The general form is,
where
public keyword
public can be accessed in
vanable or method defined as
the,
same class
(i) same
package
class in the
sub package
(i) same
classes
in the
other
(ii)
in other pACkages
classes
other
(v) packages
other
in
(V)sub class
() protected access
form is
The general
protected datatype variablename;
(or)
protected returntype membername()
datatype variablename;
(or)
retumtype methodname()
variablename;
private protected datatype
(or)
methodname)
private protected returntype
) same class
Interfaces
(ii) student
Creating9 package
The following steps are followed to create our
packages.
package packagename;
Where
package keyword
packagename valid package name
C:jdk1.3bin>md packagenameJ
C:jdk1.3tbin>cd packagename J
C:jdk1.3tbin\packagename>
Store the source file as classname java in the
(iv) .
(v) Compile the file. This creates the .class file in the
subdirectory.
(vi) lf we want to create another package within this
package, store the source file in a subdirectory within
the already created subdirectory. By following tnis
we can create hierarchy of
packages as shown belo
3.84
class1,class2,..classn
class1,class2,..classn
class1,class2,...classn
package3
package2
package1
of
command to create the above hierarchy
(vi) The DOS
package is,
C>md package1
C>cd package1
C:/package1>md package2
C/package1>cd package2
C:/package1/package2>md package3
as shown
required classes
in the packages
Add the
in the above figure.
A 06
s of exception handling
n s i c so fe x
try
catch(Exceptionclass2 obj2)
finally
before exiting
Statements to be executed
4.06
Where
- Keywords
try, catch and finally
Exception class1, Exceptionclass2 Prede fined exceptir
Exceptionclassn classes that can ar
a specitic excepton
Ifan exception occurs within the statementsin
the
block, it is thrown automatically. The thrown
caught by the corresponding catch block. Thenexception
it evee
the statements for handling the
exeote
exception(error).
The finally block statements will be
executed bete
exiting the exception handler.
Try block
try
{
statement 1;
statement n;
Where
try keyword
Statementswho
statement 1, can crealean
exception
statementn
4.07
ero
found, the try block throws the error and is caught by
efiock. In a program there can be any number of try
h block.
calch
DOCks.
Throwingaan exception
t h r o w
throw
the exceptions explicitly. This is called
The general form is
can
we exception.
an
throwing
try
--
Where
- keyword
try, throw
exception class
Exception object already defined
object.
ITuication between threadsS). a pite
Method Description
i n ta v a i l a b l e ( )
This method gives the nurnber of bytes
of input currently available for reading
v o i dc l o s e ( )
This method is used to close the input
SOurce
(ii)
OutputStream is an abstract class and is
data in bytes. This class contains
USed to
steam output
operations. If error occurs
it
numbe
methods to do output
below lis the
throws G
Exception.
The table given methods
Method Description
This method is used to close the
voidclose()
stream
output
This method is used to clear the outpu
void flush()
buffers.
This method is used to write a sirgje
void write(int b)
to an output stream.
byte
This method is used to write a butifer
void write(byte b[ ]})
array b of bytes to an output stream
This method is used to write bytes
m
void write(byte b[ ], | from n
int n, int m) from buffer array b starting
byte.
Character stream classes
are used to do
l/o operato
Character classes.
stream
character
1) Writer
skipped.
m
4.48
Method Description
method is used to close the
abstract void close( ) This
output stream.