You are on page 1of 17

LAB 5-6

Name-Shashwat Shankar Singh


Sec-‘F’
Reg No-209301500
1. Packages
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.

In package
package FIRST;

public class one


{ public static void show()
{ System.out.println("Inside first package");
}
public static void main(String []args)
{
}
}
package SECOND;
import FIRST.*;
public class two
{
public static void main(String [] args)
 {   System.out.println("Shashwat Shankar Singh");
     System.out.println("209301500");
      FIRST.one.show();  
}}
Exercise2: For the exercise 1 try and test with all the
access modifiersand 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
Scenario Public Private Protected Default
Same Package Accessibl Not Accessibl Accessible
Sub Class e Accessibl e
e
Same Package Accessibl Not Not Accessible
Non sub class e Accessibl Accessibl
e e
Different Package Accessibl Not Accessibl Not
Sub class e Accessibl e Accessible
e
Different Packages Accessibl Not Not Not
Non Sub class e Accessibl Accessibl Accessible
e e
Exercise3: In reference toexercise 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.FIRST_SENIOR;
public class Three
{
public static void show()
{
System.out.println("inside first_Seniorpackage");}
public static void main(String []args)
{
}
}
package SECOND;

public class two


{
public static void main(String [] args)
 {   System.out.println("Shashwat Shankar Singh");
     System.out.println("209301500");
      FIRST.one.show();  
      FIRST.FIRST_SENIOR.Three.show();
}}

Output-

2. Arrays
Exercise 1: Write a java program to illustrate how to declare,
instantiate, initialize, and traverse the Java array.
Exercise 2: Write a program to print the array elements
using for-each loop
Exercise 3: Write a programto demonstrate the way of
passing an array to method

Note*-All three exercises related to arrays have been done in a single program

Code-

import java.util.Scanner;

public class MAIN

public static int sum_of_array(int a[])

