Showing posts with label java programs. Show all posts
Showing posts with label java programs. Show all posts

Saturday, 28 July 2012

Passing arguments and returning values.


Passing arguments and returning values.
class circle
{
        private int radius;
        public void setradius(int r)
        {
                radius = r;
        }
        public float calculate()
        {
                return 3.14f * radius * radius;
        }
        public static void main(String args[])
        {
                if (args.length < 1)
                {
                        System.out.println("Invalid Arguments");
                        System.exit(0);
                }
                circle c = new circle();
                c.setradius(Integer.parseInt(args[0]));
                float area = c.calculate();
                System.out.println("Radius : " + args[0]);
                System.out.println("Area : " + area);
        }
}

Passing object as arguments.


class rectangle
{
        private int len,bre;
        public void setrect(int l,int b)
        {
                len = l;
                bre = b;
        }
        public int totlen(rectangle t)
        {
                return len + t.len;
        }
        public int totbre(rectangle t)
        {
                return bre + t.bre;
        }
        rectangle combine(rectangle t)
        {
                rectangle temp = new rectangle();
                temp.len = len + t.len;
                temp.bre = bre + t.bre;
                return temp;
        }
        public void disp()
        {
                System.out.println("Length : " + len + "\t Breadth : " + bre);
        }
        public static void main(String args[])
        {
                rectangle r1 = new rectangle();
                rectangle r2 = new rectangle();
                rectangle r3 = new rectangle();
                r1.setrect(2,3);
                r2.setrect(4,5);
                int tlen = r1.totlen(r2);
                int tbre = r1.totbre(r2);
                r3 = r1.combine(r2);
                System.out.println("Total length : " + tlen);
                System.out.println("Total Breadth : " + tbre);
                r1.disp();
                r2.disp();
                r3.disp();
        }
}

Function overloading


class exam
{
        private int m1,m2,total;
        public void setmarks(int ma1,int ma2)
        {
                m1 = ma1;
                m2 = ma2;     
        }
        public void setmarks()
        {
                m1 = 80;
                m2 = 100;
        }
        void calculate()
        {
                total = m1 + m2;
        }
        void disp()
        {
                System.out.println("Mark1 : " + m1 + "\t Mark2 " + m2 + "\t Total " + total);
        }
        public static void main(String args[])
        {
                exam e1 = new exam();
                exam e2 = new exam();
                e1.setmarks();
                e2.setmarks(70,80);
                e1.calculate();
                e2.calculate();
                e1.disp();
                e2.disp();
        }
}