Code Java Kalkulator Fisika

/** ***************************************************************************
* The BasicCalc_II class implements a simple Calculator application.
* MyCalc uses an operator code to keep track of the operation being
* performed
* – 0 = multiplication
* – 1 = division
* – 2 = subtraction
* – 3 = addition
* – 4 = raise a number to the power of the second number
* – 5 = the result of the operator acting on the two numbers
* as a result of the equals button being pressed
*


*
Copyright (c) 2002-2012 Advanced Applications Total Applications Works.
* (AATAW) all Rights Reserved.
*
* AATAW grants you (“licensee”) a non-exclusive, royalty free, license to use,
* modify and redistribute this software in source and binary code form,
* provided that i) this copyright notice and license appear on all copies of
* the software; and ii) licensee does not utilize the software in a manner
* which is disparaging to AATAW.
*
* This software is provided “as IS,” without a warranty of any kind. all
* express or implied conditions, representations and warranties, including any
* implied warranty of merchantability, fitness for a particular purpose or
* non-infringement, are hereby excluded. AATAW and its licensors shall not be
* liable for any damages suffered by licensee as a result of using, modifying
* or distributing the software or its derivatives. in no event will AATAW or its
* licensors be liable for any lost revenue, profit or data, or for direct,
* indirect, special, consequential, incidental or punitive damages, however
* caused and regardless of the theory or liability arisinG out of the use of
* or inability TO use software, even if AATAW Has been advised of the
* possibility of such damages.
*
* This software is not designed or intended for use in on-line control of
* aircraft, air traffic, aircraft navigation or aircraft communications; or in
* the design, construction, operation or maintenance of any nuclear
* facility. licensee represents and warrants that it will not use or
* redistribute the Software for such purposes.
*
*
*
********************************************************************************/

import java.awt.*;
import java.lang.Math ;
import java.awt.event.*;
import javax.swing.* ;
import javax.swing.JOptionPane ;
import java.io.*;
import javax.swing.border.Border;
import javax.swing.border.* ;

 

/** *****************************************************
* The BasicCalc_III class
*
********************************************************/
public class BasicCalc_III extends JFrame
implements ActionListener {

private JButton bOne, bTwo, bThree, bFour, bFive,
bSix, bSeven, bEight, bNine, bZero, bExit,
bMult, bDiv, bSub, bPlus, bCEnt, bCAll, bEquals,
bPlusMinus, bExp, bMPlus, bMMinus, bHelp,
bFact, bAbout, bOneDivX, bSqrt, bDecPt, bLog,
bCos, bSin, bTan, bACos, bASin, bATan, bALog ;
private JTextField enterBox ;
private JPanel textPanel, exitPanel, buttonPanel, functionPanel ;
private Container c ;
private Font font;
private String firstNum = new String(“”), secondNum = new String(“”), tempNum, tempStr ;
private boolean myDiag = false, result = true ,
tempfirstNumSet = false, firstNumSet = true, secondNumSet = false,
pendingOp = false ;
private double aNum, dNum1 = 0.0, dNum2 = 0.0 , answer = 0.0,
tempD = 0.0 , minusOne = -1.0 ;
private double dArray[] = new double[ 10 ] ;
private Border raisedBorder = BorderFactory.createRaisedBevelBorder() ;
private Border raisedBorder2 = BorderFactory.createRaisedBevelBorder() ;
private int opCode = -1, tempInt = 0, tempInt2 = 0,
dArrayPtr = 0, pendingOpCode = -1 ;

/** *****************************************************
* BasicCalc_II() – constructor used for initialization
* 1- set Title of this app
* 2- Instantiate LogoWindowFrame
* 3- Call setup() method
* 4- Call etLocation() method
* 5- Call setup() method
* 6- Call setVisible() method
* ******************************************************/
BasicCalc_III() {

super( “Basic Calculator” );

LogoWindowFrame sqf = new LogoWindowFrame() ;

setup() ;

setSize( 350, 300 ); /** sets the size of the frame */
setLocation( 200, 200 ) ; /** sets the location of the frame on the screen*/
setVisible( true ); /** makes the frame visible */
}

/** *****************************************************
* The setup() method is used to allocate the panel and
* button objects using the new function.
* 1- Instantiate a JTextField object
* a- Initiate the textfield with a “0”
* b- Insure that the textfield is not editable
* c- Set the textfield background color to “white”
* d- Set the textfield Horizontal alignment to “right”
* e- All an event listner to the textfield
* 2- Add an JPanel
* 3- Add the JTextField to the JPanel
* 4- Add the JPanel to the North section
* 5- Call setUpButtons() to create the JButtons
* 6- Call etUpActionListener() method to set up the event
* listner responses
* 7- Add the buttons to the buttonPanel
* 8- Call setUpFunctionPanel()
* 9- Call setUpExitPanel()
*******************************************************/
public void setup() {
c = getContentPane() ;
/** setup text field and panel */
enterBox = new JTextField( 15 ) ;
enterBox.setText( “0” );
enterBox.setEditable( false );
enterBox.setBackground( Color.white );
enterBox.setHorizontalAlignment( JTextField.RIGHT );
enterBox.addKeyListener(
/** this code reprimes the enterbox and avoids exceptions */
new KeyAdapter() {
public void keyPressed ( KeyEvent e ) {
if ( result ) {
MyPrint( “The value of result is ” + result +
“this is from the enterbox keylistener.” );
result = false ;
enterBox.setText( “” );
}
}
}
);
textPanel = new JPanel() ;
textPanel.add(enterBox ) ;

c.add( textPanel , BorderLayout.NORTH ) ;

setUpButtons() ;

setUpActionListener() ;

/** add the following buttons to the buttonPanel panel. */

buttonPanel.add( bLog ) ;
buttonPanel.add( bCos ) ;
buttonPanel.add( bSin ) ;
buttonPanel.add( bTan ) ;

buttonPanel.add( bALog ) ;
buttonPanel.add( bACos ) ;
buttonPanel.add( bASin ) ;
buttonPanel.add( bATan ) ;

buttonPanel.add( bSeven ) ;
buttonPanel.add( bEight ) ;
buttonPanel.add( bNine ) ;
buttonPanel.add( bMult ) ;

buttonPanel.add( bFour ) ;
buttonPanel.add( bFive ) ;
buttonPanel.add( bSix ) ;
buttonPanel.add( bDiv ) ;

buttonPanel.add( bOne ) ;
buttonPanel.add( bTwo ) ;
buttonPanel.add( bThree ) ;
buttonPanel.add( bSub ) ;

buttonPanel.add( bPlusMinus ) ;
buttonPanel.add( bZero ) ;
buttonPanel.add( bDecPt ) ;
buttonPanel.add( bPlus ) ;

buttonPanel.add( bExp );
buttonPanel.add( bOneDivX );
buttonPanel.add( bSqrt );
buttonPanel.add( bEquals ) ;

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

setUpFunctionPanel() ;

setUpExitPanel() ;
}

/** *******************************************************
* The setUpButtons() method sets up button Panel and the
* number buttons. The logic is:
* 1- Create the 0 – 9, +, -, /, *, Exit, POW, SQRT, and
* decimal buttons.
* 2- Set the font type and size of the font for some of the
* buttons.
**********************************************************/
public void setUpButtons() {
buttonPanel = new JPanel() ;
c.add( buttonPanel , BorderLayout.CENTER ) ;

bZero = new JButton( “0” ) ;
bZero.setBorder( raisedBorder ) ;
bZero.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bOne = new JButton( “1” ) ;
bOne.setBorder( raisedBorder ) ;
bTwo = new JButton( “2” ) ;
bTwo.setBorder( raisedBorder ) ;
bThree = new JButton( “3” ) ;
bThree.setBorder( raisedBorder ) ;
bFour = new JButton( “4” ) ;
bFour.setBorder( raisedBorder ) ;
bFive = new JButton( “5” ) ;
bFive.setBorder( raisedBorder ) ;
bSix = new JButton( “6” ) ;
bSix.setBorder( raisedBorder ) ;
bSeven = new JButton( “7” ) ;
bSeven.setBorder( raisedBorder ) ;
bEight = new JButton( “8” ) ;
bEight.setBorder( raisedBorder ) ;
bNine = new JButton( “9” ) ;
bNine.setBorder( raisedBorder ) ;
bExit = new JButton( “Exit” ) ;
bExit.setBorder( raisedBorder ) ;
bMult = new JButton( “*” ) ;
bMult.setFont( new Font(“Sanserif”, Font.BOLD, 20 ) );
bMult.setBorder( raisedBorder ) ;
bDiv = new JButton( “/” ) ;
bDiv.setFont( new Font(“Sanserif”, Font.BOLD, 20 ) );
bDiv.setBorder( raisedBorder ) ;
bSub = new JButton( “-” ) ;
bSub.setFont( new Font(“Sanserif”, Font.BOLD, 24 ) );
bSub.setBorder( raisedBorder ) ;
bPlus = new JButton( “+” ) ;
bPlus.setBorder( raisedBorder ) ;
bPlusMinus = new JButton( “+/-” ) ;
bPlusMinus.setBorder( raisedBorder ) ;
bPlusMinus.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bEquals = new JButton( “=” ) ;
bEquals.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bEquals.setBorder( raisedBorder ) ;
bPlus = new JButton( “+” ) ;
bPlus.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bPlus.setBorder( raisedBorder ) ;
bExp = new JButton( “Pow” ) ;
bExp.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bExp.setBorder( raisedBorder ) ;
bOneDivX = new JButton( “1/x” ) ;
bOneDivX.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bOneDivX.setBorder( raisedBorder ) ;
bSqrt = new JButton( “SQRT” ) ;
bSqrt.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bSqrt.setBorder( raisedBorder ) ;
bDecPt = new JButton( “.” ) ;
bDecPt.setFont( new Font(“Sanserif”, Font.BOLD, 22 ) );
bDecPt.setBorder( raisedBorder ) ;
bLog = new JButton( “Log” ) ;
bLog.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bCos = new JButton( “Cos” ) ;
bCos.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bSin = new JButton( “Sin” ) ;
bSin.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bTan = new JButton( “Tan” ) ;
bTan.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bACos = new JButton( “ACos” ) ;
bACos.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bASin = new JButton( “ASin” ) ;
bASin.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bATan = new JButton( “ATan” ) ;
bATan.setFont( new Font(“Sanserif”, Font.BOLD, 12 ) );
bALog = new JButton( “ALog” ) ;

}
/** ***** End of setUpButtons() ****************/

/** *********************************************************
* The setUpFunctionPanel method assigns an event handler to
* the buttons and indicates that the responses will be handled
* inside this class. Add the 0 – 9, Exit, Multiplication,
* Division, Subtraction, Addition, PlusMinus, Equals, Power,
* OneDivX, Sqrt, and Decimal Point to this application’s
* action listner.
* ************************************************************/
public void setUpActionListener() {

bZero.addActionListener( this );
bOne.addActionListener( this );
bTwo.addActionListener( this );
bThree.addActionListener( this );
bFour.addActionListener( this );
bFive.addActionListener( this );
bSix.addActionListener( this );
bSeven.addActionListener( this );
bEight.addActionListener( this );
bNine.addActionListener( this );
bExit.addActionListener( this );
bMult.addActionListener( this );
bDiv.addActionListener( this );
bSub.addActionListener( this );
bPlus.addActionListener( this );
bPlusMinus.addActionListener( this );
bEquals.addActionListener( this );
bExp.addActionListener( this );
bOneDivX.addActionListener( this );
bSqrt.addActionListener( this );
bDecPt.addActionListener( this );

bALog.addActionListener( this );
bLog.addActionListener( this );
bCos.addActionListener( this );
bSin.addActionListener( this );
bTan.addActionListener( this );
bACos.addActionListener( this );
bASin.addActionListener( this );
bATan.addActionListener( this );
}

/** *********************************************************
* The setUpFunctionPanel method sets up the function panel and
* its buttons. The logic:
* 1- Create the function panel and add it to the East border
* location.
* 2- Create the Clear Entry, Clear All, M+ (Add to memory),
* M- (retrieve from memory), and !n (factorial buttons
* 3- Add the above buttons to the function Panel.
* 4- Add background and foregruond colors to the above buttons.
* 5- Add the above buttons to this class’ action listner.
*
* ************************************************************/
public void setUpFunctionPanel() {
/** setup function panel and its buttons*/
functionPanel = new JPanel() ;

functionPanel.setLayout( new GridLayout( 6, 2, 5, 3 ) );
functionPanel.setSize( 400 , 400 );

bCEnt = new JButton( ” CE ” ) ;
bCEnt.setFont( new Font(“Sanserif”, Font.BOLD, 14 ) );
bCEnt.setBackground( Color.blue ) ;
bCEnt.setForeground( Color.white );
bCEnt.setBorder( raisedBorder2 ) ;
bCAll = new JButton( ” CA ” ) ;
bCAll.setFont( new Font(“Sanserif”, Font.BOLD, 14 ) );
bCAll.setBackground( Color.blue ) ;
bCAll.setForeground( Color.white );
bCAll.setBorder( raisedBorder2 ) ;
bMPlus = new JButton( ” M+ ” ) ;
bMPlus.setFont( new Font(“Sanserif”, Font.BOLD, 14 ) );
bMPlus.setBackground( Color.blue ) ;
bMPlus.setForeground( Color.white );
bMPlus.setBorder( raisedBorder2 ) ;
bMMinus = new JButton( ” M- ” ) ;
bMMinus.setFont( new Font(“Sanserif”, Font.BOLD, 14 ) );
bMMinus.setBackground( Color.blue ) ;
bMMinus.setForeground( Color.white );
bMMinus.setBorder( raisedBorder2 ) ;
bFact = new JButton( ” n! ” ) ;
bFact.setFont( new Font(“Sanserif”, Font.BOLD, 20 ) );
bFact.setBorder( raisedBorder2 ) ;
bFact.setBackground( Color.blue ) ;
bFact.setForeground( Color.white );
bFact.setBorderPainted( true );
/** add the following buttons to the functionPanel panel. */
functionPanel.add( bCEnt ) ;
functionPanel.add( bCAll );
functionPanel.add( bMPlus );
functionPanel.add( bMMinus );
functionPanel.add( bFact );

bCEnt.addActionListener( this );
bCAll.addActionListener( this );
bMPlus.addActionListener( this );
bMMinus.addActionListener( this );
bFact.addActionListener( this );

c.add( functionPanel , BorderLayout.EAST ) ;
}
/** ***** End of setUpFunctionPanel() ****************/

/** *********************************************************
* The setUpExitPanel() is the method that is called to setup
* exit panel and its buttons. The logic:
* 1- Create the exit panel.
* 2- Add the exitPanel to South border location.
* 3- Create the Exit, Help, About button.
* 4- Create the background and foreground colors for the above
* buttons.
* 5- Create the fonts and size of these fonts for the above
* buttons.
* 6- Add the above buttons to the exit panels.
* 7- Add the above buttons to this class’ action listner.
* ************************************************************/
public void setUpExitPanel() {
/** setup exit panel and its buttons */
exitPanel = new JPanel() ;
c.add( exitPanel , BorderLayout.SOUTH ) ;
bExit = new JButton( “Exit” ) ;
bExit.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bExit.setBackground( Color.red ) ;
bExit.setForeground( Color.white );
bHelp = new JButton( “Help” ) ;
bHelp.setFont( new Font(“Sanserif”, Font.BOLD, 16 ) );
bHelp.setBackground( Color.blue ) ;
bHelp.setForeground( Color.white );
bAbout = new JButton( “About” ) ;
bAbout.setFont( new Font(“Sanserif”, Font.BOLD, 14 ) );
bAbout.setBackground( Color.darkGray ) ;
bAbout.setForeground( Color.white );

/** add the following buttons to the exitPanel panel. */
exitPanel.add( bExit ) ;
exitPanel.add( bHelp ) ;
exitPanel.add( bAbout ) ;

bExit.addActionListener( this );
bHelp.addActionListener( this );
bAbout.addActionListener( this );
}
/** ***** End of setUpExitPanel() ****************/

/** *********************************************************
* This is the method that is called to respond to a button
* event
* ************************************************************/
public void actionPerformed( ActionEvent e ) {

tempNum = “” + answer ;

if ( e.getSource() == bPlus || e.getSource() == bSub ||
e.getSource() == bMult || e.getSource() == bDiv ) {
tempNum = “” + answer ;

MyPrint( “actionPerformed() 1a: The value of result is ” + result +
” The value of tempNum is ” + enterBox.getText() +
” The value of opCode is ” + opCode ) ;
}

if ( result ) {
/** If result =s true, set the text field to blank */
MyPrint( “actionPerformed() 1b: The value of result is ” + result +
“The value of tempNum is ” + enterBox.getText()
);
result = false ;
enterBox.setText( “” );
}

if ( !pendingOp && opCode == 99 ) {
/** If opCode =s 99, then the =s button was pressed */
MyPrint( “actionPerformed() 1c: The value of opCode is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of tempNum is ” + tempNum );
pendingOpCode = -1 ;
//opCode = -1 ;
firstNumSet = true ;
firstNum = tempNum ;
enterBox.setText( “” );
}

if ( e.getSource() == bDecPt ) {
MyPrint( “The decimal point button was pressed.” );
enterBox.setText( enterBox.getText() + “.” ) ;
}
else if ( e.getSource() == bZero ) {
MyPrint( “The zero button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “0” ) ;
}
else if ( e.getSource() == bOne ) {
MyPrint( “The one button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “1” ) ;
}
else if ( e.getSource() == bTwo ) {
MyPrint( “The two button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “2” ) ;
}
else if ( e.getSource() == bThree ) {
MyPrint( “The Three button was pressed. And pendingOp is ” + pendingOp);
enterBox.setText( enterBox.getText() + “3” ) ;
}
else if ( e.getSource() == bFour ) {
MyPrint( “The Four button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “4” ) ;
}
else if ( e.getSource() == bFive ) {
MyPrint( “The Five button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “5” ) ;
}
else if ( e.getSource() == bSix ) {
MyPrint( “The Six button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “6” ) ;
}
else if ( e.getSource() == bSeven ) {
MyPrint( “The Seven button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “7” ) ;
}
else if ( e.getSource() == bEight ) {
MyPrint( “The Eight button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “8” ) ;
}
else if ( e.getSource() == bNine ) {
MyPrint( “The Nine button was pressed. And pendingOp is ” + pendingOp );
enterBox.setText( enterBox.getText() + “9” ) ;
}
else if ( e.getSource() == bExit ) {
MyPrint( “The Exit button was pressed.” );
sysExit() ;
}
else if ( e.getSource() == bMult ) {
MyPrint( “The Mult button was pressed. And pendingOp is ” + pendingOp );
multOp() ;
}
else if ( e.getSource() == bDiv ) {
MyPrint( “The Div button was pressed. And pendingOp is ” + pendingOp );
divOp() ;
}
else if ( e.getSource() == bSub ) {
MyPrint( “The Sub button was pressed. And pendingOp is ” + pendingOp );
subOp() ;
}
else if ( e.getSource() == bPlus ) {
MyPrint( “The Plus button was pressed. And pendingOp is ” + pendingOp );
plusOp() ;
}
else if ( e.getSource() == bCEnt ) {
MyPrint( “The CEnt button was pressed.” );
enterBox.setText( “” ) ;
secondNum = “” ;
dNum2 = 0.0 ;
}
else if ( e.getSource() == bSqrt ) {
MyPrint( “The bSqrt button was pressed. And pendingOp is ” + pendingOp );

MySqrt() ;
}
else if ( e.getSource() == bOneDivX ) {
MyPrint( “The bOneDivX button was pressed. And pendingOp is ” + pendingOp );
OneDivX() ;
}
else if ( e.getSource() == bCAll ) {
MyPrint( “The CAll button was pressed.” );
enterBox.setText( “” ) ;
firstNum = “” ;
firstNumSet = true ;
secondNum = “” ;
answer = 0.0 ;
dNum1 = 0.0 ;
dNum2 = 0.0 ;
opCode = -1 ;
pendingOpCode = -1 ;
pendingOp = false ;
}
else if ( e.getSource() == bEquals ) {
MyPrint( “The Equals button was pressed. And pendingOp is ” + pendingOp +
” The opCode value is ” + opCode );
MyPrint( “bEquals 1: The Equals button was pressed. And pendingOp is ” + pendingOp +
” The opCode value is ” + opCode );
MyEqual() ;
}
else if ( e.getSource() == bPlusMinus ) {
MyPrint( “The PlusMinus button was pressed.” );
opCode = -1 ;

PlusMinus() ;
}
else if ( e.getSource() == bExp ) {
MyPrint( “The Power button was pressed.” );
secondNumSet = false ;
powerOp() ;
}
else if ( e.getSource() == bMPlus ) {
MyPrint( “1a: The MPlus button was pressed. pendingOp =s ” + pendingOp +
” tempNum =s ” + tempNum );

if ( enterBox.getText().equals( “” ) && tempNum.equals( “” )) {
JOptionPane.showMessageDialog( this, “A number must be entered befor clicking the M+ button” ,
“Attempted to store a blank number.”,
JOptionPane.ERROR_MESSAGE );
}
else if ( dArrayPtr < 10 ) { /** array hold 10 entries */
if ( !pendingOp ) {
if ( opCode == 99 ) {
//enterBox.setText( “” );
MyPrint( “1b MPlus: The value of opCode is ” + opCode +
” firstNum =s ” + firstNum + ” answer =s ” + answer );
pendingOpCode = -1 ;
firstNumSet = true ;
firstNum = tempNum ;
//firstNum = “” + enterBox.getText() ;
dArray [ dArrayPtr ] =
Double.parseDouble( “” + firstNum ) ;
dArrayPtr++ ;
enterBox.setText( firstNum ) ;
MyPrint( “1c MPlus: The value of dArrayPtr is ” + dArrayPtr );
}
else {
/** This code is entered if the =s button was not just pressed */
firstNum = “” + enterBox.getText( );
if( firstNum.equals( “” ) ) {
firstNum = “0.0” ;
}

dArray [ dArrayPtr ] =
Double.parseDouble( firstNum ) ;
firstNumSet = true ;
dArrayPtr++ ;
MyPrint( “1d: The value of dArrayPtr is ” + dArrayPtr );
}
}
else if ( !enterBox.getText().equals( “” ) ) {
dArray [ dArrayPtr ] =
Double.parseDouble( enterBox.getText() ) ;
dArrayPtr++ ;
firstNumSet = true ;
firstNum = “” + answer ;
MyPrint( “1d: The value of dArrayPtr is ” + dArrayPtr );
}
else {
JOptionPane.showMessageDialog( this, “A number must be entered befor clicking the M+ button” ,
“Attempted to store a blank number.”,
JOptionPane.ERROR_MESSAGE );
}
}
result = true ;
}
else if ( e.getSource() == bMMinus ) {
/** This code retrieves values from the array stack */
MyPrint( “The MMinus button was pressed.” ) ;
MyMinus() ;
}
else if ( e.getSource() == bLog ) {
MyPrint( “The Log button was pressed.” );
MyLog() ;
}
else if ( e.getSource() == bALog ) {
MyPrint( “The Log button was pressed.” );
MyALog() ;
}
else if ( e.getSource() == bCos ) {
MyPrint( “The Cosine button was pressed.” );
MyCos() ;
}
else if ( e.getSource() == bSin ) {
MyPrint( “The Sine button was pressed.” );
MySin() ;
}
else if ( e.getSource() == bTan ) {
MyPrint( “The Tangent button was pressed.” );
MyTan() ;
}
else if ( e.getSource() == bACos ) {
MyPrint( “The Arc Cosine button was pressed.” );
MyACos() ;
}
else if ( e.getSource() == bASin ) {
MyPrint( “The Arc Sine button was pressed.” );
MyASin() ;
}
else if ( e.getSource() == bATan ) {
MyPrint( “The Arc Tangent button was pressed.” );
MyATan() ;
}
else if ( e.getSource() == bHelp ) {
MyPrint( “The Help button was pressed.” );
File hd = new File(“BasicCalc_II_help_doc.html”);
//File net = new File(“Netscp.exe”);
String helpStr = hd.getAbsolutePath() ;
Runtime rt = Runtime.getRuntime();
/* String[] callAndArgs = { “c:\\Program Files\\Internet Explorer\\IExplore.exe” ,
“” +
E:\MyExamples\SimpleCalc\Calc\help_doc.html }; */
String[] callAndArgs = { “c:\\Program Files\\Internet Explorer\\IExplore.exe” ,
“” + helpStr };

try {

Process child = rt.exec( callAndArgs );
child.waitFor();
MyPrint (“Process exit code is: ” +
child.exitValue());
}
catch(IOException e2) {
MyPrint (
“IOException starting process!”);
}
catch(InterruptedException e3) {
System.err.println(
“Interrupted waiting for process!”);
}
}
else if ( e.getSource() == bFact ) {
MyPrint( “1a- bFact(): opCode is ” + opCode + ” firstNum is ” + firstNum ) ;

MyFact() ;
}
else if ( e.getSource() == bAbout ) {
MyPrint( “The About button was pressed.” );
Runtime rt = Runtime.getRuntime();
String[] callAndArgs = { “c:\\Program Files\\Internet Explorer\\IExplore.exe” ,
http://sumtotalz.com/TotalAppsWorks/index.html ” };
try {
Process child = rt.exec(callAndArgs);
child.waitFor();
MyPrint (“Process exit code is: ” +
child.exitValue());
}
catch(IOException e2) {
MyPrint( “IOException starting process!”);
}
catch(InterruptedException e3) {
MyPrint( “Interrupted waiting for process!”);
}
}

} /** ***** End of actionPerformed() ****************/

/** **********************************************************
* setResults() – enters the result of an operation
* 1- In the textbox, firstNum and dNum1
* 2- Sets the indicator that the first number is set
* 3- Sets the indicator that a result has been reached
* 4- Indicates that there is no operation pending.
* ***********************************************************/
public void setResults() {
MyPrint( “setResults() 1: answer is ” + answer + ” dNum1 is ” + dNum1 );

enterBox.setText( “” + answer );
firstNum = “” + answer ;
secondNum = “” ;
dNum1 = answer;
dNum2 = 0.0 ;
firstNumSet = true ;
result = true ;
pendingOp = false ;
}
/** ***** End of setResults() ****************/

/** ************************************************************
* opCodeMethod() – determines if there is a pending op
* to perform
* – if so, that operation is performed
* – if not, the current operation is performed
* ************************************************************/
public void opCodeMethod() {
int currentOpCode = -1 , tempOpCode = opCode ;
boolean temppendingOp = pendingOp ;

MyPrint( “opCodeMethod() 1: with an opCode of ” + opCode );

/** Check if there is a pending operation that must
* be performed before the current operation is
* performed. */
if ( pendingOp ) {
MyPrint( “opCodeMethod() 2: with an opCode of ” + opCode +
” and a pendingOpCode =s ” + pendingOpCode );
currentOpCode = pendingOpCode ;
pendingOp = false ;
}
else {
currentOpCode = opCode ;
MyPrint( “opCodeMethod() 2b: with an opCode of ” + opCode ) ;
}

switch ( currentOpCode ) {
case 0 :
multOp() ;
break ;
case 1 :
divOp() ;
break ;
case 2 :
MyPrint( “opCodeMethod() 3: opCode =s ” + currentOpCode ) ;
subOp() ;
break ;
case 3 :
MyPrint( “opCodeMethod() 4: opCode =s ” + currentOpCode ) ;
plusOp() ;
break ;
case 4 :
MyPrint( “opCodeMethod() 5: Calling PowerOp method.” );
MyPrint( “opCodeMethod() 6: the value of firstNum is ” +
firstNum + ” the value of secondNum is ” +
secondNum ) ;
secondNumSet = true ;
powerOp() ;
break ;
case 5 :
powerOp() ;
break ;
default :
MyPrint( “opCodeMethod() 7: default case is ” + currentOpCode );
setResults() ;
firstNumSet = true ;
secondNum = “0.0” ;
dNum2 = 0.0 ;
pendingOp = false ;
opCode = 99 ;
MyPrint( “opCodeMethod() 8: Case default: firstNum =s ” + firstNum);
break ;
}

MyPrint( “opCodeMethod() 9: pendingOpCode =s ” + pendingOpCode +
” pendingOp ” + pendingOp );

pendingOpCode = tempOpCode ;
pendingOp = temppendingOp ;

if ( !( opCode == 99 ) )
opCode = -1 ;

}
/** ********** End of opCodeMethod() **************/

/** ************************************************************
* MyEqual() – sets the entry box to the results of any pending operation.
* The logic is:
* 1- Check for pending operations other than multiplication
* 3- It must be multiplication operation, so get the first number
*
* **************************************************************/
public void MyEqual() {

MyPrint( “MyEqual() 1a: The opcode value is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of answer is ” + answer ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyEqual 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

setResults() ;

firstNum = “” + answer ;
secondNum = “” ;
enterBox.setText( firstNum ) ;
dNum1 = answer ;
dNum2 = 0.0 ;
opCode = -1 ;
pendingOpCode = -1 ;
pendingOp = false ;
}
/** ********** End of MyEqual() **************/

 

/** ************************************************************
* powerOp() – raises the first number to the power of the second.
* The logic is:
* 1- If the pending operation Code equals 0 then
* a) Get the second number
* b) Convert the numbers to double
* c) Raise the first number to the power of the second number.
* d) Indicate there are no pending operations
* 2- Check for pending operations other than multiplication
* 3- It must be multiplication operation, so get the first number
*
* **************************************************************/
public void powerOp() {

opCode = 5 ;

MyPrint( “powerOp() 1: The opcode value is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of answer is ” + answer ) ;

if ( pendingOpCode == 5 ) {
secondNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
convertNumsToDouble() ;
answer = Math.pow( dNum1 , dNum2 ) ;
pendingOp = false ;
setResults() ;
MyPrint( “powerOp 2: opCode is ” + opCode + ” The value of answer is ” + answer );
secondNum = “0.0” ;
opCode = -1 ;

}
else if ( pendingOpCode == 5 ) {
MyPrint( “powerOp 3: In pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}
else if ( opCode == 5 ) {
MyPrint( “powerOp 4: In pendingOp code. The opcode value is ” + opCode ) ;
firstNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
opCode = 5 ;
pendingOp = false ;
pendingOpCode = 5 ;
MyPrint( “powerOp 5: opCode is ” + opCode + ” The value of answer is ” + answer );
opCode = -1 ;
result = true ;
MyPrint( “powerOp 6: pendingOpCode is ” + pendingOpCode + ” The value of answer is ” + answer );
}
}
/** ********** End of powerOp() **************/

/** *****************************************************
* multOp() method multiplies two numbers. The logic is:
* 1- If the pending operation Code equals 0 then
* a) Get the second number
* b) Convert the numbers to double
* c) Multiply the numbers
* d) Indicate there are no pending operations
* e) Call setResults()
* 2- Check for pending operations other than multiplication
* 3- It must be multiplication operation, so get the first number
*
* Version 1.1
* Changed 9/10/2003 – Fixed chained operation errors
*********************************************************/
public void multOp() {
opCode = 0 ;

MyPrint( “multOp() 1a: The opcode value is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of answer is ” + answer ) ;

if ( pendingOpCode == 0 ) {
secondNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
convertNumsToDouble() ;
answer = dNum1 * dNum2 ;
pendingOp = false ;
setResults() ;
MyPrint( “multOp() 2: opCode is ” + opCode + ” The value of answer is ” + answer );
secondNum = “0.0” ;
opCode = -1 ;

}
else if ( pendingOpCode >= 1 ) {
MyPrint( “multOp() 2a: In pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}
else if ( opCode == 0 ) {
MyPrint( “multOp() 3: In pendingOp code. The opcode value is ” + opCode ) ;
firstNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
opCode = 0 ;
pendingOp = false ;
pendingOpCode = 0 ;
MyPrint( “multOp() 5: opCode is ” + opCode + ” The value of answer is ” + answer );
opCode = -1 ;
result = true ;
MyPrint( “multOp() 4: pendingOpCode is ” + pendingOpCode + ” The value of answer is ” + answer );
}
}
/******************************* End of multOp() ***************************/

/** ********************************************************************************
* divOp() – divides the two entered numbers and checks for a zero divisor
* 1- If the pending operation Code equals 1 then
* a) Get the second number
* b) Convert the numbers to double
* c) If the first number is zero, then the answer is zero
* d) If the second number is zero, then indicate that dividing by zero is not
* allowed
* e) Otherwise, multiply the numbers
* f) Indicate there are no pending operations
* g) Call setResults()
* 2- Check for pending operations other than divide
* 3- It must be divide operation, so get the first numberVersion 1.1
* Changed 9/10/2003 – Fixed chained operation errors
***********************************************************************************/
public void divOp() {

opCode = 1 ;
MyPrint( “divOp() 1a: opCode is ” + opCode + ” firstNumSet =s ” + firstNumSet +
” pendingOpCode =s ” + pendingOpCode );
opCode = 1 ;

MyPrint( “divOp() 1b: The opcode value is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of answer is ” + answer ) ;

if ( pendingOpCode == 1 ) {
secondNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
convertNumsToDouble() ;
if ( dNum1 == 0.0 ) {
answer = dNum1 ;
}
else if ( dNum2 == 0.0 ) {
answer = dNum1 ;
setResults() ;
JOptionPane.showMessageDialog( this, “The number ” +
dNum2 + ” cannot be used as a denometer.\n” +
“You cannot divide by zero.”,
“Attempted to Divide by Zero”,
JOptionPane.ERROR_MESSAGE );
}
else {
answer = dNum1 / dNum2 ;
}
pendingOp = false ;
setResults() ;
MyPrint( “divOp() 2: opCode is ” + opCode + ” The value of answer is ” + answer );
secondNum = “0.0” ;
opCode = -1 ;
}
else if ( pendingOpCode == 0 || pendingOpCode > 1 ) {
MyPrint( “divOp() 2a: In pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}
else if ( opCode == 1 ) {
MyPrint( “divOp() 3: In pendingOp code. The opcode value is ” + opCode ) ;
firstNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
opCode = 1 ;
pendingOp = false ;
pendingOpCode = 1 ;
opCode = -1 ;
result = true ;
MyPrint( “divOp() 4: pendingOpCode is ” + pendingOpCode +
” The value of answer is ” + answer );
}

}
/******************************* End of divOp() *******************************/

/** ********************************************************************
* subOp() – subtracts the second number from the first number The
* logic is:
* 1- If the pending operation Code equals 2 then
* a) Get the second number
* b) Convert the numbers to double
* c) Subtract the second number from the first number
* d) Indicate there are no pending operations
* e) Call setResults()
* 2- Check for pending operations other than subtraction
* 3- It must be subtraction operation, so get the first number
*
*
* Version 1.1
* Changed 9/10/2003 – Fixed chained operation errors
* *********************************************************************/
public void subOp() {
opCode = 2 ;

MyPrint( “subOp() 1a: The opcode value is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of answer is ” + answer +
” pendingOpCode =s ” + pendingOpCode ) ;

if ( pendingOpCode == 2 ) {
secondNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
convertNumsToDouble() ;
answer = dNum1 – dNum2 ;
pendingOp = false ;
setResults() ;
MyPrint( “subOp() 2: opCode is ” + opCode + ” The value of answer is ” + answer );
secondNum = “0.0” ;
opCode = -1 ;

}
else if ( pendingOpCode == 0 || pendingOpCode == 1 || pendingOpCode >= 3 ) {
MyPrint( “subOp() 2a: In pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}
else if ( opCode == 2 ) {
MyPrint( “subOp() 3: In pendingOp code. The opcode value is ” + opCode ) ;
firstNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
// convertNumsToDouble() ;
// answer = dNum1 – dNum2 ;
pendingOp = true ;
pendingOpCode = 2 ;
opCode = 2 ;
result = true ;
MyPrint( “subOp() 4: pendingOpCode is ” + pendingOpCode +
” The value of answer is ” + answer );
}
}
/********************************** End of subOp() *********************************/

/** *****************************************************
* plusOp() – adds the two entered numbers. The
* logic is:
* 1- If the pending operation Code equals 3 then
* a) Get the second number
* b) Convert the numbers to double
* c) Add the second number from the first number
* d) Indicate there are no pending operations
* e) Call setResults()
* 2- Check for pending operations other than addition
* 3- It must be addition operation, so get the first number
* Version 1.1
* Changed 9/10/2003 – Fixed chained operation errors
***********************************************************/
public void plusOp() {
opCode = 3 ;

MyPrint( “plusOp() 1a: The opcode value is ” + opCode +
” The value of firstNum is ” + firstNum +
” The value of answer is ” + answer ) ;

if ( pendingOpCode == 3 ) {
secondNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
convertNumsToDouble() ;
answer = dNum1 + dNum2 ;
pendingOp = false ;
setResults() ;
MyPrint( “plusOp() 2: opCode is ” + opCode + ” The value of answer is ” + answer );
secondNum = “0.0” ;
opCode = -1 ;

}
else if ( pendingOpCode == 0 || pendingOpCode == 1 || pendingOpCode == 2
|| pendingOpCode == 5 ) {
MyPrint( “plusOp() 3: In pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}
else if ( opCode == 3 ) {
MyPrint( “plusOp() 4: In pendingOp code. The opcode value is ” + opCode ) ;
firstNum = enterBox.getText() ;
if ( secondNum.equals( “” ) ) {
secondNum = “0.0” ;
}
opCode = 3 ;
pendingOp = false ;
pendingOpCode = 3 ;
MyPrint( “plusOp() 5: opCode is ” + opCode + ” The value of answer is ” + answer );
opCode = -1 ;
result = true ;
}

}
/********************** End of plusOp() ******************************/

/** **************************************************************
* convertNumsToDouble() – converts numbers entered from string
* to double
* *****************************************************************/
public void convertNumsToDouble() {

MyPrint( “convertNumsToDouble() 1: The value of firstNum is ” + firstNum );
MyPrint( “convertNumsToDouble() 2: The value of secondNum is ” + secondNum );

if ( !firstNum.equals( “” ) ) {
dNum1 = Double.parseDouble( firstNum ) ;
}
if ( !secondNum.equals( “” ) ) {
dNum2 = Double.parseDouble( secondNum ) ;
}
MyPrint( “convertNumsToDouble() 3: The value of dNum1 is ” + dNum1 );
MyPrint( “convertNumsToDouble() 4: The value of dNum2 is ” + dNum2 );

}
/** ********************* End of convertNumsToDouble() **************/

/** ******************************************************************
* PlusMinus() – toggles the sign of the entered number
* *******************************************************************/
public void PlusMinus() {
MyPrint( “PlusMinus: The value of enterBox is ” + enterBox.getText() );

if ( !enterBox.getText().equals( “” ) ) {
answer = Double.parseDouble( enterBox.getText() ) * minusOne ;
MyPrint( “1a- PlusMinus: The value of answer is ” + answer );
setResults() ;
}
else if ( !firstNum.equals( “” ) ) {
answer = Double.parseDouble( firstNum ) * minusOne ;
setResults() ;
MyPrint( “1b- PlusMinus: The value of firstNum is ” + firstNum );
}
result = true ;

}
/********************** End of PlusMinus() *******************************/

/** ***********************************************************
* MySqrt() – computes the square root of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MySqrt() {
MyPrint( “MySqrt() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MySqrt() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

setResults() ;
MyPrint( “MySqrt() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
answer = Math.sqrt( Double.parseDouble( enterBox.getText() ) ) ;
}
else {
answer = 0.0 ;
}
firstNum = “” + answer ;
secondNum = “” ;
enterBox.setText( firstNum ) ;
dNum1 = answer ;
dNum2 = 0.0 ;
opCode = -1 ;
pendingOpCode = -1 ;
pendingOp = false ;
}
/*********************** End of MySqrt() *************************/

/** ***********************************************************
* MyFact() – computes the factorial of the entered number. For
* example, !5 = 5*4*3*2*1 = 120.0 The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
*******************************************************************/
public void MyFact() {
MyPrint( “MyFact() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyFact() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyFact() 3: enterBox.getText() =s ” + enterBox.getText() ) ;
if ( opCode == 99 ) {
tempInt = ( int ) answer ;
}
else if ( !enterBox.getText().equals( “” ) ) {
MyPrint( “bFact() 4: The value of enterBox.getText() is ” + enterBox.getText() );
tempInt = (int)Double.parseDouble( enterBox.getText() ) ;
MyPrint( “bFact() 5: The value of tempInt is ” + tempInt );
}
else if ( firstNumSet ) {
MyPrint( “bFact() 5: The value of firstNum is ” + firstNum ) ;
if( firstNum.equals( “” ) )
firstNum = “0.0” ;
tempInt = (int)Double.parseDouble( firstNum ) ;
MyPrint( “bFact() 6: The value of tempInt is ” + tempInt );
}

MyPrint( “bFact() 7: The value of tempInt is ” + tempInt );
tempInt2 = 1 ;
for( int ii = 1 ; ii < tempInt + 1; ii++ ) {
tempInt2 *= ii ;
}
/**
* Code changed on 9/10/2003 to allow result to be a
* first operand.
*/
answer = (double)tempInt2 ;
enterBox.setText( “” + answer ) ;
MyPrint( “MyFact 8: The value of answer is ” + answer );
secondNum = “” ;
opCode = -1 ;
dNum2 = 0.0 ;
dNum1 = answer ;
secondNumSet = false ;
firstNum = “” + answer ;
firstNumSet = true ;
pendingOp = false ;
}
/*********************** End of MyFact() *************************/

/** ***********************************************************
* The MyMinus()) method retrieves a number from the meory. The
* logic is:
* 1- If there are numbers to be retrieved
* a) Retrieve a number using LIFO.
* b) Decrement the array index.
* c) If there is a pending operation:
* aa) Store the number in the entry box into the second number
* and indicate that the second number has been set.
* bb) Call the opCodeMethod() method.
* d) If there is a pending operation, indicate that the first
* number has been set.
* 2- If there are no numbers in the array to retriev, indicate to
* the user that there are no numbers.
*****************************************************************/
public void MyMinus() {
String tempNum ;

if ( dArrayPtr > 0 ) {
MyPrint( “MyMinus() 1: The retrieved value is ” + answer +
“firstNum is ” + firstNum ) ;
enterBox.setText( “” + dArray [ dArrayPtr-1 ] ) ;
answer = dArray [ dArrayPtr-1 ] ;
tempNum = “” + answer ;
dArrayPtr– ;
if ( pendingOpCode >= 0 ) {
secondNumSet = true ;
secondNum = enterBox.getText() ;
MyPrint( “MyMinus() 2: pendingOpCode =s ” + pendingOpCode +
“firstNum =s ” + firstNum +
“secondNum =s ” + secondNum ) ;
opCodeMethod() ;
}
else {
firstNumSet = true ;
//firstNum = enterBox.getText() ;

}
MyPrint( “MyMinus() 3: The retrieved value is ” + answer +
“firstNum is ” + firstNum ) ;
}
else {
enterBox.setText( “” ) ;
MyPrint( “MyMinus() 4: There are no more values to retrieve.” ) ;
JOptionPane.showMessageDialog( this, “There are no more values to retrieve using M-.” ,
“No More Values.”,
JOptionPane.ERROR_MESSAGE );
}
}
/*********************** End of MyMinus() *************************/

/** *****************************************************************
* OneDivX() – computes the value of one divided by the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Divide the answer/result into 1.0.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* *******************************************************************/
public void OneDivX() {
MyPrint( “OneDivX() 1: opCode is ” + opCode + ” firstNum is ” + firstNum ) ;

if ( pendingOpCode >= 0 ) {
MyPrint( “OneDivX() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

firstNum = enterBox.getText() ;
MyPrint( “OneDivX() 3: firstNum =s ” + firstNum ) ;
secondNum = “0” ;
convertNumsToDouble() ;

if ( dNum1 == 0.0 ) {
answer = dNum1 ;
setResults() ;
JOptionPane.showMessageDialog( this, “The number ” +
dNum1 + ” cannot be used as a denometer.\n” +
“You cannot divide by zero.”,
“Attempted to Divide by Zero”,
JOptionPane.ERROR_MESSAGE );
}
else {
answer = 1.0 / dNum1 ;
}

firstNum = “” + answer ;
secondNum = “” ;
setResults() ;
dNum1 = answer ;
dNum2 = 0.0 ;
opCode = -1 ;
pendingOpCode = -1 ;
pendingOp = false ;

}
/** ********** End of OneDivX() **************/

/** ***********************************************************
* MyLog() – computes the log base 10 of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyLog() {
MyPrint( “MyLog() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyLog() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyLog() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
firstNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.log( dNum1 ) / Math.log( 10.0 );
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyLog() *************************/

/** ***********************************************************
* MyALog() – computes the anti-log base 10 of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyALog() {
MyPrint( “MyALog() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
” pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp +
” enterBox.getText() =s ” + enterBox.getText() ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyALog() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyALog() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
//firstNum = enterBox.getText() ;
convertNumsToDouble() ;
//answer = Math.exp( dNum1 ) / Math.exp( 10.0 ) ;
answer = Math.pow( 10.0 , dNum1 );
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyALog() *************************/

/** ***********************************************************
* MyCos() – computes the cosine of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyCos() {
MyPrint( “MySqrt() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MySqrt() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyCos() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
firstNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.cos( Math.toRadians( dNum1 ) ) ;
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyCos() *************************/

/** ***********************************************************
* MySin() – computes the sine of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MySin() {
firstNum = enterBox.getText() ;
MyPrint( “MySin() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MySin() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MySin() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
MyPrint( “MySin() 4: enterBox.getText() =s ” + enterBox.getText() ) ;
convertNumsToDouble() ;
answer = Math.sin( Math.toRadians( dNum1 ) ) ;
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MySin() *************************/

/** ***********************************************************
* MyTan() – computes the tangent of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyTan() {
MyPrint( “MyTan() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyTan() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyTan() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
firstNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.tan( Math.toRadians( dNum1 ) ) ;
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyTan() *************************/

/** ***********************************************************
* MyACos() – computes the arc cosine of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyACos() {
MyPrint( “MyACos() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyACos() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyACos() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
firstNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.toDegrees( Math.acos( dNum1 ) ) ;
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyACos() *************************/

/** ***********************************************************
* MyASin() – computes the arc sine of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyASin() {
MyPrint( “MyASin() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyASin() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyASin() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
firstNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.toDegrees( Math.asin( dNum1 ) ) ;
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyASin() *************************/

/** ***********************************************************
* MyATan() – computes the arc tangent of the entered number.
* The logic is:
* 1- If there is a pending operation, proces it.
* 2- Take the squareroot of the value in the enter box field.
* 3- Store the anser into firstNum and dNum1
* 4- Set the secondNum to null
* 5- Enter firstNum/answer into the enter box field
* 6- Set dNum2 to 0.0
* 7- Indicate that there are no pending oprations.
* **************************************************************/
public void MyATan() {
MyPrint( “MyATan() 1: opCode is ” + opCode + ” firstNum is ” + firstNum +
“pendingOpCode =s ” + pendingOpCode +
” pendingOp =s ” + pendingOp ) ;
if ( pendingOpCode >= 0 ) {
MyPrint( “MyATan() 2: pendingOpCode =s ” + pendingOpCode ) ;
pendingOp = true ;
opCodeMethod() ;
}

MyPrint( “MyATan() 3: enterBox.getText() =s ” + enterBox.getText() ) ;

if( enterBox.getText() != “” ) {
firstNum = enterBox.getText() ;
convertNumsToDouble() ;
answer = Math.toDegrees( Math.atan( dNum1 ) ) ;
setResults() ;
}
else {
answer = 0.0 ;
}
}
/*********************** End of MyATan() *************************/

/** ***********************************************************
* sysExit() – causes the application to exit
*************************************************************/
public void sysExit() {
System.exit( 0 );

}
/** ********** End of sysExit() **************/

/** *******************************************************
* MyPrint() – toggles debug aid on or off
* ******************************************************/
public void MyPrint( String str ) {
if ( myDiag )
System.out.println( str );

}
/** ********** End of MyPrint() **************/

/** *****************************************************
* The main() method is the entry point called by Java when
* this application is loaded.
* ******************************************************/

public static void main( String args[] ) {

/** creates an instance of BasicCalc_II */
final BasicCalc_III app = new BasicCalc_III () ;

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
app.sysExit() ;
}
}
);
}
/** ********** End of main() **************/

class LogoWindowFrame extends Frame {
LogoWindow sw;
Image logoIm;

LogoWindowFrame() {
MediaTracker mt = new MediaTracker(this);
logoIm = Toolkit.getDefaultToolkit().getImage(“LogoScreen.gif”);
mt.addImage(logoIm,0);
try {
mt.waitForID(0);
}
catch(InterruptedException ie){}

sw = new LogoWindow( this, logoIm );

try {
Thread.sleep(3000);
}
catch(InterruptedException ie){}

sw.dispose();
}
}

class LogoWindow extends Window {
Image logoIm;
Thread currentThread = Thread.currentThread() ;

LogoWindow(Frame parent, Image logoIm) {
super(parent);
this.logoIm = logoIm;
setSize(550,300);

/* Center the window */
Dimension screenDim =
Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winDim = getBounds();
setLocation((screenDim.width – winDim.width) / 2,
(screenDim.height – winDim.height) / 2);
setVisible(true);
}

public void paint(Graphics g) {
if (logoIm != null) {
g.drawImage(logoIm,0,0,this);
try {
currentThread.sleep(1000);
}
catch (InterruptedException e) {}
}
}
}
}
/** ********** End of BasicCalc_II Class **************/

 

 

 

 

Advertisements

2 thoughts on “Code Java Kalkulator Fisika

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s