CSD J0201 JAVA (LAB-2) : Objectives: 1. Method Overloading 2. Use of Constructors
CSD J0201 JAVA (LAB-2) : Objectives: 1. Method Overloading 2. Use of Constructors
Exercise 1 [Method Overloading] [Type the Following Java code in file Lab2Ex1.java and compile and execute it. Note Down your Observation] class Test { public static double sum( double a, double b) //sum(double,double) { System.out.println("Sum: Double Double Called"); return (a+b); }// End of sum public static int sum( int a, int b) //sum(int,int) { System.out.println("Sum: int int Called"); return (a+b); }// End of sum public static void main(String x[]) { System.out.println(sum(20,10)); // which method will be invoked? System.out.println(sum(20.5,10.7)); // which method will be invoked? System.out.println(sum(20.5f,10)); // which method will be invoked? System.out.println(sum(20.0,10)); // which method will be invoked? byte b = 10; short s = 8; System.out.println(sum(b,s)); // which method will be invoked? }// End of main }// End of class Test Exercise 2[Method Overloading] [Type the Following Java code in file Lab2Ex2.java and compile and execute it. Note Down your Observation] class Test { public static double sum( double a, float b) { System.out.println("Sum: Double Float Called"); return (a+b); }// End of sum public static double sum( float a, double b) { System.out.println("Sum: Float Double Called"); 1|Page
int int
a,b,c; d;
public static void main(String[] args) { System.out.println(a=+this.a); System.out.println(b=+this.b); System.out.println(c=+this.c); System.out.println(d=+this.d); }// End of main() }// End of class A Exercise 4: [Type the Following Java code in file Lab2Ex4.java and compile and execute it. Note down your Observation. If there are any errors then you had to correct them before execution] class Box { private double length,wdith,height; Box() { this.length = this.width = this.height =10; } Box(double side) { this.length = this.width = this.height =side; } Box(double length, double height) { this.length = this.width = length; this.height =height; } Box(double length, double height, double height) { this.length = length; this.width = width; this.height =height; } 2|Page
class BoxTest { public static void main(Straing args[]) { Box b1 = new Box(); Box b2 = new Box(); Box b3 = new Box(); Box b4 = new Box(b4); System.out.println(Area :+b1.area+Volume:+b1.volume); If(b1 == b2) else System.out.println(Hello); System.out.println(Hi); System.out.println(Hello); System.out.println(Hi);
if(b3.equals(b4)) else System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); b1 = b2; b3 = b4; If(b1 == b2) else
if(b3.equals(b4)) else
3|Page
Exercise 5 Add the following methods in Box class of Exercise 4 at the end public String toString() { Return ( Length: +this.length +Width: +this.width+ Height: +this.height); } public boolean equals(Box other) { return this.area() == other.area(); } public static boolean equals(Box first, Box second) { return first.area() == second.area(); } Add the following lines in the main() method of BoxTest at the end if((Box.equals(b3,b4)) else System.out.println(Hello); System.out.println(Hi);
Now Recompile and Execute the code and observe the o/p?
4|Page