Arnav Adarsh LAB 5&6
Arnav Adarsh LAB 5&6
209301591
SEC- F
LAB 5&6
Q1. Exercise1:
CODE-
Exercise 1
package First;
{
show();
}
}
}
PART 2
package Second;
import First.one;
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
Code-
package First_Senior;
}
}
2. ArraysExercise
EXAMPLE 1:
Write a java program to illustrate how to declare,
instantiate, initialize, and traverse the Java array.
package Array;
}
}
Exercise 2:
Write a program to print the array elements using for-each
loop
package Array;
}
}
3. Polymorphism and
Inheritance
Excercise 1
Write a java program for method overloading
package Polymorphism;
sayHi();
sayHi("Arnav says Hello");}
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
{
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");
}
}
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.
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;
}
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("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("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.
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);
}
package Polymorphism;
import java.util.Scanner;
class Rectangle
{ private int breath;
private int 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);
}
}
}