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());
     }
   
}

1 comment: