Java Interfaces and Packages & java.util Package

 

JAVA

Interfaces and Packages & java.util Package

 


Fill in the blanks

 

1.Write a keyword for interface implements

2.Write a keyword for class to class extends

3.Write a keyword for class to interface implements

4.Collection of class and object is called package

5.We can use one file to another program by using  import .[T/F] True.

6.Vector implements a dynamic array.

7.One of the ways to store information for fast lookup is a hase table.

8.To make use of classes within different packages , the import statement is used.

9.Obtained the element at a specific location elementAt() method is used.

10.To remove an element  removeElement()  method is used.

Answer the Following Questions

                                                                (4 marks)

1.Write a program to calculate area of rectangle & area of circle using Interface

File name: Area.java

interface Area

{

        final static double pi=3.14;

        double compute(double x,double y);

}

File name: Rect1.java

class Rect1 implements Area

{

         public double compute(double x, double y)

       {

                  return(x*y);

       }

}

File name: Circle.java

class Circle implements Area

{

         public double compute(double x, double y)

       {

                  return(pi*x*x);

        }

}

File name: InterfaceTest.java

class InterfaceTest

{

         public static void main(String args[])

         {

         Rect1 r=new Rect1();

         Circle c=new Circle();

         Area area;

         area=r;

         System.out.println("Area of the Rectangle = "+area.compute(10,20));

         area=c;

           System.out.println("Area of the Rectangle = "+area.compute(10,0));

            }

}

2.Write a program for Vector

File name: VectorDemo.java

import java.util.*;

 public class VectorDemo {

    public static void main(String []args) {

         Vector<String> vectStr = new Vector<String>(); //Declaring a Vector in Java

         vectStr.add("apple");

        vectStr.add("orange");

        vectStr.add("graphs");

        //Displaying vector elements

        System.out.println("The Vector elements are:");

        System.out.println("");

        for (int i=0;i<vectStr.size();i++){

                 System.out.println(vectStr.get(i));

              }

 }   

}


 

Answer the Following Questions

                                                                (10 marks)


1.Write a program a package using Math function

 File Name : MathFun.java

package Math;

public class MathFun

{

public int fact(int num)

{

        if(num==1)

        return 1;

else

          return(num*fact(--num));

}

public int square(int num)

{

        return(num*num);

}

public int cube(int num)

{

      return(num*num*num);

}

public int sum(int a,int b)

{

         return(a+b);

}

public float sum(float a, float b)

{

         return(a+b);

}

 

public float multiply(float a, float b)

{

           return(a*b);

   }

}

//File Name: PackTest.java

import Math.MathFun;

class PackTest

{

  public static void main(String args[])

 {

 MathFun mf=new MathFun();

System.out.println("Factorial:"+mf.fact(5));

System.out.println("Square:"+mf.square(5));

System.out.println("Cube:"+mf.cube(5));

System.out.println("Sum of Two Interger:"+mf.sum(5,5));

System.out.println("Sum of Two float Number:"+mf.sum(5.5f,5.3f));

    }

}

 

No comments:

Post a Comment