0% found this document useful (0 votes)
39 views

CSD J0201 JAVA (LAB-2) : Objectives: 1. Method Overloading 2. Use of Constructors

The document provides instructions and code examples for exercises on method overloading, use of constructors, static keywords, and overriding toString() and equals() methods in Java. The exercises demonstrate: 1) Overloading sum() methods to add doubles and ints and observing which is called based on argument types 2) Overloading sum() to add double-float and float-double and observing output 3) Using static variables and methods and noting errors 4) Creating Box class with multiple constructors and getter methods and comparing Box objects 5) Overriding toString() and equals() in Box class and comparing Boxes for equality

Uploaded by

Arul Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

CSD J0201 JAVA (LAB-2) : Objectives: 1. Method Overloading 2. Use of Constructors

The document provides instructions and code examples for exercises on method overloading, use of constructors, static keywords, and overriding toString() and equals() methods in Java. The exercises demonstrate: 1) Overloading sum() methods to add doubles and ints and observing which is called based on argument types 2) Overloading sum() to add double-float and float-double and observing output 3) Using static variables and methods and noting errors 4) Creating Box class with multiple constructors and getter methods and comparing Box objects 5) Overriding toString() and equals() in Box class and comparing Boxes for equality

Uploaded by

Arul Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

CSD J0201 JAVA [LAB-2]


return (a+b); }// End of sum public static void main(String x[]) { System.out.println(sum(5.6f,4.5f)); System.out.println(sum(5.6,4.5)); }// End of main }// End of class Test Exercise 3[Use of static keyword ] [Type the Following Java code in file Lab2Ex3.java and compile and execute it. Note down your Observation. If there are any errors then you had to correct them before execution] class A { private public static

int int

a,b,c; d;

// instance field // class variable

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

CSD J0201 JAVA [LAB-2]


Box(Box other) { this.length = other.length; this.width = other.width; this.height =other.height; } // Getter Methods public double getLength() public double getWidth() public double getHeight() public double public double }// End of class Box { { { return this.length; return this.width; return this.height; } } } }

area(){ return 2.0*(length*width + width*height + height*length); volume() { return (length*width*height); }

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

System.out.println(Hello); System.out.println(Hi); System.out.println(Hello); System.out.println(Hi);

if(b3.equals(b4)) else

}// End of main() }// End of class BoxTest

3|Page

CSD J0201 JAVA [LAB-2]

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

You might also like