Showing posts with label Calculator. Show all posts
Showing posts with label Calculator. Show all posts

Create a class Calculator that has the following features: addition, subtraction, multiplication, division. Create another class named ScientificCalculator that will have the features of Calculator and some other features like: to find square, cube, power, factorial.... Note:Inheritance to complete this task.

class Calculator
  {
   public void calc()
    {
    int add, sub, mul, a, b;
    double div;
    a=Integer.parseInt(System.console().readLine("enter the first no.  :"));
    b=Integer.parseInt(System.console().readLine("enter the second no. :"));
    add=a+b;
    System.out.println("addition of two no.      ="+add);
    sub=a-b;
    System.out.println("substraction of two no.  ="+sub);
    mul=a*b;
    System.out.println("multiplecation of two no.="+mul);
    div=(double)a/b;
    System.out.println("division of two no.      ="+div);
   }
  }
class ScientificCalculator extends Calculator
{
   void scincalc()
   {
    System.out.println("****************values of ScientificCalculator******************");
   int n=Integer.parseInt(System.console().readLine("enter the  no.  :"));
   int sqr=n*n;
   System.out.println("Square  of the no.      ="+sqr);
   int cube=n*n*n;
   System.out.println("cube of the no.="+cube);
   int fact=1;
   for(int j=1;j<=n;j++)
      {
        fact=fact*j;
      }
    System.out.println("factorial of the no.="+fact);
   int d=Integer.parseInt(System.console().readLine("enter the power no.  :"));
   int pow=n;
   for(int i=1;i<d;i++)
     {
     
       pow=pow*n;
     }
  
    System.out.println("power of n to d is ="+pow);
   
  
  }
   public static void main(String args[])
    {
      ScientificCalculator ab=new ScientificCalculator();
      ab.calc();
      ab.scincalc();
      
    }
 }


 

Create a class SimpleCalculator that has functionality of addition, substraction division, multiplication, square and squareroot.

class Calculator
  {
   public void calc()
    {
    int add, sub, mul, a, b, n,sqr,m;
    double div;
    a=Integer.parseInt(System.console().readLine("enter the first no.  :"));
    b=Integer.parseInt(System.console().readLine("enter the second no. :"));
    add=a+b;
    System.out.println("addition of two no.      ="+add);
    sub=a-b;
    System.out.println("substraction of two no.  ="+sub);
    mul=a*b;
    System.out.println("multiplecation of two no.="+mul);
    div=(double)a/b;
    System.out.println("division of two no.      ="+div);
    n=Integer.parseInt(System.console().readLine("enter the no. jiska square niklna hai  :"));
    sqr=n*n;
    System.out.println("square of the no="+sqr);
    m=Integer.parseInt(System.console().readLine("enter the no. jiska square niklna hai  :"));
    double sqtroot=Math.sqrt(m);
    System.out.println("squareroot of the no="+sqtroot);
   }
 
public static void main(String args[])
    {
      Calculator s=new Calculator();
      s.calc();
    
      
    }
 }


 

Create a java program that has three version of add method which can add two, three, and four integers

class VersionAddition
    {
      void firstVersion()
        {
          System.out.println("Enter the two no.s->");
          int a=Integer.parseInt(System.console().readLine("enter the valye fo first no. ="));
          int b=Integer.parseInt(System.console().readLine("enter the valye fo second no.="));
          int sum=a+b;
          System.out.println("sum of two integer ="+sum);
        }
       void secondVersion()
        {
          System.out.println(" ");
          System.out.println("Enter the three no.s->");
          int c=Integer.parseInt(System.console().readLine("enter the valye fo first no. ="));
          int d=Integer.parseInt(System.console().readLine("enter the valye fo second no.="));
          int e=Integer.parseInt(System.console().readLine("enter the valye fo third  no.="));
          int sum=c+d+e;
          System.out.println("sum of three integer ="+sum);
        }
       void thirdVersion()
        {
          System.out.println(" ");
          System.out.println("Enter the four no.s->");
          int f=Integer.parseInt(System.console().readLine("enter the valye fo first no. ="));
          int g=Integer.parseInt(System.console().readLine("enter the valye fo second no.="));
          int h=Integer.parseInt(System.console().readLine("enter the valye fo third  no.="));
          int i=Integer.parseInt(System.console().readLine("enter the valye fo forth  no.="));
          int sum=f+g+h+i;
          System.out.println("sum of four integer ="+sum);
        }
   public static void main(String args[])
      {
        VersionAddition v=new VersionAddition();
        v.firstVersion();
        v.secondVersion();
        v.thirdVersion();
      }
  }

create a package packCal in which main class Calculater and you create 4 different class,add,Multi, sub,Div in packAdd,packMulti,packSub,packDiv.These classes can be accessed through Calculator

This program is completed in 5 steps. These steps are :-


STEP 1 :- 

      ------Make Add package and run it------
package packadd;
public class add
{
public void add1(int a,int b)
{
int c=a+b;
System.out.println("the sum is="+c);
}
}

STEP 2 :- 

      ------Make Subtraction package and run it------
package packsub;
public class sub
{
public void sub1(int a,int b)
{
int c=a-b;
System.out.println("the subtraction is="+c);
}
}

STEP 3 :- 

      ------Make Multiplication package and run it------
package packmul;
public class mul
{
public void mul1(int a,int b)
{
int c=a*b;
System.out.println("the multiplication is="+c);
}
}

STEP 4 :- 

      ------Make Division package and run it------
package packdiv;
public class div
{
public void div1(int a,int b)
{
int c=a/b;
System.out.println("the division is="+c);
}
}

STEP 5 :- 

      ------Finally make a  package Calculator and import all  packages and store all packages in same directory and run it------
package packcal;
import packadd.add;
import packsub.sub;
import packmul.mul;
import packdiv.div;
public class calculator
{
public static void main(String...ghj)
{
add a= new add();
a.add1(5,4);
sub b= new sub();
b.sub1(5,4);
mul m=new mul();
m.mul1(4,5);
div d= new div();
d.div1(5,4);
}
}