answersLogoWhite

0


Best Answer

import java.io.*;

import java.util.*;

public class Calculator

{

public static void main(String args[])

{

System.out.println("Make your arithmetic selection from the choices below:\n");

System.out.println(" 1. Addition");

System.out.println(" 2. Subtraction");

System.out.println(" 3. Multiplication");

System.out.println(" 4. Division\n");

System.out.print(" Your choice? ");

Scanner kbReader = new Scanner(System.in);

int choice = kbReader.nextInt();

if((choice<=4) && (choice>0))

{

System.out.print("\nEnter first operand. ");

double op1 = kbReader.nextDouble();

System.out.print("\nEnter second operand.");

double op2 = kbReader.nextDouble();

System.out.println("");

switch (choice)

{

case 1: //addition

System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2) );

break;

case 2: //subtraction

System.out.println(op1 + " minus " + op2 + " = " + (op1 - op2) );

break;

case 3: //multiplication

System.out.println(op1 + " times " + op2 + " = " + (op1 * op2) );

break;

case 4: //division

System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2) );

}

}

else

{

System.out.println("Please enter a 1, 2, 3, or 4.");

}

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

In this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.

Writing Calculator Program in SwingIn this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.

For developing a small calculator program in swing we need two different classes

1) SwingCalculator.java

2) Calculator.java

The SwingCalculator.java calls the Calculator.javaclass by JFrame frame = new Calculator(). All the methods and actions are to be performed in Calculator.javaclass.

Calculator Code in Java Swing

Please save the code as SwingCalculator.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

class SwingCalculator {

public static void main(String[] args) {

JFrame frame = new Calculator();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

Here is the code of Calculator.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

class Calculator extends JFrame {

private final Font BIGGER_FONT = newFont("monspaced",

Font.PLAIN, 20);

private JTextField textfield;

private boolean number = true;

private String equalOp = "=";

private CalculatorOp op = new CalculatorOp();

public Calculator() {

textfield = new JTextField("0", 12);

textfield.setHorizontalAlignment(JTextField.RIGHT);

textfield.setFont(BIGGER_FONT);

ActionListener numberListener = new NumberListener();

String buttonOrder = "1234567890 ";

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(4, 4, 4, 4));

for (int i = 0; i < buttonOrder.length(); i++) {

String key = buttonOrder.substring(i, i+1);

if (key.equals(" ")) {

buttonPanel.add(new JLabel(""));

} else {

JButton button = new JButton(key);

button.addActionListener(numberListener);

button.setFont(BIGGER_FONT);

buttonPanel.add(button);

}

}

ActionListener operatorListener = newOperatorListener();

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(4, 4, 4, 4));

String[] opOrder = {"+", "-", "*", "/","=","C"};

for (int i = 0; i < opOrder.length; i++) {

JButton button = new JButton(opOrder[i]);

button.addActionListener(operatorListener);

button.setFont(BIGGER_FONT);

panel.add(button);

}

JPanel pan = new JPanel();

pan.setLayout(new BorderLayout(4, 4));

pan.add(textfield, BorderLayout.NORTH );

pan.add(buttonPanel , BorderLayout.CENTER);

pan.add(panel , BorderLayout.EAST );

this.setContentPane(pan);

this.pack();

this.setTitle("Calculator");

this.setResizable(false);

}

private void action() {

number = true;

textfield.setText("0");

equalOp = "=";

op.setTotal("0");

}

class OperatorListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (number) {

action();

textfield.setText("0");

} else {

number = true;

String displayText = textfield.getText();

if (equalOp.equals("=")) {

op.setTotal(displayText);

} else if (equalOp.equals("+")) {

op.add(displayText);

} else if (equalOp.equals("-")) {

op.subtract(displayText);

} else if (equalOp.equals("*")) {

op.multiply(displayText);

} else if (equalOp.equals("/")) {

op.divide(displayText);

}

textfield.setText("" + op.getTotalString());

equalOp = e.getActionCommand();

}

}

}

class NumberListener implements ActionListener {

public void actionPerformed(ActionEvent event) {

String digit = event.getActionCommand();

if (number) {

textfield.setText(digit);

number = false;

} else {

textfield.setText(textfield.getText() + digit);

}

}

}

