Question #53264

How to write a program for calculator in java without using swing?

Expert's answer

Answer on Question #53264 - Programming, Java | JSP | JSF

How to write a program for calculator in java without using swing?

Solution.

//In the console using Scanner:


import java.util.Scanner; //imports scanner
public class Calc {
    public static void main(String[] args) {
        boolean go = true;     //sets up loop
        while(go) //creates loop to top
        {
            System.out.println("Hello this is my calculator!");
            System.out.println("To add, type a, to subtract, type s.");
            System.out.println("To multiply, type m, to divide, type d.");
            Scanner scan = new Scanner(System.in); //sets up scanner
            Scanner scan1 = new Scanner(System.in);
            String action = scan.nextLine(); //tells comp. to take user input
            if("a".equals(action)) //addition
            {
                System.out.println("Now type in the first number you would like to add.");
                int add1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int add2 = scan.nextInt();
                int add3 = add1 + add2;
                System.out.println(add1 + " added to " + add2 + " equals " + add3 + "!");
            }
            if("s".equals(action)) //subtraction
            {
                System.out.println("Now type in the first number you would like to subtract.");
                int sub1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int sub2 = scan.nextInt();
                int sub3 = sub1 - sub2;
                System.out.println(sub1 + " subtracted by " + sub2 + " equals " + sub3 + "!");
            }
            if("m".equals(action)) //multiplication
            {
                System.out.println("Now type in the first number you would like to multiply.");
                int mul1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int mul2 = scan.nextInt();
                int mul3 = mul1 * mul2;
                System.out.println(mul1 + " multiplied by " + mul2 + " equals " + mul3 + "!");
            }
            if("d".equals(action))//division
            {
                System.out.println("Now type in the first number you would like to divide.");
                int div1 = scan.nextInt();
                System.out.println("Now type the second number.");
                int div2 = scan.nextInt();
                int div3 = div1 / div2;
                System.out.println(div1 + " divided by " + div2 + " equals " + div3 + "!");
            }
            System.out.println("Would you like to start over? (yes,no)");
            String startOver = scan1.nextLine();
            if("no".equals(startOver))
            {
                go = false;
                System.out.println("Bye");
            }
        }
    }
}


//Using AWT:


import java.awt.*;
import java.awt.event.*;
public class SimpleCalculator implements ActionListener
{
    // containers
    private Frame f;
    private Panel p1, p2, p3, p4;
    // components
    private Label l1, l2, l3;
    private TextField tf1, tf2, tf3;
    private Button bAdd, bSub, bMul, bDiv, bClear;
    public SimpleCalculator()
    {
        f = new Frame("Calculator");
        p1 = new Panel();
        p2 = new Panel();
        p3 = new Panel();
        p4 = new Panel();
        l1 = new Label("First: ");
        l2 = new Label("Second: ");
        l3 = new Label("Result: ");
        tf1 = new TextField(15);
        tf2 = new TextField(15);
        tf3 = new TextField(15);
        bAdd = new Button("+");
        bSub = new Button("-");
        bMul = new Button("*");
        bDiv = new Button("/");
        bClear = new Button("C");
    }
    public void launchFrame()
    {
        // use default layout manager of the Panel (FlowLayout)
        p1.add(l1);
        p1.add(tf1);
        p2.add(l2);
        p2.add(tf2);
        p3.add(l3);
        p3.add(tf3);
        p4.add(bAdd);
        p4.add(bSub);
        p4.add(bMul);
        p4.add(bDiv);
        p4.add(bClear);
        // change the layout manager of the Frame,
        // use GridLayout(4, 1)
        f.setLayout(new GridLayout(4, 1));
        f.add(p1);
        f.add(p2);
        f.add(p3);
        f.add(p4);
        f.pack();
        f.setVisible(true);
        // register event handlers
        bAdd.addActionListener(this);
        bSub.addActionListener(this);
        bMul.addActionListener(this);
        bDiv.addActionListener(this);
        bClear.addActionListener(this);
        f.addWindowListener(new MyCloseButtonHandler());
    }
    // override the actionPerformed method
    public void actionPerformed(ActionEvent ae)
    {
        Object source = ae.getSource();
        double num1, num2, result = 0.0;
        if (tf1.getText() != null && tf2.getText() != null)
        {
            num1 = Double.parseDouble(tf1.getText());
            num2 = Double.parseDouble(tf2.getText());
            if (source == bAdd)
                result = num1 + num2;
            else if (source == bSub)
                result = num1 - num2;
            else if (source == bMul)
                result = num1 * num2;
            else if (source == bDiv)
                result = num1 / num2;
            else if (source == bClear)
            {
                tf1.setText("0.0");
                tf2.setText("0.0");
                tf3.setText("0.0");
            }
            tf3.setText(new Double(result).toString());
        }
    }
    private class MyCloseButtonHandler extends WindowAdapter
    {
        public void windowClosing(WindowEvent we)
        {
            System.exit(0);
        }
    }
    public static void main(String args[])
    {
        SimpleCalculator sc = new SimpleCalculator();
        sc.launchFrame();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS