JAVA LAB ASSIGNMENT
UNIT 2
Harsh Baldaniya
20BCP073
CE-DIV.1
1 Harsh Baldaniya – 20BCP073
1. Write a program to create a “distance” class with methods where distance is
computed in terms of feet and inches, how to create objects of a class.
package unit_02;
import java.util.Scanner;
public class Distance {
int x;
int y;
double coordinates() {
return Math.sqrt(x * x + y * y);
}
void distanceInFoot() {
double d1 = coordinates();
System.out.println("The distance in foot is : " + d1 * 3.28084 + "
feets");
}
void distanceInInches() {
double d2 = coordinates();
System.out.println("The distance in inch is : " + d2 * 39.3701 + "
inches");
}
public static void main(String[] args) {
2 Distance d = new Distance(); Harsh Baldaniya – 20BCP073
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of x and y coordinates : ");
int x = sc.nextInt();
int y = sc.nextInt();
d.x = x;
d.y = y;
d.coordinates();
d.distanceInFoot();
d.distanceInInches();
sc.close();
}
2. Modify the “distance” class by creating constructor for assigning values (feet and
inches) to the distance object. Create another object and assign second object as
reference variable to another object reference variable. Further create a third
object which is a clone of the first object.
package unit_02;
import java.util.Scanner;
public class Distance2 {
public double distance;
public double feet;
public double inches;
public Distance2(double distance) {
this.distance = distance;
this.feet = distance * 3.281;
this.inches = distance * 39.370;
}
public double getDistance() {
return distance;
}
3 Harsh Baldaniya – 20BCP073
public double getFeet() {
return feet;
}
public double getInches() {
return inches;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Enter distance in metre : ");
Double value = sc.nextDouble();
Distance2 distance = new Distance2(value);
System.out.println("Distance entered by user is " +
distance.getDistance());
System.out.println(value + " m in feet is " + distance.getFeet());
System.out.println(value + " m in inches is " + distance.getInches());
sc.close();
}
3. Write a program in Java in which a subclass constructor invokes the constructor
of the super class and instantiate the values.
package unit_02;
class Superclass {
public void printMethod() {
System.out.println("Printed in Superclass.");
}
}
public class Question_3 extends Superclass {
public void printMethod() {
super.printMethod();
System.out.println("Printed in Subclass");
}
public static void main(String[] args) {
Question_3 s = new Question_3();
s.printMethod();
}
}
4 Harsh Baldaniya – 20BCP073
4. Write a program in Java to develop overloaded constructor. Also develop the copy
constructor to create a new object with the state of the existing object.
package unit_02;
class Division {
String name;
String rollNo;
int div;
Division(String n, String r) {
this.name = n;
this.rollNo = r;
}
Division(String n, String r, int c) {
this.name = n;
this.rollNo = r;
this.div = c;
}
Division(Division object) {
name = object.name;
rollNo = object.rollNo;
5 div = object.div; Harsh Baldaniya – 20BCP073
}
}
public class Question_4 {
public static void main(String[] args) {
Division d1 = new Division("HarshBaldaniya", "20BCP073");
Division d2 = new Division("X", "123", 1);
Division d3 = new Division(d2);
System.out.println(d3.name);
System.out.println(d3.rollNo);
System.out.println(d3.div);
}
5. Write a program to show the difference between public and private access
specifiers. The program should also show that primitive data types are passed by
value and objects are passed by reference and to learn use of final keyword
package unit_02;
class C {
private int x;
public int y;
final int me = 55;
C(int y) {
this.y = y;
// me = 55; //we can't because me is final
}
void xMultiplyWithNumber(int num) {
x = 15;
System.out.println("The value of private x * 55 is " + x * num);
}
}
class Changing {
String s;
Changing(String s) {
this.s = s;
6 } Harsh Baldaniya – 20BCP073
}
public class Question_5 {
public static void main(String[] args) {
String name = "You can't change me because I am passing by value
not reference";
System.out.println(name);
change(name);
System.out.println(name);
Changing str = new Changing("By object reference, You can do");
System.out.println(str.s);
changeByReference(str);
System.out.println(str.s);
static void change(String s) {
s = "look I am going to You ..........oh no I can't yaar";
}
static void changeByReference(Changing str) {
str.s = "look I changed You , thanks to Object reference";
}
}
7 Harsh Baldaniya – 20BCP073
6. Write a program to show the use of static functions and to pass variable length
arguments in a function.
package unit_02;
public class Question_6 {
public static void display(String... length) {
System.out.println("Number of length values entered are: " +
length.length);
System.out.println("The argument values are: ");
for (String i : length) {
System.out.println(i);
}
public static void main(String[] args) {
display("Computer", "coding");
display(); // Empty argument
display("JAVA");
8 Harsh Baldaniya – 20BCP073
7. Develop minimum 4 program based on variation in methods i.e., passing by value,
passing by reference, returning values and returning objects from methods.
package unit_02;
public class Question_7 {
String changing = "Bounty";
int n1, n2;
// pass by value
void change_pass_by_value(String s) {
s = "HarshBaldaniya";
}
// pass by reference
void change_pass_by_reference(Question_7 s) {
changing = "Hunting";
}
void setValue(int x, int y) {
n1 = x;
n2 = y;
}
int returning_Value() {
return n1 + n2;
}
Question_7 returningObject() {
Question_7 x = new Question_7();
9 x.setValue(19, 90); Harsh Baldaniya – 20BCP073
return x;
}
public static void main(String[] args) {
Question_7 ro = new Question_7();
ro = ro.returningObject();
System.out.println(ro.returning_Value());
}
8. Write a program that implements two constructors in the class. We call the other
constructor using ‘this’ pointer, from the default constructor of the class.
package unit_02;
public class Question_8 {
Question_8() {
this(3);
System.out.println("This is the default constructor");
}
Question_8(int num) {
System.out.println("The entered number is: " + num);
}
public static void main(String[] args) {
new Question_8();
10 Harsh Baldaniya – 20BCP073
9. Write a program in Java to demonstrate single inheritance, multilevel inheritance
and hierarchical inheritance.
package unit_02;
class people {
void display() {
System.out.println("This is the people's class");
}
}
class teacher extends people {
void display() {
System.out.println("Single Inheritance");
}
}
class student extends teacher {
void display() {
System.out.println("Multilevel Inheritance");
}
}
class professional extends people {
void display() {
System.out.println("Hierarchial Inheritance");
}
}
11 Harsh Baldaniya – 20BCP073
public class Question_9 {
public static void main(String[] args) {
people p = new people();
student s = new student();
teacher t = new teacher();
professional pro = new professional();
p.display();
s.display();
t.display();
pro.display();
}
}
10. Java Program to demonstrate the real scenario (e.g., bank) of Java Method
Overriding where three classes are overriding the method of a parent class.
Creating a parent class.
package unit_02;
class bank {
int cash() {
return 0;
}
}
class bank1 extends bank {
int cash() {
return 20;
}
}
class bank2 extends bank {
int cash() {
return 50;
}
}
class bank3 extends bank {
int cash() {
return 70;
12 } Harsh Baldaniya – 20BCP073
}
public class Question_10 {
public static void main(String[] args) {
bank1 B1 = new bank1();
bank2 B2 = new bank2();
bank3 B3 = new bank3();
System.out.println("Bank B1 has " + B1.cash() + " Crores");
System.out.println("Bank B2 has " + B2.cash() + " Crores");
System.out.println("Bank B3 has " + B3.cash() + " Crores");
}
}
11. Write a program that implements simple example of Runtime Polymorphism with
multilevel inheritance. (e.g., Animal or Shape)
package unit_02;
class Animals {
public void ani() {
System.out.println("This is Animal Class");
}
}
class herbivores extends Animals {
public void her() {
System.out.println("Herbivores eat plants");
}
}
class deer extends herbivores {
public void de() {
System.out.println("Deer is a herbivores animal");
}
}
public class Question_11 {
public static void main(String[] args) {
Animals a = new Animals();
herbivores h = new herbivores();
13 deer d = new deer(); Harsh Baldaniya – 20BCP073
a.ani();
h.her();
d.de();
}
}
12.Write a program to compute if one string is a rotation of another. For example, pit
is rotation of tip as pit has same character as tip.
Solutionpackage unit_02;
import java.util.Scanner;
public class Question_12 {
static boolean func(String str1 , String str2)
{
int i = 0;
int j=str2.length()-1;
while(j>=0 && i<str1.length())
{
if(str1.charAt(i) != str2.charAt(j))
{
return false;
}
i++;
j--;
}
return true;
}
public static void main(String[] args) {
System.out.println("Enter the two strings");
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
14 if (func(s1 , s2)) Harsh Baldaniya – 20BCP073
{
System.out.println("String 1 : "+ s1 + " is rotation of another
string 2 : "+ s2);
}
else
{
System.out.println("String 1 : "+ s1 + " is not rotation of
another string 2 : "+ s2);
}
sc.close();
}
}
13.Describe abstract class called Shape which has three subclasses say Triangle,
Rectangle, Circle. Define one method area() in the abstract class and override this
area() in these three subclasses to calculate for specific object i.e. area() of
Triangle subclass should calculate area of triangle etc. Same for Rectangle and
Circle.
package unit_02;
import java.util.Scanner;
public class Question_13 {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
System.out.println("Enter the value of radius");
float r = cin.nextFloat();
Circle c = new Circle(r);
System.out.format("Area of this circle is %.2f Unit", c.area());
cin.close();
}
abstract class Shape {
abstract double area();
}
15 Harsh Baldaniya – 20BCP073
class Circle extends Shape {
float radius;
Circle(float r) {
radius = r;
}
@Override
double area() {
return (Math.PI * radius * radius);
}
}
class Rectangle extends Shape {
float length, width;
Rectangle(float l, float b) {
length = l;
width = b;
}
@Override
double area() {
return (length * width);
}
}
class Triangle extends Shape {
float height, base;
16 Harsh Baldaniya – 20BCP073
14.Write a program in Java to demonstrate multiple inheritance.
package unit_02;
public class Question14 {
public void ques() {
System.out.println("Parent class");
}
public class Dog extends Question14 {
void run() {
System.out.println("Dog is running");
}
}
public class puppy extends Dog {
void pup() {
System.out.println("Puppies are so cute");
}
}
public static void main(String[] args) {
Question14 q = new Question14();
Dog d = q.new Dog();
puppy p = d.new puppy();
d.run();
p.pup();
p.ques();
}
}
17 Harsh Baldaniya – 20BCP073
15. A) Write an application that illustrates method overriding in the same package
and different packages.
B) Also demonstrate accessibility rules in inside and outside packages.
#SubPackage inside_package:
#Class P15:
package
} unit2.P15.inside_package;
public the
// inside static package
void main(String arg[])
public{ class exampleIn {
public static void main(String arg[])
{ B obj=new B();
Protection p1=new Protection();
obj.show(); //show() method of class-B will called.
System.out.println("n= "+p1.n);
} System.out.println("n_pub= "+p1.n_pub);
}
} }
#Class Protection:
package unit2.P15.Part2;
#Class OutsidePackageExample:
// inside the package
public class
package Protection {
unit2.P15;
int n=1;
public int new_n = 2;
import Question15.part2.Protection;
}
#P15 PACKAGE:
public class OutsidePackageExample {
#P15 Class:
package unit2.P15;
class public
A static void main(String arg[]) {
18 { Harsh Baldaniya – 20BCP073
void show()p1 = new Protection();
Protection
{
// System.out.println("Inside Class-A");
System.out.println("n= " + p1.n); // this line generate error without it, output like following
}
}class System.out.println("n_pub=
B extends A " + p1.n_pub);
{
}
//Overriding method show of class A
void show()
}{
System.out.println("Inside Class-B");
}
}
public class exampleOut
{
public class Protection {
int n=1;
public int n_pub = 2;