public class CalculatorOp {

private int total;

public CalculatorOp() {

total = 0;

}

public String getTotalString() {

return ""+total;

}

public void setTotal(String n) {

total = convertToNumber(n);

}

public void add(String n) {

total += convertToNumber(n);

}

public void subtract(String n) {

total -= convertToNumber(n);

}

public void multiply(String n) {

total *= convertToNumber(n);

}

public void divide(String n) {

total /= convertToNumber(n);

}

private int convertToNumber(String n) {

return Integer.parseInt(n);

}

}

}

The constructor new CalculatorOp() calls the CalculatorOp class. The Swing component JTextField is used to create textbox on which calculation is to be performed. JPanel arranges the numeric buttons in a panel. JButton is used to perform an action. OperatorListener class is called to perform action on operators, i.e, '+,-,*,/,='. The class NumberListener is called for numbers 0 to 9.

Output will be displayed as:

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

I made this in bluej(Free Java compiler)

import java.io.*;

import java.util.*;

public class Calculator

{

public static void main(String args[])

{

System.out.println("Make your arithmetic selection from the choices below:\n");

System.out.println(" 1. Addition");

System.out.println(" 2. Subtraction");

System.out.println(" 3. Multiplication");

System.out.println(" 4. Division\n");

System.out.print(" Your choice? ");

Scanner kbReader = new Scanner(System.in);

int choice = kbReader.nextInt();

if((choice<=4) && (choice>0))

{

System.out.print("\nEnter first operand. ");

double op1 = kbReader.nextDouble();

System.out.print("\nEnter second operand.");

double op2 = kbReader.nextDouble();

System.out.println("");

switch (choice)

{

case 1: //addition

System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2) );

break;

case 2: //subtraction

System.out.println(op1 + " minus " + op2 + " = " + (op1 - op2) );

break;

case 3: //multiplication

System.out.println(op1 + " times " + op2 + " = " + (op1 * op2) );

break;

case 4: //division

System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2) );

}

}

else

{

System.out.println("Please enter a 1, 2, 3, or 4.");

}

}

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.text.*;

import javax.swing.border.*;

class Calc extends JFrame implements ActionListener

{

private static final Font BIGGER_FONT = new Font("Tahoma", Font.BOLD, 13);

private JRadioButton decimalb,binaryb,octalb,hexab,enableb, disableb;

private JTextField _displayField, decimalTF, binaryTF, octalTF, hexaTF;

private JLabel decimalL,binaryL,octalL,hexaL;

private JButton exitb, convertb, clearb, backSpace, b9, b8, b7, b6, b5, b4, b3, b2, b1, b0, b11, b12, b13;

private JButton b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25;

private JPanel cbPanel, upperButtonPanel;

private ButtonGroup group, group2;

private boolean pluz = false;

private boolean subs = false;

private boolean multiply = false;

private boolean divide = false;

private boolean _startNumber = true;

private boolean dotCheck = false;

private String firstValue, Secondvalue;

private String _previousOp = "=";

private DecimalFormat df = new DecimalFormat("###,###,###.##");

private CalculatorLogic calLogic = new CalculatorLogic();

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

} catch (Exception unused) {

;

}

Calc window = new Calc();

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setVisible(true);

}

public Calc()

