Question

Project: Project: Calculator I need java code for following Problem with screenshot outputs: Develop a Calculator...

Project: Project: Calculator

I need java code for following Problem with screenshot outputs:

Develop a Calculator that takes inputs from the user for the following operations (+, -, *, /, =,and any trignometric function such as log,sine , cos,tan etc ) as well as numbers to be computed.

Sample 1:

Enter the problem statement:

2 + 4 * 5 = 22

Click AC to restart or Exit to stop.

You must have one method for each one of those operations.

For more sample your can use the online calculator.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE IT A THUMBS UP

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

class ScientificCalculator extends JFrame implements ActionListener {
static JTextField tfield, t1;
static double temp, temp1, result, a;
static double m1, m2;
int k = 1, x = 0, y = 0, z = 0;
char ch;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, plus, min, div, mul, eq, dot, sin, cos, tan;
Container cont;
JPanel textPanel, buttonpanel;

static void opt(String s){
if(s.equals("add")){
double result = temp + temp1;
tfield.setText("");
tfield.setText(tfield.getText() + result);

}
if(s.equals("sub")){
double result = temp - temp1;
tfield.setText("");
tfield.setText(tfield.getText() + result);
}
if(s.equals("mul")){
double result = temp * temp1;
tfield.setText("");
tfield.setText(tfield.getText() + result);
}
if(s.equals("div")){
double result = temp / temp1;
tfield.setText("");
tfield.setText(tfield.getText() + result);
}
if(s.equals("sin")){
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.sin(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if(s.equals("cos")){
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.cos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if(s.equals("tan")){
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
t1.setText(tfield.getText());
}

ScientificCalculator() {
cont = getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel = new JPanel();
tfield = new JTextField(25);
t1 = new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
t1.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent keyevent) {
char c = keyevent.getKeyChar();
if (c >= '0' && c <= '9') {
} else {
keyevent.consume();
}
}
});   
textpanel.add(t1);
textpanel.add(tfield);
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));
boolean t = true;
tfield.setVisible(false);
b1 = new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2 = new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3 = new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);

b4 = new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5 = new JButton("5");
buttonpanel.add(b5);
b5.addActionListener(this);
b6 = new JButton("6");
buttonpanel.add(b6);
b6.addActionListener(this);

b7 = new JButton("7");
buttonpanel.add(b7);
b7.addActionListener(this);
b8 = new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9 = new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);

zero = new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);

plus = new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);

min = new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);

mul = new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);

div = new JButton("/");
div.addActionListener(this);
buttonpanel.add(div);

dot = new JButton(".");
buttonpanel.add(dot);
dot.addActionListener(this);

eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);

sin = new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
cos = new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
tan = new JButton("TAN");
buttonpanel.add(tan);
tan.addActionListener(this);

clr = new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);
cont.add("Center", buttonpanel);
cont.add("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();   
if (s.equals("1")) {
if (z == 0) {
tfield.setText(tfield.getText() + "1");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "1");   
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"1");
else
    t1.setText(tfield.getText());   
}
if (s.equals("2")) {
if (z == 0) {
tfield.setText(tfield.getText() + "2");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "2");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"2");
else
    t1.setText(tfield.getText());
}
if (s.equals("3")) {
if (z == 0) {
tfield.setText(tfield.getText() + "3");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "3");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"3");
else
    t1.setText(tfield.getText());
}
if (s.equals("4")) {
if (z == 0) {
tfield.setText(tfield.getText() + "4");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "4");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"4");
else
    t1.setText(tfield.getText());
}
if (s.equals("5")) {
if (z == 0) {
tfield.setText(tfield.getText() + "5");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "5");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"5");
else
    t1.setText(tfield.getText());
}
if (s.equals("6")) {
if (z == 0) {
tfield.setText(tfield.getText() + "6");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "6");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"6");
else
    t1.setText(tfield.getText());
}
if (s.equals("7")) {
if (z == 0) {
tfield.setText(tfield.getText() + "7");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "7");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"7");
else
    t1.setText(tfield.getText());
}
if (s.equals("8")) {
if (z == 0) {
tfield.setText(tfield.getText() + "8");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "8");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"8");
else
    t1.setText(tfield.getText());
}
if (s.equals("9")) {
if (z == 0) {
tfield.setText(tfield.getText() + "9");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "9");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"9");
else
    t1.setText(tfield.getText());
}
if (s.equals("0")) {
if (z == 0) {
tfield.setText(tfield.getText() + "0");
} else {
tfield.setText("");
tfield.setText(tfield.getText() + "0");
z = 0;
}
if(t1.getText().contains("+") || t1.getText().contains("-") || t1.getText().contains("/") || t1.getText().contains("*"))
    t1.setText(t1.getText()+"0");
else
    t1.setText(tfield.getText());
}
if (s.equals("AC")) {
tfield.setText("");
t1.setText(tfield.getText());
x = 0;
y = 0;
z = 0;
}
if (s.equals(".")) {
if (y == 0) {
tfield.setText(tfield.getText() + ".");
y = 1;
} else {
tfield.setText(tfield.getText());
}
}
if (s.equals("+")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 0;
ch = '+';
} else {
temp = Double.parseDouble(tfield.getText());
t1.setText(tfield.getText()+"+");
tfield.setText("");
ch = '+';
y = 0;
x = 0;
}
tfield.requestFocus();
}
if (s.equals("-")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 0;
ch = '-';
} else {
    t1.setText(tfield.getText()+"-");
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
tfield.setText("");
ch = '-';
}
tfield.requestFocus();
}
if (s.equals("/")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 1;
ch = '/';
} else {
    t1.setText(tfield.getText()+"/");
x = 0;
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '/';
tfield.setText("");
}
tfield.requestFocus();
}
if (s.equals("*")) {
if (tfield.getText().equals("")) {
tfield.setText("");
temp = 1;
ch = '*';
} else {
x = 0;
t1.setText(tfield.getText()+"*");
y = 0;
temp = Double.parseDouble(tfield.getText());
ch = '*';
tfield.setText("");
}
tfield.requestFocus();
}
if (s.equals("SIN")) {
opt("sin");
}
if (s.equals("COS")) {
opt("cos");
}
if (s.equals("TAN")) {
opt("tan");
}
if (s.equals("=")) {
if (tfield.getText().equals("")) {
tfield.setText("");
} else {
temp1 = Double.parseDouble(tfield.getText());
switch (ch) {
case '+':
opt("add");
break;
case '-':
opt("sub");
break;
case '/':
opt("div");
break;
case '*':
opt("mul");
break;
}
z = 1;
}
}
tfield.requestFocus();
}
public static void main(String args[]) {
try {
UIManager
.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
}
ScientificCalculator f = new ScientificCalculator();
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
}
}

