JAVA programs

Java program for classes and object

In this program we demonstrate use of java classes, methoed, objects

Program

//Program Using classes, methods and objects

class square

{

int square(int side)

    {

        int area=side*side;

        return (area);

    }

    int sqpari(int side)

    {

        int pari=4*side;

        return(pari);

    }

}

class rectangle

{

int rectarea(int length,int breadth)

    {

        int area=length*breadth;

        return (area);

    }

    int rectpari(int length,int breadth)

    {

        int pari =2*(length+breadth);

        return(pari);

}

}

public class ObjectClass

{

    public static void main(String [] args)

    {

        int sarea1,sarea2,pari1,pari2;

        int rectarea1,rectarea2,rectpari1,rectpari2;

        square sq=new square();

        rectangle rect=new rectangle();

            int a=20;

            System.out.println("\n Side of 1st square="+a);

             sarea1=sq.square(a);

             pari1=sq.sqpari(a);

            System.out.println("\n Area  of 1st square="+sarea1);

            System.out.println("\n parimeter of 1st square="+pari1);

            a=30;

                System.out.println("\n Side of 2nd square="+a);

                sarea2=sq.square(a);

                pari2=sq.sqpari(a);

                System.out.println("\n Area  of 2st square="+sarea2);

            System.out.println("\n parimeter of 2nd square="+pari2);

            int x=10,y=20;

            System.out.println("\n length  of 1st Rectangl=e="+x);

    System.out.println("\n breadth  of 1st Rectangl=e="+y);

    rectarea1=rect.rectarea(x,y);

ectpari1=rect.rectpari(x,y);

    System.out.println("\n area  of 1st Rectangl=e="+rectarea1);

    System.out.println("\n parimeter  of 1st Rectangl=e="+rectpari1)

    x=15; y=25;

    System.out.println("\n length  of 2nd Rectangl=e="+x);

                    System.out.println("\n breadth  of 2nd Rectangl=e="+y);

                    rectarea2=rect.rectarea(x,y);

                    rectpari2=rect.rectpari(x,y);

                    System.out.println("\n Area of 2nd Rectangl=e="+rectarea2);

                    System.out.println("\n Parimeter  of 2nd Rectangl=e="+rectpari2);

        

    }

}

When Run The Program Following Output:

See below output window

Description

Releted Programs

© 2017 codewmAll rights reserved.