{

super("Calculator With Number System Converter");

_displayField = new JTextField("", 15);

_displayField.setHorizontalAlignment(JTextField.RIGHT);

_displayField.setFont(BIGGER_FONT);

_displayField.setEditable(false);

_displayField.setBackground(Color.WHITE);

upperButtonPanel = new JPanel();

upperButtonPanel.setLayout(new FlowLayout());

upperButtonPanel.setBackground(Color.lightGray);

upperButtonPanel.setPreferredSize(new Dimension(170, 300));

decimalb = new JRadioButton("Decimal", false);

binaryb = new JRadioButton("Binary", false);

octalb = new JRadioButton("Octal", false);

hexab = new JRadioButton("Hexadecimal", false);

decimalb.setFont(BIGGER_FONT);

binaryb.setFont(BIGGER_FONT);

octalb.setFont(BIGGER_FONT);

hexab.setFont(BIGGER_FONT);

decimalb.addActionListener(this);

binaryb.addActionListener(this);

octalb.addActionListener(this);

hexab.addActionListener(this);

group = new ButtonGroup();

group.add(decimalb);

group.add(binaryb);

group.add(octalb);

group.add(hexab);

upperButtonPanel.add(decimalb);

upperButtonPanel.add(binaryb);

upperButtonPanel.add(octalb);

upperButtonPanel.add(hexab);

upperButtonPanel.setVisible(false);

JPanel panel = new JPanel(new GridLayout(10, 1));

panel.setPreferredSize(new Dimension(300, 300));

panel.setBorder(new BevelBorder(BevelBorder.LOWERED));

decimalTF = new JTextField(20);

binaryTF = new JTextField(20);

octalTF = new JTextField(20);

hexaTF = new JTextField(20);

decimalTF.setEditable(false);

binaryTF.setEditable(false);

octalTF.setEditable(false);

hexaTF.setEditable(false);

decimalTF.setBackground(Color.WHITE);

binaryTF.setBackground(Color.WHITE);

octalTF.setBackground(Color.WHITE);

hexaTF.setBackground(Color.WHITE);

decimalTF.setFont(BIGGER_FONT);

binaryTF.setFont(BIGGER_FONT);

octalTF.setFont(BIGGER_FONT);

hexaTF.setFont(BIGGER_FONT);

decimalTF.setVisible(false);

binaryTF.setVisible(false);

octalTF.setVisible(false);

hexaTF.setVisible(false);

decimalL = new JLabel("Decimal Value");

binaryL = new JLabel("Binary Value");

octalL = new JLabel("Octal Value");

hexaL = new JLabel("Hexadecimal Value");

decimalL.setFont(BIGGER_FONT);

binaryL.setFont(BIGGER_FONT);

octalL.setFont(BIGGER_FONT);

hexaL.setFont(BIGGER_FONT);

decimalL.setVisible(false);

binaryL.setVisible(false);

octalL.setVisible(false);

hexaL.setVisible(false);

panel.add(decimalL);

panel.add(decimalTF);

panel.add(binaryL);

panel.add(binaryTF);

panel.add(octalL);

panel.add(octalTF);

panel.add(hexaL);

panel.add(hexaTF);

upperButtonPanel.add(panel);

add(upperButtonPanel, BorderLayout.NORTH);

JPanel buttonPanel = new JPanel();

buttonPanel.setLayout(new GridLayout(5, 2, 5, 5));

buttonPanel.setBackground(Color.lightGray);

buttonPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));

b9 = new JButton("9");

b8 = new JButton("8");

b7 = new JButton("7");

b6 = new JButton("6");

b5 = new JButton("5");

b4 = new JButton("4");

b3 = new JButton("3");

b2 = new JButton("2");

b1 = new JButton("1");

b0 = new JButton("0");

b11 = new JButton(".");

b12 = new JButton("/");

b13 = new JButton("*");

b14 = new JButton("-");

b15 = new JButton("+");

b16 = new JButton("A");

b17 = new JButton("B");

b18 = new JButton("C");

b19 = new JButton("D");

b20 = new JButton("E");

b21 = new JButton("F");

b22 = new JButton("=");

b23 = new JButton("sqrt");

b24 = new JButton("x\u00B2");

b25 = new JButton("x\u00B3");

b1.setFont(BIGGER_FONT);

b2.setFont(BIGGER_FONT);

b3.setFont(BIGGER_FONT);

b4.setFont(BIGGER_FONT);

b5.setFont(BIGGER_FONT);

b6.setFont(BIGGER_FONT);

b7.setFont(BIGGER_FONT);

b8.setFont(BIGGER_FONT);

b9.setFont(BIGGER_FONT);

b0.setFont(BIGGER_FONT);

b11.setFont(BIGGER_FONT);

b12.setFont(BIGGER_FONT);

b13.setFont(BIGGER_FONT);

b14.setFont(BIGGER_FONT);

b15.setFont(BIGGER_FONT);

b16.setFont(BIGGER_FONT);

b17.setFont(BIGGER_FONT);

b18.setFont(BIGGER_FONT);

b19.setFont(BIGGER_FONT);

b20.setFont(BIGGER_FONT);

b21.setFont(BIGGER_FONT);

b22.setFont(BIGGER_FONT);

b23.setFont(BIGGER_FONT);

b24.setFont(BIGGER_FONT);

b25.setFont(BIGGER_FONT);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

