Basic Java Code Examples for
Beginners
import java.util.Scanner;
public class MyClass
{
public static void main(String[] args)
{
Maths math = new Maths();
Scanner userinput = new Scanner(System.in);
System.out.println("Welcome to Java Calculator");
System.out.println("===========================");
System.out.print("Enter First Number:");
int num1 = userinput.nextInt();
System.out.print("Enter Second Number:");
int num2= userinput.nextInt();
System.out.print("Enter operation to perform (+,-,x,/):");
String operation= userinput.next();
if (operation.equals("+"))
System.out.println(math.Add(num1, num2));
else if (operation.equals("-"))
System.out.println(math.Subtract(num1, num2));
else if (operation.equals("x"))
System.out.println(math.Multiply(num1, num2));
else if (operation.equals("/"))
System.out.println(math.Divide(num1, num2));
else
System.out.println("The operation is not valid.");
}
}
Simple Calculator
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class Calc extends JFrame
{
private static final Font Bigger_font = new Font("monspaced", Font.PLAIN, 20);
private JTextField _displayField;
private boolean _startNumber = true;
private String _previousOp = "=";
private CalcLogic _logic = new CalcLogic();
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()
{
_displayField = new JTextField("0", 12);
_displayField.setHorizontalAlignment(JTextField.Right);
_displayField.setFont(Bigger_Font);
JButton clearButton = new JButton("Clear");
clearButton.setFont(Bigger_Font);
clearButton.addActionListener(new ClearListener());
ActionListener numListener = new NumListener();
String buttonOrder = "789456123 0 ";
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 3, 2, 2));
for (int i = 0; i < buttonOrder.length(); i++)
{
String keyTop = buttonOrder.substring(i, i+1);
JButton b = new JButton(keyTop);
if (keyTop.equals(" "))
{
b.setEnabled(false);
}
else
{
b.addActionListener(numListener);
b.setFont(Bigger_Font);
}
buttonPanel.add(b);
}
ActionListener opListener = new OpListener();
JPanel opPanel = new JPanel();
opPanel.setLayout(new GridLayout(5, 1, 2, 2));
String[] opOrder = {"+", "-", "*", "/", "="};
for (int i = 0; i < opOrder.length; i++)
{
JButton b = new JButton(opOrder[i]);
b.addActionListener(opListener);
b.setFont(Bigger_Font);
opPanel.add(b);
}
JPanel clearPanel = new JPanel();
clearPanel.setLayout(new FlowLayout());
clearPanel.add(clearButton);
JPanel content = new JPanel();
content.setLayout(new BorderLayout(5, 5));
content.add(_displayField, BorderLayout.North );
content.add(buttonPanel , BorderLayout.Center);
content.add(opPanel , BorderLayout.East );
content.add(clearPanel , BorderLayout.South );
content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
this.setContentPane(content);
this.pack();
this.setTitle("Simple Calc");
this.setResizable(false);
this.setLocationRelativeTo(null);
}
private void actionClear()
{
_startNumber = true;
_displayField.setText("0");
_previousOp = "=";
_logic.setTotal("0");
}
class OpListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (_startNumber)
{
actionClear();
_displayField.setText("ERROR - No operator");
}
else
{
startNumber = true;
try
{
String displayText = _displayField.getText();
if (_previousOp.equals("="))
{
_logic.setTotal(displayText);
}
else if (_previousOp.equals("+"))
{
_logic.add(displayText);
}
else if (_previousOp.equals("-"))
{
_logic.subtract(displayText);
}
else if (_previousOp.equals("*"))
{
_logic.multiply(displayText);
}
else if (_previousOp.equals("/"))
{
_logic.divide(displayText);
}
displayField.setText("" + _logic.getTotalString());
}
catch (NumberFormatException ex)
{
actionClear();
displayField.setText("Error");
}
previousOp = e.getActionCommand();
}
}
}
class NumListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String digit = e.getActionCommand();
if (_startNumber)
displayField.setText(digit);
startNumber = false;
}
else
{
displayField.setText(_displayField.getText() + digit);
}
}
}
class ClearListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
actionClear();
}
}
}
No comments:
Post a Comment