/* Title - Calculator.java * * Author - Coding By David Mann - 0203401 * * * Description - A computer based calculator resembling a standard calculator. Each * function is utilised by clicking a button on the screen. * * *Note* - This is an early version of the program and as such many errors and * bugs remain within the program. This was due to the limited amount * of time available to complete the program. These errors and bugs will * hopefully be eradicated by the next version. */ //the imports required for the Applet import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.border.*; //start of the Calculators class public class Calculators extends JApplet implements ActionListener{ //New button variables for display on calculator private JButton powerBut, memRecBut, memPlusBut, memMinusBut, divideBut, percentageBut, sevenBut, eightBut, nineBut, timesBut, sqrRootBut, fourBut, fiveBut, sixBut, minusBut, clearBut, oneBut, twoBut, threeBut, plusBut, allClearBut, zeroBut, pointBut, equalsBut, infoBut; //Create new areas for display on calculator private JPanel displayPanel = new JPanel(); private JPanel buttonPanel = new JPanel(); private JTextField ledDisplay = new JTextField(13); //variables used to store calculator strings and numbers private String memory = new String(); private String ledString; private double ledNumber; private String firstString; private String secondString; private double firstNumber; private double secondNumber; private String calculatedString; private double calculatedNumber; private int selection = 0; private int numberDigits = 0; Calculations myCalc = new Calculations(); public void init() { Container pane = getContentPane();//create a new container object pane.setLayout(new BorderLayout());//define a borderLayout for the container setBackground(Color.black);//set the container background color ledString = "0";//set the initial state of the ledString showLedDisplay();//call showLedDisplay method buttonPanel.setSize(242,200);//set the size of the button panel buttonPanel.setLayout(new GridLayout(5,5)); //define a grid layout for the buttons buttonPanel.setBackground(Color.black);//set the button background color pane.add("North", displayPanel);//add displayPanel to the container pane.add("Center", buttonPanel);//add buttonPanel to the container setVisible(true);//make everything visible //Call method buttonDetails and assign to buttons listed powerBut = buttonDetails("x^y", "powerBut", 40, 40, Color.gray); memRecBut = buttonDetails("MRC", "memRecBut", 40, 40, Color.blue); memPlusBut = buttonDetails("M+", "memPlusBut", 40, 40, Color.blue); memMinusBut = buttonDetails("M-", "memMinusBut", 40, 40, Color.blue); divideBut = buttonDetails("/", "divideBut", 40, 40, Color.gray); percentageBut = buttonDetails("%", "percentageBut",40, 40, Color.gray); sevenBut = buttonDetails("7", "sevenBut", 40, 40, Color.gray); eightBut = buttonDetails("8", "eightBut", 40, 40, Color.gray); nineBut = buttonDetails("9", "nineBut", 40, 40, Color.gray); timesBut = buttonDetails("X", "timesBut", 40, 40, Color.gray); sqrRootBut = buttonDetails("Sqr", "sqrRootBut", 40, 40, Color.gray); fourBut = buttonDetails("4", "fourBut", 40, 40, Color.gray); fiveBut = buttonDetails("5", "fiveBut", 40, 40, Color.gray); sixBut = buttonDetails("6", "sixBut", 40, 40, Color.gray); minusBut = buttonDetails("-", "minusBut", 40, 40, Color.gray); clearBut = buttonDetails("C", "clearBut", 40, 40, Color.red); oneBut = buttonDetails("1", "oneBut", 40, 40, Color.gray); twoBut = buttonDetails("2", "twoBut", 40, 40, Color.gray); threeBut = buttonDetails("3", "threeBut", 40, 40, Color.gray); plusBut = buttonDetails("+", "plusBut", 40, 40, Color.gray); allClearBut = buttonDetails("AC", "allClearBut", 40, 40, Color.red); zeroBut = buttonDetails("0", "zeroBut", 40, 40, Color.gray); pointBut = buttonDetails(".", "pointBut", 40, 40, Color.gray); equalsBut = buttonDetails("=", "equalsBut", 40, 40, Color.gray); infoBut = buttonDetails("?", "infoBut", 40, 40, Color.gray); //add the buttons to the buttonPanel using the grid layout buttonPanel.add(powerBut); buttonPanel.add(memRecBut); buttonPanel.add(memPlusBut); buttonPanel.add(memMinusBut); buttonPanel.add(divideBut); buttonPanel.add(percentageBut); buttonPanel.add(sevenBut); buttonPanel.add(eightBut); buttonPanel.add(nineBut); buttonPanel.add(timesBut); buttonPanel.add(sqrRootBut); buttonPanel.add(fourBut); buttonPanel.add(fiveBut); buttonPanel.add(sixBut); buttonPanel.add(minusBut); buttonPanel.add(clearBut); buttonPanel.add(oneBut); buttonPanel.add(twoBut); buttonPanel.add(threeBut); buttonPanel.add(plusBut); buttonPanel.add(allClearBut); buttonPanel.add(zeroBut); buttonPanel.add(pointBut); buttonPanel.add(equalsBut); buttonPanel.add(infoBut); repaint(); } //Method that setups the displayPanel and ledDisplay // //Parameters: Nil //Return Type: Void // private void showLedDisplay() { displayPanel.setSize(242,145);//set the size ofthe displayPanel displayPanel.setBackground(Color.black);//set the background color displayPanel.add(ledDisplay);//add the ledDisplay to the displayPanel ledDisplay.setSize(235,75);//set the size of the ledDisplay ledDisplay.setText(ledString);// ledDisplay.setEditable(false);//lock any user input from text field ledDisplay.setBackground(Color.lightGray);//set background color ledDisplay.setForeground(Color.black);//set the foreground color ledDisplay.setFont(new Font("",5,22));//set the font and font size ledDisplay.setHorizontalAlignment(JTextField.RIGHT);//ledDisplay text alignment ledDisplay.setBorder(new CompoundBorder(new MatteBorder(12,12,12,12,Color.darkGray), new BevelBorder(1))); } //Method that gets called once for every button to be created. Its purpose is to //assign values to the button // //Parameters: String text - The text to be displayed on the button // String name - The button name used as an action command // int Width - The width of the button // int Height - The height of the button // Color bColor- The backround color of the button // //Return Type: JButton(b1) - Returns the button with the added details // public JButton buttonDetails(String text, String name, int Width, int Height, Color bColor) { JButton b1 = new JButton(text);//create new JButton called b1 b1.setActionCommand(name);// b1.setPreferredSize(new Dimension(Width,Height));//set the button size b1.setBorder(new CompoundBorder(new MatteBorder(4,4,4,4,Color.black), new BevelBorder(1)));//create border around button b1.setDebugGraphicsOptions(DebugGraphics.NONE_OPTION); b1.setBackground(bColor);//set the background color b1.setForeground(Color.white);//set the foreground color b1.setFont(new Font("",5,16));//set the fone and font size b1.addActionListener((ActionListener)this);//add an action listener to button return b1;//return the button with the details added } //Method that listens for any mouse clicks on the buttons // //Parameter: ActionEvent event - The event performed //Return Type: Void // public void actionPerformed(ActionEvent event) { String command = event.getActionCommand();//variable to store button command //The number buttons, calls displaySelection method everytime clicked if(command.equals("oneBut")) { displaySelection("1"); } if(command.equals("twoBut")) { displaySelection("2"); } if(command.equals("threeBut")) { displaySelection("3"); } if(command.equals("fourBut")) { displaySelection("4"); } if(command.equals("fiveBut")) { displaySelection("5"); } if(command.equals("sixBut")) { displaySelection("6"); } if(command.equals("sevenBut")) { displaySelection("7"); } if(command.equals("eightBut")) { displaySelection("8"); } if(command.equals("nineBut")) { displaySelection("9"); } if(command.equals("zeroBut")) { //check if ledString is empty or already contains an "0" //if it does return else calls displaySelection method and adds the 0 if(ledString.equals("0") || ledString.equals("")) return; else displaySelection("0"); } //Point button pressed if(command.equals("pointBut")) { if( ledString.equals("") || ledString.equals(".")) return; //if ledDisplay is empty or is a point return //if ledDisplay equals 0 then call displaySelection method and add 0. else //add . if( ledString.equals("0")) displaySelection("0."); else displaySelection("."); } //the arithmetic buttons if(command.equals("plusBut")) { firstString = ledString;//store ledString value in firstString variable firstNumber = Double.parseDouble(firstString);//convert String to Double calculatedNumber = myCalc.addition(firstNumber);//call addition method calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = "0";//set the variable ledString numberDigits = 0;//reset the number of digits to 0 ledDisplay.setText(calculatedString);//display result of sum in ledDisplay selection = 1;//set selection variable numberDigits = 0;//reset number of digits to 0 } if(command.equals("minusBut")) { firstString = ledString;//store ledString value in firstString variable firstNumber = Double.parseDouble(firstString);//convert String to Double calculatedNumber = myCalc.subtract(firstNumber);//call subtract method calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = "0";//set the variable ledString numberDigits = 0;//reset the number of digits to 0 ledDisplay.setText(calculatedString);//display result of sum in ledDisplay selection = 2;//set selection variable numberDigits = 0;//reset number of digits to 0 } if(command.equals("timesBut")) { firstString = ledString;//store ledString value in firstString variable firstNumber = Double.parseDouble(firstString);//convert String to Double calculatedNumber = myCalc.multiply(firstNumber);//call multiply method calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = "0";//set the variable ledString numberDigits = 0;//reset the number of digits to 0 ledDisplay.setText(calculatedString);//display result of sum in ledDisplay selection = 3;//set selection variable numberDigits = 0;//reset number of digits to 0 } if(command.equals("divideBut")) { firstString = ledString;//store ledString value in firstString variable firstNumber = Double.parseDouble(firstString);//convert String to Double calculatedNumber = myCalc.divide(firstNumber);//call divide method calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = "0";//set the variable ledString numberDigits = 0;//reset the number of digits to 0 ledDisplay.setText(calculatedString);//display result of sum in ledDisplay selection = 4;//set selection variable numberDigits = 0;//reset number of digits to 0 } //thew equals button if(command.equals("equalsBut")) { if(ledString.equals("0")) return;//only continue if two numbers selected ledNumber = Double.parseDouble(ledString);//convert string to double //depnding on value of selection call the appropriate method if(selection==1) calculatedNumber = myCalc.addition(myCalc.buffer, ledNumber); if(selection==2) calculatedNumber = myCalc.subtract(myCalc.buffer, ledNumber); if(selection==3) calculatedNumber = myCalc.multiply(myCalc.buffer, ledNumber); if(selection==4) calculatedNumber = myCalc.divide(myCalc.buffer, ledNumber); if(selection==5) calculatedNumber = myCalc.pow(myCalc.buffer, ledNumber); calculatedString = Double.toString(calculatedNumber);//convert double to string ledString = "0";//set the variable ledString ledDisplay.setText(calculatedString);//display result of sum in ledDisplay } //the specialist buttons if(command.equals("powerBut")) { firstString = ledString;//store ledString value in firstString variable firstNumber = Double.parseDouble(firstString);//convert String to Double calculatedNumber = myCalc.power(firstNumber);//call power method String calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = "0";//set the variable ledString ledDisplay.setText(calculatedString);//display result of sum in ledDisplay selection = 5;//set selection variable numberDigits = 0;//reset number of digits to 0 } if(command.equals("sqrRootBut")) { secondString = ledString;//store ledString value in secondString variable secondNumber = Double.parseDouble(secondString);//convert String to Double calculatedNumber = myCalc.squareRoot(secondNumber);//call squareRoot method calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = calculatedString;//assign result of sum to ledString ledDisplay.setText(ledString);//display result of sum in ledDisplay numberDigits = 0;//reset number of digits to 0 } if(command.equals("percentageBut")) { if(ledString.equals("0")) return;//dont allow percentage of 0 ledNumber = Double.parseDouble(ledString);//convert String to Double calculatedNumber = myCalc.percentage(myCalc.buffer,ledNumber);//call percentage method calculatedString = Double.toString(calculatedNumber);//convert Double to String ledString = "0";//set the variable ledString ledDisplay.setText(calculatedString);//display result of sum in ledDisplay selection = 3;//set selection variable numberDigits = 0;//reset number of digits to 0 } //The clear buttons if(command.equals("clearBut")) { ledDisplay.setText("0");//set ledDisplay to 0 ledString = "0";//set ledString to 0 numberDigits = 0;//set the number of digits to 0 } if(command.equals("allClearBut")) { firstString = "";//clear the contents of firstString secondString = "";//clear the contents of secondString ledDisplay.setText("0");//set ledDisplay to 0 ledString = "0";//set ledString to 0 numberDigits = 0;//set the number of digits to 0 myCalc.buffer = 0;//set the buffer to 0 } //The memory buttons if(command.equals("memRecBut")) { ledString = memory;//assign the value of memory to ledString ledDisplay.setText(ledString);//display ledString on ledDisplay } if(command.equals("memPlusBut")) memory = ledString;//store vale in memory if(command.equals("memMinusBut")) memory = "";//clear memory variable if(command.equals("infoBut")) { //display message box JOptionPane.showMessageDialog(null, "David Mann - 0203401"); repaint(); } } //Method that takes a string variable and adds it to the ledDisplay // //Parameters: String numberToBeAdded - the button text 0-9 and . //Return Type: Void // public void displaySelection(String numberToBeAddedToLed) { if(numberDigits>16) return;//limit the amount of digits allowed if(ledString.equals("0")) ledString = "";//if ledString equals 0 clear ledString ledString = ledString + numberToBeAddedToLed;//add number string to ledString ledDisplay.setText(ledString);//display ledString on the ledDisplay numberDigits++;//increment the number of digits by 1 } //Paint method // //Parameters: Graphics g - a graphics object //Return Type: Void // public void paint(Graphics g) { repaint();//repaint the applet displayPanel.repaint();//repaint the displayPanel ledDisplay.repaint();//repaint the ledDisplay buttonPanel.repaint();//repaint the buttonPanel } }