Sunday, March 22, 2015

Java Calculator using instance variable

Design and Write  a simple java class program which can perform all basic operation of a Calculator. Define necessary instance variable but do not use method.

Reference:
1. http://c-programming-sourcecode.blogspot.com/2012/06/write-program-performing-as-calculator.html?q=simple+interest

2. Course Book(The Complete Reference Java j2se) ; Page 106

Source Code:

package calculator_shohan;
import java.util.Scanner;
import java.io.*;
public class Calculator_shohan {
public static void main(String[] args) {
calculator_ c=new calculator_();
int num1=0,num2=0;
Scanner input=new Scanner(System.in);
System.out.println("Enter the first number:");
num1=input.nextInt();
System.out.println("Enter the second number:");
num2=input.nextInt();
c.cal(num1, num2);
}

}
class calculator_
{
int add=0,sub=0,div=0,mul=0,mod=0;
public void cal(int x,int y)
{
Scanner input=new Scanner(System.in);

char ch;
System.out.println("Enter your choice:");
System.out.println("Addition for + :");
System.out.println("Subtraction for - :");
System.out.println("Multiplication for * :");
System.out.println("Divition for /:");
System.out.println("Modulation for %:");
ch=input.next().charAt(0);
switch(ch)
{
case '+':
System.out.println(x+y);
break;

case '-':
System.out.println(x-y);
break;

case '*':
System.out.println(x*y);
break;
case '/':
System.out.println(x/y);
break;
case '%':
System.out.println(x%y);
break;
default:
System.out.println("no choice");
}
}
}