Add a comment
Know the answer?
Add Answer to:
Project: Project: Calculator I need java code for following Problem with screenshot outputs: Develop a Calculator...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Project Please have the code written in JAVA This is a game I am trying...

    JAVA Project Please have the code written in JAVA This is a game I am trying to develop. Please let me know if its possible and what the base code is. Prompt User “Hello! Welcome to the game.” Prompt User “To begin the game, please enter a letter” Random word generator, will set the String to a new word After the user inputs a letter, the computer will enter a loop Once in the loop the computer will test each...

  • I need a simple program in C language that calculates the values of Sine, Cosine, and...

    I need a simple program in C language that calculates the values of Sine, Cosine, and Tangent of values given in degrees. It has to be a range of values between 0 and 360(integer values). The output should be a table of values showing Sin, Cos and Tan values. The complete computation and printing of the table must be done inside a function. The function also returns to the main program with 3 output arguments: It also needs to show...

  • I need help with this Java problem, please. Code must be written in Java Create an...

    I need help with this Java problem, please. Code must be written in Java Create an application that calculates batting statistics for baseball players. Console Welcome to the Batting Average Calculator Enter number of times at bat: 5 0 = out, 1 = single, 2 = double, 3 = triple, 4 = home run Result for at-bat 1: 0 Result for at-bat 2: 1 Result for at-bat 3: 0 Result for at-bat 4: 2 Result for at-bat 5: 3 Batting...

  • Need a java program which solves the following problem (COMPUTER PROJECT) Twenty computers are connected in...

    Need a java program which solves the following problem (COMPUTER PROJECT) Twenty computers are connected in a network. One computer be- comes infected with a virus. Every day, this virus spreads from any infected computer to any uninfected computer with probability 0.1. Also, every day, a computer technician takes 5 infected computers at random (or all infected computers, if their number is less than 5) and removes the virus from them. Estimate: (a) the expected time it takes to remove...

  • JAVA Lab -Lottery Calculator IMPORTANT: This lab is best implemented using a loop structure. If you...

    JAVA Lab -Lottery Calculator IMPORTANT: This lab is best implemented using a loop structure. If you implement this without a loop, you would need to run your program once for each test item. Create a java program that will test for winning numbers on a lottery game. Each lottery ticket will have 5 integer numbers. Valid numbers must be integer numbers between 1 and 55 Assume that the winning Lottery ticket is 1 9 15 33 40 Your program is...

  • python code DC Final Project Implement a class Car with the following properties. A car has...

    python code DC Final Project Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. The following two lines are written in a File called FuelEffic.txt (you have to read these from the txt file) Miles per gallon: 20 Tank Size (in gallons): 25 Therefore, based on these...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • please explain this Java problem with source code if possible Project: Strictly identical arrays Problem Deseription:...

    please explain this Java problem with source code if possible Project: Strictly identical arrays Problem Deseription: Two arrays are strictly identical if their correspondimg elements are equal Write a program with class name StrictlyIdentical that prompts the user to enter two lists ed integers of size 5 and displays whether the two are strictly identical. Usc following method header to pass two arrays and retum a Boolcan public static boolean cualsint[] array 1, int[] array2) Method will return true if...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT