0% found this document useful (0 votes)
51 views25 pages

Arnav Adarsh LAB 5&6

The document contains the code solutions to various exercises on Java concepts like packages, arrays, polymorphism and inheritance. It includes code snippets to demonstrate: 1. Creating packages and calling methods between packages 2. Declaring, initializing and traversing arrays 3. Method overloading, overriding and different types of polymorphism through examples 4. Inheritance hierarchy between classes like Animal, Herbivores etc. and demonstrating runtime polymorphism 5. Other inheritance and polymorphism examples It provides the full code for each exercise with comments explaining the purpose and output.

Uploaded by

arnav adarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views25 pages

Arnav Adarsh LAB 5&6

The document contains the code solutions to various exercises on Java concepts like packages, arrays, polymorphism and inheritance. It includes code snippets to demonstrate: 1. Creating packages and calling methods between packages 2. Declaring, initializing and traversing arrays 3. Method overloading, overriding and different types of polymorphism through examples 4. Inheritance hierarchy between classes like Animal, Herbivores etc. and demonstrating runtime polymorphism 5. Other inheritance and polymorphism examples It provides the full code for each exercise with comments explaining the purpose and output.

Uploaded by

arnav adarsh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ARNAV ADARSH

209301591
SEC- F

LAB 5&6

Q1. Exercise1:

Create a package with name “First” with class as “One”


and declare a method with name “show” and display a
console statement “inside first package”. Create one more
package with name “Second” with class as “Two” and
declare main method and call the message “show” of
“One” class.

CODE-

Exercise 1

package First;

public class one