{ int sum=0;

for(int x:a)

{sum+=x;}

return sum;

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int arr[]=new int[10];//declaring and instantiating an array

for(int i=0;i<10;i++)

arr[i]=sc.nextInt();//Initialising the array

for(int i=0;i<10;i++)

{
System.out.print(arr[i]+" ");//Traversing the array

System.out.println();

for(int x:arr)

System.out.print(x+" ");//Using for each loop to print element of array

System.out.println();

int sum=sum_of_array(arr);

System.out.println("The sum is:"+sum);

Output-

3. PolymorphismandInheritance
Exercise 1: Write a java program for method overloading
Code-
import java.util.Scanner;

class student
{
        public void introduce(String name)
        {
            System.out.println("The name of the student is:"+name);
        }
        public void introduce(String name,int reg_no)
        {
            System.out.println("The name of the student is:"+name);
            System.out.println("Registration number is:"+reg_no);
        }

public class MAIN


{
  public static void main(String[] args) {
  System.out.println("Shashwat Shankar Singh");  
  System.out.println("209301500");
  student s1=new student();
  s1.introduce("Shashwat");
  s1.introduce("Shashwat",209301500);

}
}
Output-

Exercise 2:Write a java program for method overriding


Code-
import java.util.Scanner;

class employee
{
        public void introduce(String name,int Id)
        {
            System.out.println("Employee name:"+name);
            System.out.println("Employee Id:"+Id);
        }
    }

class programmer extends employee


{
     @Override
    public void introduce(String name,int Id)
        {
            System.out.println("The name of the Programmer is:"+name);
            System.out.println("The Id of Programmer:"+Id);
        }

}
public class MAIN
{
  public static void main(String[] args) {
  System.out.println("Shashwat Shankar Singh");  
  System.out.println("209301500");
  employee e1=new employee();
  e1.introduce("Shashwat",69);
  programmer p1=new programmer();
  p1.introduce("Shashwat",616);

}
}
Output-

Exercise 3:Write a program to illustrate compile-time


polymorphism in java

Code-
import java.util.Scanner;

class arithemtic
{ int add(int a,int b)
    {return a+b;}
double add(double a,double b)
{
    return a+b;
}

public class MAIN {


    public static void main(String[] args) {
        System.out.println("Shashwat Shankar Singh");
        System.out.println("209301500");
        arithemtic a=new arithemtic();
        System.out.println("Adding two integers:"+a.add(5, 6));
        System.out.println("Adding two decimals:"+a.add(5.6, 7.2));
    }
}

Output-

Explanation: We know In java Compile time polymorphism is


achieved through method overloading therefore we will use
an overloaded method to achieve this-

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 ofParent 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.
Code-
import java.util.Scanner;

class animal
{
    public void eat()
    {
        System.out.println("The animal is eating");
    }
}
class Herbivore extends animal
{

    public void eat()


    {
        System.out.println("The animal is eating grass and leaves");
    }
}
class Carnivore extends animal
{
    public void eat()
    {
        System.out.println("The animal is eating Red meat");
    }
}
class Omnivore extends animal
{
    public void eat()
    {

        System.out.println("The animal is eating leaves and Red meat");


    }
}

public class MAIN


{
    public static void main(String[] args) {
        System.out.println("Shashwat Shankar Singh");
        System.out.println("209301500");
        animal a1=new Herbivore();
        animal a2=new Carnivore();
        animal a3=new Omnivore();
        a1.eat();
        a2.eat();
        a3.eat();

    }
}

Output-

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
Code-
import java.util.Scanner;

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

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

public class MAIN


{
    public static void main(String[] args) {
        System.out.println("Shashwat Shankar Singh");
        System.out.println("209301500");
     parent p=new parent();
     child c=new child();
     p.display_parent();
     c.display_child();
     c.display_parent();
   

    }
}
Output-

Exercise 6: Create a class named 'Member' having the


following members:Data members
1 -Name2 -Age3 -Phone number4 -Address5 -SalaryIt 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 bothclasses and print the
same.

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

public class MAIN


{
    public static void main(String[] args) {
        System.out.println("Shashwat Shankar Singh");
        System.out.println("209301500");
      employee e1=new employee();
      manager m1=new manager();
      e1.Name="Shashwat";
      e1.age=20;
      e1.ph_no=98458492;
      e1.address="XY/8423/Jaipur";
      e1.salary=100000;
      e1.specialization="CSE";
      m1.Name="Mrigank";
      m1.age=20;
      m1.ph_no=85586950;
      m1.address="PFA_colony,jaipur";
     m1.salary=250000;
     m1.department="CSE";
     System.out.println("The details of the employee-");
     System.out.println("Name-"+e1.Name);
     System.out.println("Age-"+e1.age);
     System.out.println("Adress-"+e1.address);
     System.out.println("Phone number-"+e1.ph_no);
     System.out.println("Salary-"+e1.salary);
     System.out.println("Specialization-"+e1.specialization);
     e1.print_salary();
     System.out.println("The details of the Manager-");
     System.out.println("Name-"+m1.Name);
     System.out.println("Age-"+m1.age);
     System.out.println("Adress-"+m1.address);
     System.out.println("Phone number-"+m1.ph_no);
     System.out.println("Salary-"+m1.salary);
     System.out.println("Department-"+m1.department);
     m1.print_salary();

    }
}
Output-

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-
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 is:"+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 MAIN


{

    public static void main(String[] args)


     {
        System.out.println("Shashwat Shankar Singh");
        System.out.println("209301500");
        square sq=new square(5);//Declaring and Initiallizing a square with side
5
        rectangle R=new rectangle(3,7);  //Declaring and Initiallizing a
rectangle with length 3 and breath 7
       System.out.println("The area of rectangle:");
        R.area();
        System.out.println("The perimeter  of rectangle:");
       R.perimeter();
       System.out.println("The area of square:");
       sq.area();
       System.out.println("The perimeter of square:");
       sq.perimeter();

    }
}
Output-

Exercise 8. Now repeat the exercise 7 to print the area of 10


squares.

Code-

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 is:"+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 MAIN


{  

    public static void main(String[] args)


     {Scanner Sc=new Scanner(System.in);
        System.out.println("Shashwat Shankar Singh");
        System.out.println("209301500");
        square sq[]=new square[10];
        System.out.println("Enter the sides of square");
        for(int i=0;i<10;i++)
        {
          int x=Sc.nextInt();
          sq[i]=new square(x);
          sq[i].area();
        }

    }
}

Output-

You might also like