View Full Version: Calculator in java

C++ Learning Community > Other Programing Languages > Calculator in java


Title: Calculator in java
Description: In the making


kccc - November 21, 2006 09:35 PM (GMT)
I'm currently working on a calculator in my primary programming language, Java. I started it off as a command prompt and now I'm including GUI.

CODE
import java.util.*;

class calc1001 {
public static void main (String[] args) {
 Scanner scan = new Scanner (System.in);
 double inputX;
 double inputY;
 double solution = 0.0;
 char inputLetter = 'q';
 String inputString;

 for (int i = 0; i < 1;) {
  System.out.println ("\na... Addition\ns... Subtraction/Sqareroot\nm... Multiplication\nd... Division\nh... Hypotenuse\nl... Logarithm(base ten)\n");
 
  System.out.print ("Enter first number: ");
  inputX = scan.nextDouble();
 
  System.out.print ("Enter second number (enter zero if none): ");
  inputY = scan.nextDouble();

  System.out.print ("Enter corresponding letter: ");
  inputString = scan.next().trim();
  inputLetter = inputString.charAt(0);
 
  switch (inputLetter) {
   case 'a':
    solution = inputX + inputY;
    break;
   case 's':
    if (inputY !=  0)
     solution = inputX - inputY;
    else
     solution = Math.sqrt(inputX);
    break;
   case 'm':
    solution = inputX * inputY;
    break;
   case 'd':
    solution = inputX / inputY;
    break;
   case 'h':
    solution = Math.hypot(inputX, inputY);
    break;
   case 'l':
    solution = Math.log10(inputX);
    break;
   case 'q':
    i = 1;
    break;
   default:
    solution = 0;
    String outputA = "\nThe operation entered was not recognized.";
    System.out.println (outputA);
    break;
  }
 
  if (i == 0)
   System.out.println ("\nSolution: " + solution + "\n");
 }
}
}

kccc - November 25, 2006 06:30 PM (GMT)
Here's where I am so far, tell me any techniques to shorten it up or to improve the code please if you know any:
CODE

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

public class calc2100 extends JFrame implements ActionListener {
private double storedDouble = 0.0;
private double displayDouble = 0.0;
private int count = 0;
private String operation = " ";
private char opChar = operation.charAt(0);

JTextField display = new JTextField(20);

JLabel padding0 = new JLabel("   ");
JLabel padding1 = new JLabel("   ");
JLabel padding2 = new JLabel("   ");
JLabel padding3 = new JLabel("   ");

JPanel butPanel = new JPanel();
JPanel opPanel  = new JPanel();
JPanel numPanel = new JPanel();
JPanel botPanel = new JPanel();

private JButton add = new JButton(" + ");
private JButton sub = new JButton(" - ");
private JButton mul = new JButton(" * ");
private JButton div = new JButton(" / ");
private JButton mod = new JButton("mod");
private JButton hyp = new JButton("hyp");
private JButton log = new JButton("log");
private JButton sqr = new JButton("Sqr");

private JButton one = new JButton(" 1 ");
private JButton two = new JButton(" 2 ");
private JButton thr = new JButton(" 3 ");
private JButton fou = new JButton(" 4 ");
private JButton fiv = new JButton(" 5 ");
private JButton six = new JButton(" 6 ");
private JButton sev = new JButton(" 7 ");
private JButton eig = new JButton(" 8 ");
private JButton nin = new JButton(" 9 ");
private JButton zer = new JButton(" 0 ");
private JButton dot = new JButton(" . ");
private JButton exp = new JButton(" e ");

private JButton clear = new JButton("C/CE");
private JButton equal = new JButton(" =  ");
private JButton close = new JButton("Exit");

String displayText   = " ";

public calc2100() {
 
 getContentPane().setLayout(new FlowLayout());
 
 getContentPane().add(display);
 display.setText("0");
 display.setEditable(false);
 
 opPanel.setLayout(new GridLayout(6, 2));
 opPanel.add(add);
 opPanel.add(sub);
 opPanel.add(mul);
 opPanel.add(div);
 opPanel.add(mod);
 opPanel.add(hyp);
 opPanel.add(log);
 opPanel.add(sqr);
 opPanel.add(padding0);
 opPanel.add(padding1);
 opPanel.add(padding2);
 opPanel.add(padding2);
 
 numPanel.setLayout(new GridLayout(6, 2));
 numPanel.add(sev);
 numPanel.add(eig);
 numPanel.add(nin);
 numPanel.add(fou);
 numPanel.add(fiv);
 numPanel.add(six);
 numPanel.add(one);
 numPanel.add(two);
 numPanel.add(thr);
 numPanel.add(zer);
 numPanel.add(dot);
 numPanel.add(exp);
 
 botPanel.setLayout(new GridLayout(0, 3));
 botPanel.add(clear);
 botPanel.add(equal);
 botPanel.add(close);
 
 butPanel.setLayout(new BoxLayout(butPanel, BoxLayout.X_AXIS));
 butPanel.add(opPanel);
 butPanel.add(numPanel);
 
 getContentPane().add(display);
 getContentPane().add(butPanel);
 getContentPane().add(botPanel);
 
 add.addActionListener(this);
 sub.addActionListener(this);
 mul.addActionListener(this);
 div.addActionListener(this);
 mod.addActionListener(this);
 hyp.addActionListener(this);
 log.addActionListener(this);
 sqr.addActionListener(this);
 
 zer.addActionListener(this);
 one.addActionListener(this);
 two.addActionListener(this);
 thr.addActionListener(this);
 fou.addActionListener(this);
 fiv.addActionListener(this);
 six.addActionListener(this);
 sev.addActionListener(this);
 eig.addActionListener(this);
 nin.addActionListener(this);
 dot.addActionListener(this);
 exp.addActionListener(this);
 
 clear.addActionListener(this);
 equal.addActionListener(this);
 close.addActionListener(this);
 
 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

void writeTo (String term) {
 String original = display.getText();
 String newString = original + term;
 display.setText(newString);
 displayDouble = Double.parseDouble(term);
}

private void add() {
 if (count == 0) {
  opChar = 'a';
  storedDouble = displayDouble;
  clear();
  count++;
 }
 else {
  displayText = (displayDouble + storedDouble) + " ";
  display.setText(displayText);
  count--;
 }
}

private void sub() {
 if (count == 0) {
  opChar = 's';
  storedDouble = displayDouble;
  clear();
  count++;
 }
 else {
  displayText = (displayDouble - storedDouble)+ " ";
  display.setText(displayText);
  count--;
 }
}

private void mul() {
 if (count == 0) {
  opChar = 'm';
  storedDouble = displayDouble;
  clear();
  count++;
 }
 else {
  displayText = (displayDouble * storedDouble)+ " ";
  display.setText(displayText);
  count--;
 }
}

private void div() {
 if (count == 0) {
  opChar = 'd';
  storedDouble = displayDouble;
  clear();
  count++;
 }
 else {
  displayText = (displayDouble / storedDouble)+ " ";
  display.setText(displayText);
  count--;
 }

}

private void mod() {
 if (count == 0) {
  opChar = 'i';
  storedDouble = displayDouble;
  clear();
  count++;
 }
 
 else {
  displayText = (displayDouble % storedDouble)+ " ";
  display.setText(displayText);
  count--;
 }

}

private void hyp() {
 if (count == 0) {
  opChar = 'h';
  storedDouble = displayDouble;
  clear();
  count++;
 }
 
 else {
  displayText = (Math.hypot(displayDouble, storedDouble))+ " ";
  display.setText(displayText);
  count--;
 }
}

private void log() {
 displayText = Math.log10(displayDouble)+ " ";
 display.setText(displayText);
}

private void sqr() {
 displayText = Math.sqrt(displayDouble)+ " ";
 display.setText(displayText);
}

private void clear() {
 display.setText(" ");
 opChar = ' ';
}

private void equal() {
 count = 1;
 
 switch (opChar) {
  case 'a':
   add();
   break;
  case 's':
   sub();
   break;
  case 'm':
   mul();
   break;
  case 'd':
   div();
   break;
  case 'i':
   mod();
   break;
  case 'h':
   hyp();
   break;
  default:
   clear();
   break;
 }
}

public void actionPerformed (ActionEvent e) {
 if (e.getSource() == add)
  add();
 if (e.getSource() == sub)
  sub();
 if (e.getSource() == mul)
  mul();
 if (e.getSource() == div)
  div();
 if (e.getSource() == mod)
  mod();
 if (e.getSource() == hyp)
  hyp();
 if (e.getSource() == log)
  log();
 if (e.getSource() == sqr)
  sqr();
 if (e.getSource() == one)
  writeTo("1");
 if (e.getSource() == two)
  writeTo("2");
 if (e.getSource() == thr)
  writeTo("3");
 if (e.getSource() == fou)
  writeTo("4");
 if (e.getSource() == fiv)
  writeTo("5");
 if (e.getSource() == six)
  writeTo("6");
 if (e.getSource() == sev)
  writeTo("7");
 if (e.getSource() == eig)
  writeTo("8");
 if (e.getSource() == nin)
  writeTo("9");
 if (e.getSource() == zer)
  writeTo("0");
 if (e.getSource() == dot)
  writeTo(".");
 if (e.getSource() == exp)
  writeTo("e");
 if (e.getSource() == clear)
  clear();
 if (e.getSource() == equal)
  equal();
 if (e.getSource() == close)
  System.exit(0);
}
public static void main (String[] args) {
 calc2100 newCalc = new calc2100();
 newCalc.setSize(270, 295);
 newCalc.setVisible(true);
 newCalc.setTitle("Calculator");
}
}


Edit - improved




Hosted for free by InvisionFree