Breaking News
Loading...
Monday, 4 April 2016

Calculator with switch case

02:12:00
calculator.java 

public class Calculator {
    int x , y ;
   public Calculator ( )
   {
       x = 0 ;
       y = 0 ;
   }
  
   int sum ( int a , int b )
   {
      
       int c = a+b;
       return c;
      
      
   }
   int mul ( int a , int b )
   {
      
       int c = a*b;
       return c;
      
      
   }
  
   int sub ( int a , int b )
   {
      
       int c = a-b;
       return c;
      
   }
         
        
        int div ( int a , int b )
   {
      
       int c = a/b;
       return c;
      
   }   
         
         
          int Remainder(int a,int b)
   {
       int c=a%b;
       return c;
  
   }
          
            float sub ( float a , float b )
   {
      
       float c = a-b;
       return c;
      
   }
             
     float div ( float a , float b )
   {
      
       float c = a/b;
       return c;
      
   }
     //OVERLOADING DIFF NUMBER OF INPUT ARGS
       /*    int mul ( int a , int b )
   {
      
       int c = a*b;
       return c;
      
   }
   float mul ( float a , float b ,float d)
     
   {
      
       float c = a*b*d;
       return c;
      
   } */
   ///////////////////////////////////////
    int factorial (int u)
   {
       int fact =1 ;
       for (int i = u ;i>0;i--)
       {
           fact =fact*i;
          
       }
       return fact;
   }
    double Sine (double v)
    {
        v= Math.toRadians(v);
        double c=Math.sin(v);
        return c;
       
    }
    double cos (double v)
    {
        v= Math.toRadians(v);
        double c=Math.cos(v);
        return c;
       
    }
   
   
    double tan (double v)
    {
        double c= Math.sin (v);
        return c;
       
    }
    double ln (double v)
    {
        double c= Math.log (v);
        return c;
       
    }
     double lOG (double v)
    {
        double c= Math.log (v);
        return c;
       
    }
   
    double  sqrt (int w)
     {
         double e = Math. sqrt (w);
         return e;
   
   }
    boolean prime (int o)
    {
        for (int i = 2 ; i <= o/2 ; i++)
        {
            if (o%1==0)
            {
                return false ;
            }
            break;
        }
        return true;
    }  }
     
TestApp.java 


   
       
import java.util.Scanner;
public class TestApp {
    public TestApp( )
    {
       
    }
  
    public static void main ( String args [ ] )
    {
       
        //it will use for to input a value thi is java function scanner
        Scanner s=new Scanner(System.in);
         //////////////////////////////
      Calculator obj = new Calculator();
      //first we input to values
      //And we created a menu of add sub etc
      //aftr then creat switc to chose wich case apply
     int choice;
      do{
       System.out.println("enter first value=");
        int a=s.nextInt();
       
        System.out.println("enter second value=");
        int b=s.nextInt();
       
        System.out.println("/t 1:add");
        System.out.println("/t 2:mul");
        System.out.println("/t 3:sub");
        System.out.println("/t 5:exit");
        System.out.println("please select from 1-4");
       
        choice = s.nextInt();
         
        int r=0;
        switch (choice)
         {
            case 1:
                  r=obj.sum(a,b);
                 break;
            case 2:
                  r=obj.mul(a,b);
                 break;
            case 3:
                  r=obj.sub(a,b);
                 break;
            case 4:
                  r = obj.div(a,b);
                 break;
               case 5:
                System.out.println("invalid number");
                break;
           default:
        System.out.println("please enter 1-4");
       
        }
    
      
      System.out.println("answer"+r);
      }
     while(choice<=5);
        }      }

   



   

 


         

0 comments:

Post a Comment

 
Toggle Footer