Pages

Sunday, 25 September 2016

Create a class Calculater having two attributes no1 & no2 and methods addition and subtraction.Derive an object with default constructor by taking value 20 & 10. Derive another object with parameter constructor by supply value 50 & 30. Display the result of both the object.

public class cal
{
    public int no1;
    public int no2;
   
    public int Add()
    {
        return(no1+no2);
    }
    public int Sub()
    {
        return(no1-no2);
    }
    cal()
    {
        no1=20;
        no2=10;
    }
    cal(int m,int n)
    {
        no1=m;
        no2=n;
    }
}


public class Calculater
{
 public static void main(String[] args)
    {
        cal t1=new cal();
        System.out.println("The addition "+t1.no1+" and "+t1.no2+" is = "+t1.Add());
        cal t2=new cal();
        System.out.println("The subtraction "+t2.no1+" and "+t2.no2+" is = "+t2.Sub());
       
       
        cal t3=new cal(50,30);
        System.out.println("The addition "+t3.no1+" and "+t3.no2+" is = "+t3.Add());
        cal t4=new cal();
        System.out.println("The subtraction "+t4.no1+" and "+t4.no2+" is = "+t4.Sub());
     }
   
}

:-Scanner class Example

public class circle
{
   public int r;

   public double area()
     {
         return(3.14*r*r);
     }
}


import java.util.Scanner;
public class test
{
  public static void main(string[] args)
    {
      Scanner ip=new Scanner(System.in);
      circle c1=new circle();
      System.out.print("Enter radius...");
      c1.r=ip.nextint();
      System.out.println("the area= "+area());
    }
}
    

Default Constructor and Parameter Constructor Example

public class cube
{
 public int no;

 cube()
   {
     no=10;
   }

 cube(int n)
   {
     no=n;
   }

 public int ans()
   {
     return(no*no*no);
   }
}

public class test
 {
    public static void main(string[] args)
  {

   cube t1=new cube();
   System.out.println("the cube value of a no="+t1.no+" is "+t1.ans());

   cube t2=new cube(3);
   System.out.println("the cube value of a no="+t2.no+" is "+t2.ans());
  }
}

:-create a class circle having radius as attributes and area as method creates object with r=10 & display it's area.

public class Circule {

    public int r;
    public double area()
    {
        return(3.14*r*r);
    }   
    public static void main(String[] args) {
       Circule p=new Circule();
       p.r=1;
       System.out.println(p.area());
    }
   
}

Program Addition and Subtraction


public class Addsub {
    public int a;
    public int b;
  public int add()
  {
      return(a+b);
  }
  public int sub()
  {
      return(a-b);
  }public static void main(String[] args) {
       Addsub p=new Addsub();
       p.a=10;
       p.b=5;
    System.out.println(p.add());
    System.out.println(p.sub());
    }
   
}