CSE23355 Labsheet 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

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,

• void test(): Takes no parameters.


• void test(int a): Takes one integer parameter.
• void test(int a, int b): Takes two integer parameters.
• double test(double a): Takes one double parameter and returns a double value.
In the driver class the function call
(i) ob.test() invokes test() giving us the output ‘No parameters’.
(ii) ob.test(10) invokes test(int a) since 10 ais an int value , giving us the output ‘a: 10’.
(iii) ob.test(10,20) invokes test(int a,int b) since 10 and 20 are int values, giving us the output ‘a and b: 10 20’.
The last function call ob.test(123.25) invokes the function double test(double a) which returns the square of 123.25 to the variable result
which is printed to give the output 15190.5625.
The method calls demonstrates that correct overloaded method is called based on the number and types of arguments passed.

_____________________________________________________________________________
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,

• void test(): Takes no parameters.


• void test(int a, int b): Takes two integer parameters.
• void test(double a): Takes one double parameter.
• double test(double a): Takes one double parameter and returns a double value.
In the driver class the function call
(i) ob.test() invokes test() giving us the output ‘No parameters’.
(ii) ob.test(10,20) invokes test(int a,int b) since 10 and 20 are int values, giving us the output ‘a and b: 10 20’.
(iii) ob.test(i) invokes test(double a) giving the output ‘Inside test(double) a: 88.0. Here the type of ‘i’ is int but it matches with the
method test(double a) because int values are implicitly converted to double by Java.
(iv) ob.test(123.2) invokes test(double a) since the 123.5 is double value, giving the output ‘Inside test(double) a: 123.2’.
The method calls demonstrates that correct overloaded method is called based on the number and types of arguments passed.

_____________________________________________________________________________
Practice Lab Assignment -III Execute the below program and write your observations
package Labsheet5;

public class Driver{


double overloadedMethod(double d){
return d*=d;
}
int overloadedMethod(int i){
return overloadedMethod(i*=i);
}
float overloadedMethod(float f){
return overloadedMethod(f*=f);
}
public static void main(String[]args){
Driver m=new Driver();
System.out.println(m.overloadedMethod(100));
}
}

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>

You might also like