b11.addActionListener(this);

b12.addActionListener(this);

b13.addActionListener(this);

b14.addActionListener(this);

b15.addActionListener(this);

b16.addActionListener(this);

b17.addActionListener(this);

b18.addActionListener(this);

b19.addActionListener(this);

b20.addActionListener(this);

b21.addActionListener(this);

b22.addActionListener(this);

b23.addActionListener(this);

b24.addActionListener(this);

b25.addActionListener(this);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

buttonPanel.add(b7);

buttonPanel.add(b8);

buttonPanel.add(b9);

buttonPanel.add(b12);

buttonPanel.add(b13);

buttonPanel.add(b15);

buttonPanel.add(b4);

buttonPanel.add(b5);

buttonPanel.add(b6);

buttonPanel.add(b15);

buttonPanel.add(b14);

buttonPanel.add(b11);

buttonPanel.add(b1);

buttonPanel.add(b2);

buttonPanel.add(b3);

buttonPanel.add(b0);

buttonPanel.add(b22);

buttonPanel.add(b16);

buttonPanel.add(b17);

buttonPanel.add(b18);

buttonPanel.add(b11);

buttonPanel.add(b23);

buttonPanel.add(b19);

buttonPanel.add(b20);

buttonPanel.add(b21);

buttonPanel.add(b24);

buttonPanel.add(b25);

add(buttonPanel);

group2 = new ButtonGroup();

disableb = new JRadioButton("Disable Converter", true);

enableb = new JRadioButton("Enable Converter", false);

disableb.setFont(BIGGER_FONT);

enableb.setFont(BIGGER_FONT);

disableb.addActionListener(this);

enableb.addActionListener(this);

group2.add(disableb);

group2.add(enableb);

cbPanel = new JPanel(new GridLayout(3, 3));

cbPanel.setBorder(new BevelBorder(BevelBorder.RAISED));

cbPanel.add(disableb);

cbPanel.add(enableb);

convertb = new JButton("Convert");

clearb = new JButton("Clear");

exitb = new JButton("Exit");

backSpace = new JButton("BackSpace");

convertb.addActionListener(this);

clearb.addActionListener(this);

exitb.addActionListener(this);

backSpace.addActionListener(this);

convertb.setFont(BIGGER_FONT);

clearb.setFont(BIGGER_FONT);

exitb.setFont(BIGGER_FONT);

backSpace.setFont(BIGGER_FONT);

cbPanel.add(convertb);

cbPanel.add(clearb);

cbPanel.add(backSpace);

cbPanel.add(exitb);

convertb.setEnabled(false);

JPanel content2 = new JPanel();

content2.setLayout(new BorderLayout(5, 5));

content2.add(_displayField, BorderLayout.NORTH);

content2.add(buttonPanel , BorderLayout.CENTER);

content2.add(cbPanel, BorderLayout.SOUTH);

add(content2, BorderLayout.SOUTH);

this.setSize(340, 300);

this.setResizable(false);

this.setLocationRelativeTo(null);

}

public void actionPerformed(ActionEvent e)

