Please write in 2 class files Write a program to create a non - scientific calcu
ID: 3843807 • Letter: P
Question
Please write in 2 class files Write a program to create a non - scientific calculator (Division/Multiplication, Addition/Subtraction ONLY). The user enters numbers using digit buttons on the screen or keys on the keyboard. Also, watch the entering of multiple decimal points. A typical calculator accepts the first decimal point and ignores the rest. For example, 1.4.3 is read as 1.43. Try dividing by zero on a calculator and see what happens as well. "CE" clears only the last entry since an operator was pushed. "C" clears out the entire memory. Use BorderFactory and setBackGround to make the edges and color more interesting.Explanation / Answer
java program to create a non scientific calculator which performs tasks like addition,multiplication,subtraction,division by inputting data through keyboard and decimals can also be entered.the java code is:
import java.util.Scanner;
public class calculator
{
public static void main(String args[])
{
float a, b, res;
char choice, ch;
Scanner scan = new Scanner(System.in);
do
{
System.out.print("1. Addition ");
System.out.print("2. Subtraction ");
System.out.print("3. Multiplication ");
System.out.print("4. Division ");
System.out.print("5:cancel-C ");
System.out.print("6:clear-CE ");
System.out.print("7. Exit ");
System.out.print("Enter Your Choice : ");
choice = scan.next().charAt(0);
switch(choice)
{
case '1' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a + b;
System.out.print("Result = " + res);
break;
case '2' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a - b;
System.out.print("Result = " + res);
break;
case '3' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a * b;
System.out.print("Result = " + res);
break;
case '4' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a / b;
System.out.print("Result = " + res);
break;
case '5':System.out.print("0");
break;
case'6':res=0;
System.out.print(res);
break;
case '7' : System.exit(0);
break;
default : System.out.print("Wrong Choice!!!");
break;
}
System.out.print(" --------------------------------------- ");
}while(choice != 7);
}
}
the cancel C option can also be used to make zero after calculation.the clear CE option is used to clear screen after you've done a calculation.the out put for the program is:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5:cancel-C
6:clear-CE
7. Exit
Enter Your Choice : 1
Enter Two Number : 23
2
Result = 25.0
---------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5:cancel-C
6:clear-CE
7. Exit
Enter Your Choice : 2
Enter Two Number : 4
2
Result = 2.0
---------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5:cancel-C
6:clear-CE
7. Exit
Enter Your Choice : 5
0
---------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5:cancel-C
6:clear-CE
7. Exit
Enter Your Choice : 6
0.0
---------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5:cancel-C
6:clear-CE
7. Exit
Enter Your Choice : 7
it exits when we enter a choice of 7..