Java Leave
Java Leave
2
class Student
name=Anana
city-Vellore":
age=23;
name="Joseph"
city"Chennai "
age-22;
Output 4.2
public void printData() Student name = Anand
Student city = Vellore
System. out.println("Student nane ="+name ) Student age -23
System. out.println("Student city +city) Student name = Joseph
System. out.println("student age "tage) Student city = Chennai
Student age =22
Class Cs
name and age are declared and initialized with thè values Joseph and 20 respectively. The two
age
ods display() and show() print the values of the data members, The data members name and
ispub
declared as private. All theinstance variables are private by default. The display method. )
and hence itcan be accessed fromthe main() method to display the data members; show() Imethodis
netho
display the data members but is defined as private, hence it can't be accessedfrom themain)
The program has commented the invoking line m1. show () otherwisethe compiler will give
Program 4.4
class minimum
a
public static int min (int x, int Y) 10.
int nini
if (x Y)
else
return (min);
88 Java Programming
Program 4.5
public class CallByValue
y 20:
display()method is defined as publ ic,return type is void, display is the name of the method and
a parameter. The value 20 is assigned to the parameter y. In the main )
method the object cbyfr
defined class is created and the value 10is passed as argument to the display ( ) method. As a cop
x is passed changing y does not change the value of x in main () method. Hence the value of xdoes
change sothe output is 10. Program 4.6 also brieflyexplains the call by value technique.
Program 4.6
class byvalue
int data-50;
void change (int data)
ata-data+i00;
11changes will be in the local variable only Output 4.6
In call by reference, however, the reference or in other words address of an argument is passed to
method.Any changes made inside the method will affect the argument's value. It manipulates objects by
reference and hence all object variables are referenced. When the reference is passed to a method, the
parameter thatreceives will refer to the same object. Hence changes to the object insidethe method will
afiect the object used as an argument. Program 4.7 demonstrates the objects being passed by reference
Progran 4.7
class byreference
int data-50;
void change(byreference op)
op.data-op. data+100
changes will be in the instance variable
Output 4.7
public static void main(String args[1) before change 50
after change 150
byreference op=new byreference(),
System.out.println( "before change +op.data) ;
op.change (op) l/passing object
System.out.println("after change "+op data)
Classes and Objects 91
Program 4.8
class Display
void show
()
System.out.println ("Value of a ta); Output 4.8
System.out.println ("Value of b +b) ; Value of a : 5
Value of b : 0
class DefaultConstructor
In Program 4.8 the class name is Display, the variables are a,bas integer type. The variable a is initial
izedwith value 4 and the other variable b is not initialized. There is a constructor namely Display(0 with
value 5. The method
nopararmeters and inside the constructor definition the variable a is assigned with the
show() is of return type void and it prints the values of a, b. The DefaultConstructor is the name ofthe
main class. Then an object data is created for the defined lass. During the data object creation, no values
are passed as arguments to the constructor Display ().On the execution of the statement, Display data =
new Display ()the ; constructor overrides the default value 4 andassigns the value 5to a. When the show ()
method iscalled it prints the value of the a as 5and b does not have any value so the constructor itself assigns
0 value byassessing the datatype to be integer. Hence the value of a is 5 and b 0 is is obtained as output.
class Rectangle
int length:
int breadth:
7iconstructor to initialize length and breadth of rectangle
Rectangle (int l,int b)
length = l;
breadth+ b;
Output 4.9
return (length * breadth);
Area of First Rectangle 5
Area of Second Rectangl:
1/ciass to create rectangle objects and calculate 56
area
class ParaneterizedConstructor
Program 4.14
Class overloading
int cal,
float val,
int add( int x, int y)
return (cal}
cal= z+i0;
return (cal)i
Output 4.14
1loat add (float x floatt y)
10+10=20
15+10=25
val =X + yi 1.5+1.5=3.0
return (val)
class MathS
In Program 4.14 the class nameis overl oading and it has two variables cal as integer, va
Progranm 4.12
class Total{
void sum(int a, int b)
In Program 4.12 the class name is Total and it has two methods named sum(). Since both m
have the samename they are called overloaded methods. The first sum ()method has two argumens
integertypeand the second sum ()method consistsof three arguments a,b, c as integertypes. Ihe
method performs addition of two numbers and the second sum method performs addition of tre
bers. In main ()method an object for the class Total is created as obj. Then with thehelp of objcate
arguments are passed tothe sum ) method. The first call passes three integer values to the sunt
with three argument. The second two integer values to the sum () method with two
call passes g
The execution in an overloaded method depends on the nümber of argument and the data type o
ments passed to the methods. Thus the first passing generates output 30 and the second time tne
40.Program 4.13explicitly describes method overloading by changing the data type of the g
Program 4.13
Class Calculation
SYstem,out.println(a + b)
height-depth=length=10;
3
Box(int x, int y)
height-x;
depth=y:
}
Box (int x, int y 1nt z)
Output 4.15
height=x;
depth=Yi Depth of a: 10
length=zi Depth of b: 15
Depth of c: 15
Class BoxClass
andlengt
In Program 4.15 the class Box containsthree variables height, depth
different co
a default
Box () is
is a dela
gers. It also has three constructors named Box().The first constructor and dep
since it does not have any parameters but initializes the variables length, height
it
assigt
and
Progranm 4.16
class Rectangle {
int length;
int breadth:
Rectangle (int l,int b) {
length = l;
breadth = b;
Rectangle getRectangleobject () {
Rectangle rect = new Rectangle (10,20)
zeturn rect; Output 4.16
obl.length: 40
obl.breadth: 50
Class RetOb ob2.length: 10
public static void main (String args[]) { ob2.breadth: 20
Rectangle ob1 = new Rectangle (40, 50);
Rectargle ob2;
ob2 = ob1.getRectangleObject ();
Systen, out println("obl,length: " + obl.length)
Systero. out.príntln(ob1, breadth:"+
ob1.breadth);
System, out.println("ob2.length: " + ob2,length)
System, out.println ("ob2.breadth;"+ ob2, breadth)
;
In Program 4.17 the class student has variable rollnoas integer, name and city as type s
constructor student ()receives three parameters rollno, name and city and assigns them
variables. In main(0 the objects s1, s2 are createdforthe defined class and one integer, two Su
Printing
s
are passed as arguments. The next statements print the content of the objects s1, s2.
a
s2 displaysthe hashcode values of the objects but when one want to print the values ofthese
becomes a complex task. Since the Java compiler internally calls toString 0 method overmu
ll
method will return tostring
thespecified values. The problem can be solved withthe help of
Program 4.18
Class Student
int rolino:
String name;
String cityi
Student(int rollno, String name, String city)
this.rollno=rollno;
this. panesame;
thís,cítyscityi
Output 4.18
public String tostring (07/overriding the
toString() nethod 101 Joseph Tirupattur
return rollno+ +nane "+city; l02 Joe Vellore
int a;
public void get ()
else
(String[] args)
public static void main
{
Even_0r_0dd ();
Even_Or_Odd e=new
e.get (0;
e.display ()
;
Output
11 Por Even
Enter any Number : 2
Given Number 2 is :Even
11 For 0dd
Enter any Number: 125: Odd
is
Given Number 125
class nesteddemo
Output
10
n20
7. Programto implement array of objects.
import java.lang. *;
public class EmployeeTest
Employee [] staff =
new Employee [3]:
3500);
staff [O] = new Employee ("Harry Hacker",
Cracker", 7500);
staff [1] = new Employee ("Carl
3800);
staff [2] = new Employee ("Tony Tester",
for (int i = 0; i < 3; i++)
staff [i].print();
class Employee
name= n;
salary = S;
Output