Tuesday, December 20, 2011

Menu driven BlueJ program on simple calculator


import java.io.*;
public class Pat
{
int a,b,c;
char ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void init()throws Exception
{

System.out.println("Enter first number:");
a=Integer.parseInt(br.readLine());
System.out.println("Enter second number:");
b=Integer.parseInt(br.readLine());
System.out.println("Enter operator (+,-,*,/):");
ch=(char)br.read();
switch(ch)
{
 case '+':
 add();
 break;
 case '-':
 sub();
 break;
 case '*':
 product();
 break;
 case '/':
 if(b==0)
 System.out.println("Division not possible");
 else
 div();
break;
default:
 System.out.println("Wrong Operator");
}
}
private void add()
{
c=a+b;
 System.out.println("Sum="+c);
}
private void sub()
{
c=a-b;
 System.out.println("Subtracted value="+c);
}
private void product()
{
c=a*b;
 System.out.println("Product="+c);
}
private void div()
{
c=a/b;
 System.out.println("Divided result="+c);
}
public static void main(String args[])throws Exception
{
Pat ob=new Pat();
 ob.init();
 }
}


Sample input and output of the menu driven calculator program

Enter first number:
22
Enter second number:
33
Enter operator (+,-,*,/):
+
Sum=55

7 comments:

  1. sir, please tell me the program code of the foll. pattern:

    12345
    22345
    33345
    44445
    55555

    ReplyDelete
  2. Already posted

    public class P
    {
    int i,j,n=1;
    public void show()
    {
    for(i=0;i<5;i++)
    {
    n=1;
    for(j=0;j<5;j++)
    {
    if(j<=i)
    System.out.print((i+1));
    else
    System.out.print(n);
    n++;
    }
    System.out.println();
    }
    }
    public static void main(String args[]) throws Exception
    {
    P ob=new P();
    ob.show();
    }
    }

    ReplyDelete
  3. sir please tell me that how i can study computer

    ReplyDelete
  4. Follow this blog and communicate with your problems through this blog site.

    ReplyDelete
  5. Sir can you show the same program using AddVal(), SubVal(), MulVal(), DivVal(), ModVal() to perform addition, subtraction, multiplication, division, and modulus respectively. Please its urgent.

    ReplyDelete
    Replies
    1. Simply change the function names:

      import java.io.*;
      public class Pat
      {
      int a,b,c;
      char ch;
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      public void init()throws Exception
      {

      System.out.println("Enter first number:");
      a=Integer.parseInt(br.readLine());
      System.out.println("Enter second number:");
      b=Integer.parseInt(br.readLine());
      System.out.println("Enter operator (+,-,*,/,%):");
      ch=(char)br.read();
      switch(ch)
      {
      case '+':
      AddVal();
      break;
      case '-':
      SubVal();
      break;
      case '*':
      MulVal();
      break;
      case '/':
      if(b==0)
      System.out.println("Division not possible");
      else
      DivVal();
      break;
      case '%':
      ModVal();
      break;
      default:
      System.out.println("Wrong Operator");
      }
      }
      private void AddVal()
      {
      c=a+b;
      System.out.println("Sum="+c);
      }
      private void SubVal()
      {
      c=a-b;
      System.out.println("Subtracted value="+c);
      }
      private void MulVal()
      {
      c=a*b;
      System.out.println("Product="+c);
      }
      private void DivVal()
      {
      c=a/b;
      System.out.println("Divided result="+c);
      }
      private void ModVal()
      {
      c=a%b;
      System.out.println("Remainder="+c);
      }
      public static void main(String args[])throws Exception
      {
      Pat ob=new Pat();
      ob.init();
      }
      }

      Delete
  6. sir can u plz tell buffered reader program to identify if the entered character is upper case , lowercase ,digit or special charcter

    ReplyDelete

Subscribe via email

Enter your email address:

Delivered by FeedBurner