11 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. char type data input hisebe nite hole ki likhte hoy such as int type datar jonno nextInt(),float type er jonno nextFloat() etc.

    ReplyDelete
  3. ID-201421066
    Batch:46th

    package ex1;

    import java.util.Scanner;

    public class Ex1 {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int a,b,sum;

    System.out.println("plz enter a digit ");

    a = input.nextInt();
    b = input.nextInt();

    sum = a+b;
    System.out.println("addition result " +sum);

    sum = a-b;
    System.out.println("addition result " +sum);

    sum = a*b;
    System.out.println("addition result " +sum);

    sum = a/b;
    System.out.println("addition result " +sum);

    }

    }

    ReplyDelete
  4. package calculator_shohan;
    import java.util.Scanner;
    import java.io.*;
    public class Calculator_shohan {
    public static void main(String[] args) {
    calculator_ c=new calculator_();
    int num1=0,num2=0;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter the first number:");
    num1=input.nextInt();
    System.out.println("Enter the second number:");
    num2=input.nextInt();
    c.cal(num1, num2);
    }

    }
    class calculator_
    {
    int add=0,sub=0,div=0,mul=0,mod=0;
    public void cal(int x,int y)
    {
    Scanner input=new Scanner(System.in);

    char ch;
    System.out.println("Enter your choice:");
    System.out.println("Addition for + :");
    System.out.println("Subtraction for - :");
    System.out.println("Multiplication for * :");
    System.out.println("Divition for /:");
    System.out.println("Modulation for %:");
    ch=input.next().charAt(0);
    switch(ch)
    {
    case '+':
    System.out.println(x+y);
    break;

    case '-':
    System.out.println(x-y);
    break;

    case '*':
    System.out.println(x*y);
    break;
    case '/':
    System.out.println(x/y);
    break;
    case '%':
    System.out.println(x%y);
    break;
    default:
    System.out.println("no choice");
    }
    }
    }

    ReplyDelete
  5. package calculator;
    import java.util.Scanner;
    public class calculator {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float a,b,sum,sub,mul,div,mod;
    int choose;

    System.out.print("Enter two digit a & b : ");
    a = input.nextFloat();
    b = input.nextFloat();

    System.out.println("1 for Add, 2 for sub, 3 for mul, 4 for div, 5 for modulus");
    System.out.print("Enter your chooce : ");
    choose = input.nextInt();
    switch(choose)
    {
    case 1:
    sum = a+b;
    System.out.println("The sum of two digit of "+a+" & "+b+ " is : " +sum);
    break;
    case 2:
    sub = a-b;
    System.out.println("The sum of two digit of "+a+" & "+b+ " is : " +sub);
    break;
    case 3:
    mul = a*b;
    System.out.println("The sum of two digit of "+a+" & "+b+ " is : " +mul);
    break;
    case 4:
    div = a/b;
    System.out.println("The sum of two digit of "+a+" & "+b+ " is : " +div);
    break;
    case 5:
    mod = a%b;
    System.out.println("The sum of two digit of "+a+" & "+b+ " is : " +mod);
    break;
    default:
    System.out.println("Enter right option");
    }
    }
    }

    ReplyDelete
  6. package calculator;
    import java.util.Scanner;
    public class calculator {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float M,N,sum,sub,mul,div,mod;
    int choose;

    System.out.print("Enter two digit M & N : ");
    M = input.nextFloat();
    N = input.nextFloat();

    System.out.println("1 for Add, 2 for sub, 3 for mul, 4 for div, 5 for modulus");
    System.out.print("Enter your chooce : ");
    choose = input.nextInt();
    switch(choose)
    {
    case 1:
    sum = M+N;
    System.out.println("+M+" & "+N+ " is : " +sum);
    break;
    case 2:
    sub = M-N;
    System.out.println("+M+" & "+N+ " is : " +sub);
    break;
    case 3:
    mul = M*N;
    System.out.println("+M+" & "+N+ " is : " +mul);
    break;
    case 4:
    div = M/N;
    System.out.println("+M+" & "+N+ " is : " +div);
    break;
    case 5:
    mod = M%N;
    System.out.println("+M+" & "+N+ " is : " +mod);
    break;
    default:
    System.out.println("Enter right option");
    }
    }
    }

    ReplyDelete
  7. package calculator_shohan;
    import java.util.Scanner;
    import java.io.*;
    public class Calculator_shohan {
    public static void main(String[] args) {
    calculator_ c=new calculator_();
    int num1=0,num2=0;
    Scanner input=new Scanner(System.in);
    System.out.println("Enter the first number:");
    num1=input.nextInt();
    System.out.println("Enter the second number:");
    num2=input.nextInt();
    c.cal(num1, num2);
    }

    }
    class calculator_
    {
    int add=0,sub=0,div=0,mul=0,mod=0;
    public void cal(int x,int y)
    {
    Scanner input=new Scanner(System.in);

    char ch;
    System.out.println("Enter your choice:");
    System.out.println("Addition for + :");
    System.out.println("Subtraction for - :");
    System.out.println("Multiplication for * :");
    System.out.println("Divition for /:");
    System.out.println("Modulation for %:");
    ch=input.next().charAt(0);
    switch(ch)
    {
    case '+':
    System.out.println(x+y);
    break;

    case '-':
    System.out.println(x-y);
    break;

    case '*':
    System.out.println(x*y);
    break;
    case '/':
    System.out.println(x/y);
    break;
    case '%':
    System.out.println(x%y);
    break;
    default:
    System.out.println("no choice");
    }
    }
    }

    ReplyDelete
  8. package calculator;
    import java.util.Scanner;
    public class calculator {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float M,N,sum,sub,mul,div,mod;
    int choose;

    System.out.print("Enter two digit M & N : ");
    M = input.nextFloat();
    N = input.nextFloat();

    System.out.println("1 for Add, 2 for sub, 3 for mul, 4 for div, 5 for modulus");
    System.out.print("Enter your chooce : ");
    choose = input.nextInt();
    switch(choose)
    {
    case 1:
    sum = M+N;
    System.out.println("+M+" & "+N+ " is : " +sum);
    break;
    case 2:
    sub = M-N;
    System.out.println("+M+" & "+N+ " is : " +sub);
    break;
    case 3:
    mul = M*N;
    System.out.println("+M+" & "+N+ " is : " +mul);
    break;
    case 4:
    div = M/N;
    System.out.println("+M+" & "+N+ " is : " +div);
    break;
    case 5:
    mod = M%N;
    System.out.println("+M+" & "+N+ " is : " +mod);
    break;
    default:
    System.out.println("Enter right option");
    }
    }
    }

    ReplyDelete
  9. Name -Main uddin
    ID-201421090
    Batch-46th


    package exercise.pkg01;
    import java.util.Scanner;
    public class Exercise01 {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float operand1,operand2,summation,subtract,multiplication,divide,modulus;
    int Operator;
    System.out.print("Enter two Operand operand1 & operand2 : ");
    operand1 = input.nextFloat();
    operand2 = input.nextFloat();

    System.out.println("1 of +,2 of -,3 of *,4 of /,5 of %");
    System.out.print("Enter your Operator value : ");
    Operator = input.nextInt();
    switch(Operator)
    {
    case 1:
    summation = operand1+operand2;
    System.out.println("The summation of two Operator of "+operand1+" & "+operand2+ " is : " +summation);
    break;
    case 2:
    subtract = operand1-operand2;
    System.out.println("The subtract of two Operator of "+operand1+" & "+operand2+ " is : " +subtract);
    break;
    case 3:
    multiplication = operand1*operand2;
    System.out.println("The multiplication of two Operator of "+operand1+" & "+operand2+ " is : " +multiplication);
    break;
    case 4:
    divide = operand1/operand2;
    System.out.println("The divide of two Operator of "+operand1+" & "+operand2+ " is : " +divide);
    break;
    case 5:
    modulus = operand1%operand2;
    System.out.println("The modulus of two Operator of "+operand1+" & "+operand2+ " is : " +modulus);
    break;
    default:
    System.out.println(" Please enter right operator");
    }
    // TODO code application logic here
    }

    }

    ReplyDelete
  10. Md : Jalis Mahmud
    ID: 201420762
    batch : 46th


    package ex1;

    import java.util.Scanner;

    public class Ex1 {

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int m,n,sum;

    System.out.println("plz enter m digit ");

    m = input.nextInt();
    n = input.nextInt();

    sum = m+n;
    System.out.println("addition result " +sum);

    sum = m-n;
    System.out.println("addition result " +sum);

    sum = m*n;
    System.out.println("addition result " +sum);

    sum = m/n;
    System.out.println("addition result " +sum);

    }

    }

    ReplyDelete
  11. Name : Habibur Rahaman
    ID-201421091
    Batch-46

    package exercise.pkg01;
    import java.util.Scanner;
    public class Exercise01 {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float Value1,Value2,Sum,Sub,Mul,Div,Mod;
    int Opt;
    System.out.print("Enter two Operand Value1 & Value2 : ");
    Value1 = input.nextFloat();
    Value2 = input.nextFloat();

    System.out.println("1 of +,2 of -,3 of *,4 of /,5 of %");
    System.out.print("Enter your Opt value : ");
    Opt = input.nextInt();
    switch(Opt)
    {
    case 1:
    Sum = Value1+Value2;
    System.out.println("The Sum of two Opt of "+Value1+" & "+Value2+ " is : " +Sum);
    break;
    case 2:
    Sub = Value1-Value2;
    System.out.println("The Sub of two Opt of "+Value1+" & "+Value2+ " is : " +Sub);
    break;
    case 3:
    Mul = Value1*Value2;
    System.out.println("The Mul of two Opt of "+Value1+" & "+Value2+ " is : " +Mul);
    break;
    case 4:
    Div = Value1/Value2;
    System.out.println("The Div of two Opt of "+Value1+" & "+Value2+ " is : " +Div);
    break;
    case 5:
    Mod = Value1%Value2;
    System.out.println("The Mod of two Opt of "+Value1+" & "+Value2+ " is : " +Mod);
    break;
    default:
    System.out.println(" Please enter right operator");
    }
    // TODO code application logic here
    }

    }

    ReplyDelete