{

try{

if(decimalb.isSelected())

{

b0.setEnabled(true);

b1.setEnabled(true);

b2.setEnabled(true);

b3.setEnabled(true);

b4.setEnabled(true);

b5.setEnabled(true);

b6.setEnabled(true);

b7.setEnabled(true);

b8.setEnabled(true);

b9.setEnabled(true);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

}

else if(binaryb.isSelected())

{

b2.setEnabled(false);

b3.setEnabled(false);

b4.setEnabled(false);

b5.setEnabled(false);

b6.setEnabled(false);

b7.setEnabled(false);

b8.setEnabled(false);

b9.setEnabled(false);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

}

else if(octalb.isSelected())

{

b0.setEnabled(true);

b1.setEnabled(true);

b2.setEnabled(true);

b3.setEnabled(true);

b4.setEnabled(true);

b5.setEnabled(true);

b6.setEnabled(true);

b7.setEnabled(true);

b16.setEnabled(false);

b17.setEnabled(false);

b18.setEnabled(false);

b19.setEnabled(false);

b20.setEnabled(false);

b21.setEnabled(false);

b8.setEnabled(false);

b9.setEnabled(false);

}

else if(hexab.isSelected())

{

b0.setEnabled(true);

b1.setEnabled(true);

b2.setEnabled(true);

b3.setEnabled(true);

b4.setEnabled(true);

b5.setEnabled(true);

b6.setEnabled(true);

b7.setEnabled(true);

b8.setEnabled(true);

b9.setEnabled(true);

b16.setEnabled(true);

b17.setEnabled(true);

b18.setEnabled(true);

b19.setEnabled(true);

b20.setEnabled(true);

b21.setEnabled(true);

}

if((e.getActionCommand().equals("Convert")) && decimalb.isSelected())

{

String input, bin="",oct="",hex="";

int rem,rem2,rem3,len;

char x;

input = _displayField.getText().trim();

try{

if(input.equals(""))

throw new Exception("Error");

else

len = input.length();

int num = Integer.parseInt(input);

while(num>0)

{

rem = num%2;

num/=2;

bin = rem + bin;

}

binaryTF.setText(bin);

decimalTF.setText(input);

int num2 = Integer.parseInt(input);

while(num2>0)

{

rem2 = num2%8;

oct = rem2 + oct;

num2/=8;

}

octalTF.setText(oct);

int num3 = Integer.parseInt(input);

while(num3>0)

{

rem3 = num3%16;

if(rem3<10)

hex = rem3 + hex;

else if(rem3 0)

{

JOptionPane.showMessageDialog(null,"Can't divide by zero", "Error Message", JOptionPane.ERROR_MESSAGE);

return 0;

}

else

return (a / b);

}

public double stringToDouble(String a)

{

return Double.parseDouble(a);

}

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How develop java calculator using awt?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possible to draw in java without using applet?

Yes it is possible to draw in java by using AWT package. or by using javax package.


Difference between awt and java script?

AWT is a Java package for creating graphical user interfaces. JavaScript is a completely unrelated programming language.


Which are the AWT components in Java?

AWT stands for Abstract window toolkit. AWT gives us the components using which we can create User- Interface based applications in java. Some of the components are: a. Frame b. Panel c. Window d. CheckBox e. RadioButton f. Button g. TextBox h. TextArea i. Etc


What is the methods with the parameters of an AWT?

AWT (Abstract Window Toolkit) is a top-level Java package. Listing out the hundreds or thousands of methods would be a waste of effort. See the related link below for the Java documentation on the AWT package.


What do you mean by AWT?

AWT stands for Abstract Window Toolkit It contains the list of java classes &amp; packages that can be used to create UI based applications using java. Some components available are: * button * text field * text area * scroll bar * etc...


What is awt in java explain its features?

awt contains all the graphical features of java, including many different elements to create an acceptable and usable GUI for applications easily. such as buttons, sliders and text areas.


Why is itemstatechanged method called thrice on changing an item in java swings?

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ItemListener.html#itemStateChanged%28java.awt.event.ItemEvent%29 "http://docs.oracle.com/javase/7/docs/api/java/awt/event/ItemListener.html#itemStateChanged%28java.awt.event.ItemEvent%29"


What are Awt Components?

AWT stands for Abstract window tootlkit . Abstract window Toolkit provides a standard application programming interface for writing graphical user interfaces in java.


What does someone use AWT for?

AWT (Abstract Window Toolkit) is Java's original widget program allowing webmasters to add outside widgets to their sites for users to interface between them and other sites.


What are the types of java program?

There are many different types of Java programs. a. Simple Standalone applications - They are simple java programs that have a bunch of classes and have a starting class that has a main method b. UI Based Applications - These are UI based applications built using AWT or Java Swings c. J2EE Web Applications - These are web based applications that can be run in a web browser. They are coded using technologies like servlets, jsp, hibernate, spring etc.


Which is best institute to learn java and mainframe course in pune?

Virtual Vision Institute in Pune is the best for Java Course. You get best training regarding Java interfaces, Java Packages, Java AWT & SwingsFor More Information you can visit our websitevirtualvision.in/core-java/Contact Number : 9766119340


What is awt in java?

java.awt is a standard package of Java. It is a GUI(Graphical User Interface) package, which has classes in it such as Frame, Panel, and Button. Most of the package was later replaced with the javax.swing package, which has most of the same classes, only with a J prepended to them (JFrame, JPanel, JButton). However the java.awt package still includes some event handlers that are considered standard in java (java.awt.event).