Write a program in Java for creating normal and scientific calculators.

Program

import java.util.Scanner;
interface Calc
{
	public void sum();
	public void divide();
	public void sub();
	public void mul();
}
class NormalCalc implements Calc
{
	Scanner sc=new Scanner(System.in);
	int a,b,c;
	public void sum()
	{
		System.out.print("Enter 1st Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a+b;
		System.out.println("Sum of "+a+" and "+b+" is:="+c);
	}
	public void divide()
	{
		try{
		System.out.print("Enter 1st Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a/b;
		System.out.println("Division of "+a+" and "+b+" is:="+c);
		}
		catch(ArithmeticException ae)
		{
			System.out.println("Can't divide by zero");
		}
	}
	public void sub()
	{
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a-b;
		System.out.println("Subtraction of "+b+" from "+a+" is:="+c);
	}
	public void mul()
	{
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a*b;
		System.out.println("Multiplication of "+a+" and "+b+" is:="+c);
	}
}
class ScientificCalc implements Calc
{
	Scanner sc=new Scanner(System.in);
	int a,b,c;
	public void sum()
	{
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a+b;
		System.out.println("Sum of "+a+" and "+b+" is:="+c);
	}
	public void divide()
	{
		try{
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a/b;
		System.out.println("Division of "+a+" and "+b+" is:="+c);
		}
		catch(ArithmeticException ae)
		{
			System.out.println("Can't divide by zero");
		}
	}
	public void sub()
	{
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a-b;
		System.out.println("Subtraction of "+b+" from "+a+" is:="+c);
	}
	public void mul()
	{
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
		c=a*b;
		System.out.println("Multiplication of "+a+" and "+b+" is:="+c);
	}
	void sqrt()
	{
		System.out.print("Enter  Number:=");
		a=sc.nextInt();
		System.out.println("Answer is:="+ Math.sqrt(a));
	}
	void cos()
	{
		System.out.print("Enter  Number:=");
		a=sc.nextInt();
		System.out.println("Answer is:="+Math.cos(a));
	}
	void min(int x,int y)
	{
		a=x;
		b=y;
		System.out.print("Enter first Number:=");
		a=sc.nextInt();
		System.out.print("Enter 2nd Number:=");
		b=sc.nextInt();
        if(a <= b )
		{
			System.out.println(a+" is minimum value");
		}
		else
		{
			System.out.println(b+" is minimum value");
		}
	}
}
class Calculator
{
	public static void main(String[] args) 
	{
		int y=0;
		System.out.println("*******WELCOME TO JAVA*******");
		Scanner c=new Scanner(System.in);
		do{
			System.out.println();
		System.out.println("Input your choice");
		System.out.println("1.Normal calculator");
		System.out.println("2.scientific calculator");
		int ch=c.nextInt();
		while(ch<1 || ch>2)
		continue;
		System.out.println("Enter your choice:=");
		switch(ch)
		{
			case 1:
			{
				NormalCalc nc=new NormalCalc();
				System.out.println("1.Add");
				System.out.println("2.Sub");
				System.out.println("3.Div");
				System.out.println("4.Mul");
				int ch1=c.nextInt();
				switch(ch1)
				{
					case 1:
					{
						nc.sum();
					}
					break;
					case 2:
					{
						nc.sub();
					}
					break;
					case 3:
					{
						nc.divide();
					}
					break;
					case 4:
					{
						nc.mul();
					}
					break;
					default:
					{
						System.out.println("Wrong input");
					}
				}
			}
			 break;
			 case 2:
			{
				ScientificCalc sc=new ScientificCalc();
				System.out.println("1.Add");
				System.out.println("2.Sub");
				System.out.println("3.Div");
				System.out.println("4.Mul");
				System.out.println("5.Sqrt");
				System.out.println("6.cos");
				System.out.println("7.Min");
				int ch2=c.nextInt();
				switch (ch2)
				{
				case 1:
					{
						sc.sum();
					}
					break;
					case 2:
					{
						sc.sub();
					}
					break;
					case 3:
					{
						sc.divide();
					}
					break;
					case 4:
					{
						sc.mul();
					}
					break;
					case 5:
					{
						sc.sqrt();
					}
					break;
					case 6:
					{
						sc.cos();
					}
					break;
					case 7:
					{
						sc.min(sc.a,sc.b);
					}
					break;
					default:
					{
						System.out.println("wrong Input");
					}
				}
			}
			break;
			default:
			{
				System.out.println("wrong Input");
			}
		}
		System.out.print("Do you want to continue:(1/0)");
		y=c.nextInt();
		}while(y==1);
	}
}

Output:

Calculator Output
Calculator Output

 

(Visited 159 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

Your email address will not be published. Required fields are marked *