CSE23355 Labsheet 5
CSE23355 Labsheet 5
CSE23355 Labsheet 5
SHIVANAND R
AM.SC.U4CSE23355
Practice Lab Assignment -I Execute the below program and write your observations
package Labsheet5;
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {System.out.println("a: "+ a);
}// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: "+ a + " "+ b);
}
// overload test for a double parameter.
double test(double a) {
System.out.println("double a: "+ a);
return a*a;
}
}
public class Driver {
public static void main(String[] args){
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): "+ result);
}
}
Output
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
Justification
The output is as above. In the code,
_____________________________________________________________________________
Practice Lab Assignment -II Execute the below program and write your observations
package Labsheet5;
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: "+ a + " "+ b);
}
// overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: "+ a);
}
}
public class Driver {
public static void main(String[] args) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test();
ob.test(10, 20);
ob.test(i);
ob.test(123.2);
}
}
Output
No parameters
a and b: 10 20
Inside test(double) a: 88.0
Inside test(double) a: 123.2
Justification
The output is as above. In the code,
_____________________________________________________________________________
Practice Lab Assignment -III Execute the below program and write your observations
package Labsheet5;
Output
Exception in thread "main" java.lang.StackOverflowError
at Labsheet5.Driver.overloadedMethod(Driver.java:8)
at Labsheet5.Driver.overloadedMethod(Driver.java:8)
Justification
The output will be a StackOverflowError which is related to infinite execution of recursion.
The method call m.overloadedMethod(100) matches to the method “int overloadedMethod(int i) “ since 100 is an integer.
Inside the overloadedMethod(int i) It squares the integer ‘i’. Then, it recursively calls overloadedMethod(int i) again with the squared value.
This recursion runs infinitely because there is no condition to terminate the recursion. Each time the method is calls itself, it further squares
the integer argument and recursively calls the method using the squared value as parameter, leading to an infinite recursion thus giving the
error.
_____________________________________________________________________________
Lab Assignment IV-Overloading
package Labsheet5;
class Addition{
float add(float a, float b) {
return a+b;
}
void add(float r1, float r2,float i1,float i2) {
System.out.println("c1 ="+r1+" + "+i1+"i");
System.out.println("c2 ="+r2+" + "+i2+"i");
System.out.println("Sum = "+(r1+r2)+" + "+(i1+i2)+"i");
}
Vector add(Vector u,Vector v) {
System.out.println("u = <"+u.k1+","+u.k2+">");
System.out.println("v = <"+v.k1+","+v.k2+">");
Vector sum1=new Vector();
sum1.k1=u.k1+v.k1;
sum1.k2=u.k2+v.k2;
Vector diff=new Vector();
diff.k1=u.k1-v.k1;
diff.k2=u.k2+v.k2;
System.out.print("u - v : ");
System.out.println("<"+(u.k1-v.k1)+","+(u.k2+v.k2)+">");
System.out.print("u + v : ");
return sum1;
}
}
class Vector{
int k1,k2;
Vector(){
k1=1; k2=1;
}
Vector(int a,int b){
k1=a;
k2=b;
}
}
public class Driver{
public static void main(String[] suii) {
System.out.println("ADDITION USING METHOD OVERLOAD");
System.out.println("-------------------------------");
Addition a=new Addition();
System.out.println("Float addition : ");
float fsum=a.add(20, 30);
System.out.println("Sum = "+fsum);
System.out.println("Complex addition : ");
a.add(4,8,-2,5);
Vector u=new Vector(3,4);
Vector v=new Vector(5,-1);
System.out.println("Vector addition : ");
Vector vsum=a.add(u, v);
System.out.println("<"+vsum.k1+","+vsum.k2+"> ");
}
}
Output
ADDITION USING METHOD OVERLOAD
-------------------------------
Float addition :
Sum = 50.0
Complex addition :
c1 =4.0 + -2.0i
c2 =8.0 + 5.0i
Sum = 12.0 + 3.0i
Vector addition :
u = <3,4>
v = <5,-1>
u - v : <-2,3>
u + v : <8,3>