{
public static void main(String[] args)

{
show();
}

public static void show()


{
System.out.println("inside first package");
System.out.println("Arnav Adarsh_209301591_Sec
F");

}
}
PART 2
package Second;

import First.one;

public class two {

public static void main(String[] args) {


one.show();
System.out.println("Arnav Adarsh_209301591_Sec
F");
}
}
Exercise2:

For the exercise 1 try and test with all the access
modifiers and draw a chart what happens when 1. Same
package sub class2. Same package non-sub class3.
Different package sub class4. Different package non-sub
class

CLASS TYPE PUBLIC PRIVATE PROTECTED DEFAULT


Same Package Accessibl Not Accessible Accessible
Sub Class e Accessibl
e
Same Package Accessibl Not Not Accessible
Non sub class e Accessibl Accessible
e
Different Accessibl Not Accessible Not
Package Sub e Accessibl Accessible
class e
Different Accessibl Not Not Not
Packages Non e Accessibl Accessible Accessible
Sub class e
EXERCISE 3
In reference to exercise 1, create a sub package
under “First” with name “First_Senior” with class as
“Three” and declare a method with name “show” and
display a console statement “inside
first_Seniorpackage”. Now through class s
“Two”againcall the message “show” of “Three” class.

Code-
package First_Senior;

public class Three {


public static void main(String[] args)
{
show();
}

public static void show()


{
System.out.println("inside first_Seniorpackage");
System.out.println("Arnav Adarsh_209301591_Sec
F");

}
}
2. ArraysExercise

EXAMPLE 1:
Write a java program to illustrate how to declare,
instantiate, initialize, and traverse the Java array.

package Array;

public class java_array {


public static void main(String[] args) {
int a[]=new int[]{1,2,3,4,5,6,7,8,9,10};

for(int i = 0; i<a.length; i++){


System.out.println(a[i]);
}

}
}
Exercise 2:
Write a program to print the array elements using for-each
loop
package Array;

public class java_array {


public static void main(String[] args)
{
int a[]=new int[]{1,2,3,4,5,6,7,8,9,10};

for(int i = 0; i<a.length; i++)


{
System.out.println(a[i]);
}
Exercise 3:
Write a program to demonstrate the way of passing an
array to method.
package Array;

public class java_array {


public static void main(String[] args)
{
int a[]=new int[]{1,2,3,4,5,6,7,8,9,10};

for(int i = 0; i<a.length; i++)


{
System.out.println(a[i]);
}
element(2);
}

public static void element(int a)


{
System.out.println("The element called after
traversing is: ");
System.out.println(a);

}
}
3. Polymorphism and
Inheritance
Excercise 1
Write a java program for method overloading

package Polymorphism;

public class excercise_1


{
public static void main(String[] args)
{
System.out.println("Arnav Adarsh_209301591_Sec
F");

sayHi();
sayHi("Arnav says Hello");}

public static void sayHi()


{
System.out.println("Hi");

public static void sayHi(String name)


{
System.out.println("Hi " + name);
}}
Exercise 2
Write a java program for method overriding

//Java Program to illustrate the use of Java Method


Overriding
//Creating a parent class.
class Vehicle{
//defining a method
void run(){System.out.println("Vehicle is running");}
}
//Creating a child class
class Bike2 extends Vehicle{
//defining the same method as in the parent class
void run(){System.out.println("Bike is running
safely");}

public static void main(String args[]){


Bike2 obj = new Bike2();//creating object
obj.run();//calling method
}
}
EXERCISE 3
Write a program to illustrate compile-time polymorphism in
java
package Polymorphism;
class excercise_3{
void disp(int number){
System.out.println ("method:" + number);
}
void disp(int number1, int number2){
System.out.println ("method:" + number1 + "," +
number2);
}
double disp(double number) {
System.out.println("method:" + number);
return number;
}
}

class CompileTimePolymorphismDemo
{
public static void main (String args [])
{
excercise_3 obj = new excercise_3();
double result;
obj.disp(40);
obj.disp(50, 30);
result = obj.disp(5.1);
System.out.println("Answer is:" + result);
}
}
Exercise 4:
Write a program to illustrate run-time polymorphism
in java by creating one superclass Animal and three
subclasses, Herbivores, Carnivores and Omnivores.
Subclasses extend the superclass and override its eat()
method. We will call the eat() method by the reference
variable of Parent class, i.e. Animal class. As it refers to
the base class object and the base class method
overrides the superclass method, the base class method
is invoked at runtime.

package Polymorphism;
import java.util.Scanner;

class animal
{
public void eat()
{
System.out.println("Animal can eat
anything");
}
}
class Herbivore extends animal
{

public void eat()


{
System.out.println("The animal is eats green
leafy veggies");
}
}
class Carnivore extends animal
{
public void eat()
{
System.out.println("The animal is eats
Meat");
}
}
class Omnivore extends animal
{
public void eat()
{

System.out.println("The animal is eats


Veggies and Meat");
}
}

public class excercise_4


{
public static void main(String[] args)
{

animal a1 = new Herbivore();


animal a2 = new Carnivore();
animal a3 = new Omnivore();
a1.eat();
a2.eat();
a3.eat();

System.out.println("Arnav
Adarsh_209301591_secF");

}
}

Exercise 5:
Create a class with a method that prints "This is parent
class" and its subclass with another method that prints
"This is child class". Now, create an object for each of the
class and call1 -method of parent class by object of
parent class2 -method of child class by object of child
class3 -method of parent class by object of child class

package Polymorphism;
import java.util.Scanner;

class parent
{
void display_parent()
{System.out.println("This is the parent class");}

}
class child extends parent
{
void display_child()
{
System.out.println("This is the child
class");
}
}

public class excercise_5


{
public static void main(String[] args) {

parent p=new parent();


child c=new child();
p.display_parent();
c.display_child();
c.display_parent();

System.out.println("Arnav
Adarsh_209301591_secF");
}
}
Exercise 6:
Create a class named 'Member' having the following
members: Data members
1 -Name
2 -Age
3 -Phone number
4 -Address
5 – Salary
It also has a method named 'printSalary' which prints the
salary of the members. Two classes 'Employee' and
'Manager' inherits the 'Member' class.

The 'Employee' and 'Manager' classes have data


members 'specialization' and 'department' respectively.

Now, assign name, age, phone number, address and


salary to an employee and a manager by making an
object of both classes and print the same.

package Polymorphism;

class member
{
public String Name;
public int age;
public int ph_no;
public String address;
public int salary;
public void printSalary()
{
System.out.println("The salary of the member
is:"+salary);
}
}
class employee extends member
{
String specialization;

}
class manager extends member
{
String department;
}

public class excercise_6


{
public static void main(String[] args) {
System.out.println("Arnav
Adarsh_209301591_SecF");

employee employee1=new employee();


manager manager1=new manager();
employee1.Name="Arnav";
employee1.age=19;
employee1.ph_no=99888776;
employee1.address="221B baker street,London";
employee1.salary=155500;
employee1.specialization="Virtual Reality";

manager1.Name="Harshit";
manager1.age=21;
manager1.ph_no=8887763;
manager1.address="army colony ramgarh,
chattisgarh";
manager1.salary=155500;
manager1.department="CSE";

System.out.println("Deatils of employee are


as follows:");
System.out.println("Name-"+employee1.Name);
System.out.println("Age-"+employee1.age);

System.out.println("Adress-"+employee1.address);
System.out.println("Phone
number-"+employee1.ph_no);
System.out.println("Salary-"+employee1.salary);

System.out.println("Specialization-"+employee1.specializa
tion);
employee1.printSalary();

System.out.println("The Details of the


Manager are as follows: ");
System.out.println("Name-"+manager1.Name);
System.out.println("Age-"+manager1.age);

System.out.println("Adress-"+manager1.address);
System.out.println("Phone
number-"+manager1.ph_no);

System.out.println("Salary-"+manager1.salary);

System.out.println("Department-"+manager1.department);
manager1.printSalary();

}
}
Exercise 7:
Create a class named 'Rectangle' with two data members
'length' and 'breadth' and two methods to print the
area and perimeter of the rectangle respectively.

Its constructor having parameters for length and


breadth is used to initialize length and breadth of the
rectangle. Let class 'Square' inherit the 'Rectangle'
class with its constructor having a parameter for its side
(suppose s) calling the constructor of its parent class
as 'super(s,s)'.

Print the area and perimeter of a rectangle and a


square.
Code-

package Polymorphism;

import java.util.Scanner;

class rectangle
{ private int length;
private int breath;
rectangle(int length,int breath)
{
this.length=length;
this.breath=breath;
}
public void area()
{
System.out.println("The
area:"+length*breath);
}
public void perimeter()
{
System.out.println("The
perimeter:"+(2*(length+breath)));
}
}
class square extends rectangle
{
square(int s)
{
super(s, s);
}

public class excercise_7


{

public static void main(String[] args)


{
System.out.println("Arnav
Adarsh_209301591_SecF");

square sqr1=new square(10);//Declaring and


Initiallizing a square with side 5
rectangle rect1=new rectangle(45,70);
//Declaring and Initiallizing a rectangle with length 3
and breath 7
System.out.println("The area of rectangle:");
rect1.area();
System.out.println("The perimeter of
rectangle:");
rect1.perimeter();
System.out.println("The area of square:");
sqr1.area();
System.out.println("The perimeter of
square:");
sqr1.perimeter();}}
Exercise 8
Now repeat the exercise 7 to print the area of 10 squares.

package Polymorphism;

import java.util.Scanner;

class Rectangle
{ private int breath;
private int length;

Rectangle(int length, int breath)


{
this.breath=breath;
this.length=length;

}
public void area()
{
System.out.println("The area in
cm:"+length*breath);
}
public void perimeter()
{
System.out.println("The perimeter
is:"+(2*(length+breath)));
}
}
class Square extends Rectangle
{
Square(int side)
{
super(side, side);
}

public class excercise_8


{

public static void main(String[] args)


{Scanner Sc=new Scanner(System.in);
System.out.println("enter the number of
squares for which you want the area");
int n=Sc.nextInt();

Square sqr[]=new Square[10];

for(int i=1;i<n+1;i++) //here ive done i=1


and n+1 so that it
//helps in the next
sout line for presentation
{
System.out.println("Enter the size of
sides of square " +i);
int x=Sc.nextInt();
sqr[i]=new Square(x);
sqr[i].area();
}
System.out.println("Arnav
Adarsh_209301591_SecF");

}
}

You might also like