05-16-2011, 07:12 PM
This is a program I originally wrote in Python. This past week, I rewrote it completely in Java and changed a few details. Only half of the program is currently in GUI. I'll be working on the GUI menu later this week.
Let me know what you think. Here's the source code:
If you want the JAR file, let me know and I'll upload it
Let me know what you think. Here's the source code:
Code:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
/**
* Write a description of class PDA here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PDA
{
public static void main(String [] args)
{
System.out.println("Personal Assistant Program\n\nMenu:\n1) Search Google\n2) Launcher\n3) Notepad\n4) Calculator\n\nEnter Menu Number to Access Function");
Scanner input = new Scanner(System.in);
String first = input.nextLine();
while(!(first.equals("9")))
{
System.out.println("Executing Command");
if(first.equals("1"))
{
URLOpener();
}
else if(first.equals("2"))
{
Launcher();
}
else if(first.equals("3"))
{
Notepad app = new Notepad();
app.setVisible(true);
}
else if(first.equals("4"))
{
Calculator calc = new Calculator();
calc.makeCalculator();
}
System.out.println("Enter Menu Number to Access Function <9 to quit>");
first = input.nextLine();
}
System.out.println("Program exited!");
}
public static void URLOpener()
{
String url = "http://www.google.com/search?q=";
System.out.println("Please enter search terms: <9 to return to menu> ");
Scanner input = new Scanner(System.in);
String choice = input.nextLine();
while(!(choice.equals("9")))
{
String searchTerms = choice;
url += searchTerms;
String os = System.getProperty("os.name").toLowerCase();
Runtime rt = Runtime.getRuntime();
try {
if (os.indexOf( "win" ) >= 0) {
// this doesn't support showing urls in the form of "page.html#nameLink"
rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
} else if (os.indexOf( "mac" ) >= 0) {
rt.exec( "open " + url);
} else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) {
// Do a best guess on unix until we get a platform independent way
// Build a list of browsers to try, in this order.
String[] browsers = {"epiphany", "firefox", "mozilla",
"konqueror", "netscape","opera","links","lynx"};
// Build a command string which looks like "browser1 "url" || browser2 "url" ||..."
StringBuffer cmd = new StringBuffer();
for (int i=0; i<browsers.length; i++)
cmd.append( (i==0 ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");
rt.exec(new String[] { "sh", "-c", cmd.toString() });
} else {
return;
}
}catch (Exception e){
return;
}
return;
}
}
public static void Launcher()
{
Scanner read = new Scanner(System.in);
//System.out.println("Enter OS: <Windows, Linux, Mac>");
String os = System.getProperty("os.name").toLowerCase();
if(os.equalsIgnoreCase("Linux"))
{
System.out.println("Enter the name of the program you want to run: ");
String programName = read.nextLine();
String command= programName;
Runtime rt = Runtime.getRuntime();
try
{Process pr = rt.exec(command);}
catch(Exception e)
{return;}
}
else
System.out.println("Other systems not currently supported; sorry :/");
}
public static void Calculator()
{
Scanner in = new Scanner(System.in);
System.out.print("What operation?");
String operation = in.nextLine();
System.out.print("How many numbers are you operating? ");
int howManyNums = in.nextInt();
int i = 1;
int [] numbers = new int [howManyNums];
while(i <= howManyNums )
{
numbers[i] = in.nextInt();
i++;
}
i = 0;
int total = 0;
while(i < numbers.length)
{
total += numbers[i];
}
}
}
class Notepad extends JFrame implements ActionListener
{
private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item
private Menu file = new Menu(); // our File menu
// what's going in File? let's see...
private MenuItem openFile = new MenuItem(); // an open option
private MenuItem saveFile = new MenuItem(); // a save option
private MenuItem close = new MenuItem(); // and a close option!
public Notepad() {
this.setSize(500, 300); // set the initial size of the window
this.setTitle("Java Notepad Tutorial"); // set the title of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed)
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // set a default font for the TextArea
// this is why we didn't have to worry about the size of the TextArea!
this.getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically
this.getContentPane().add(textArea);
// add our menu bar into the GUI
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file); // we'll configure this later
// first off, the design of the menuBar itself. Pretty simple, all we need to do
// is add a couple of menus, which will be populated later on
this.file.setLabel("File");
// now it's time to work with the menu. I'm only going to add a basic File menu
// but you could add more!
// now we can start working on the content of the menu~ this gets a little repetitive,
// so please bare with me!
// time for the repetitive stuff. let's add the "Open" option
this.openFile.setLabel("Open"); // set the label of the menu item
this.openFile.addActionListener(this); // add an action listener (so we know when it's been clicked
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false)); // set a keyboard shortcut
this.file.add(this.openFile); // add it to the "File" menu
// and the save...
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);
// and finally, the close option
this.close.setLabel("Close");
// along with our "CTRL+F4" shortcut to close the window, we also have
// the default closer, as stated at the beginning of this tutorial.
// this means that we actually have TWO shortcuts to close:
// 1) the default close operation (example, Alt+F4 on Windows)
// 2) CTRL+F4, which we are about to define now: (this one will appear in the label)
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}
public void actionPerformed (ActionEvent e) {
// if the source of the event was our "close" option
if (e.getSource() == this.close)
this.dispose(); // dispose all resources and close the application
// if the source was the "open" option
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser(); // open up a file chooser (a dialog for the user to browse files to open)
int option = open.showOpenDialog(this); // get the option that the user selected (approve or cancel)
// NOTE: because we are OPENing a file, we call showOpenDialog~
// if the user clicked OK, we have "APPROVE_OPTION"
// so we want to open the file
if (option == JFileChooser.APPROVE_OPTION) {
this.textArea.setText(""); // clear the TextArea before applying the file contents
try {
// create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) // while there's still something to read
this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
} catch (Exception ex) { // catch any exceptions, and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
// and lastly, if the source of the event was the "save" option
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser(); // again, open a file chooser
int option = save.showSaveDialog(this); // similar to the open file, only this time we call
// showSaveDialog instead of showOpenDialog
// if the user clicked OK (and not cancel)
if (option == JFileChooser.APPROVE_OPTION) {
try {
// create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // write the contents of the TextArea to the file
out.close(); // close the file stream
} catch (Exception ex) { // again, catch any exceptions and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
}
}
class CalcFrame extends Frame {
CalcFrame( String str) {
// call to superclass
super(str);
// to close the calculator(Frame)
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent we) {
System.exit(0);
}
});
}
}
// main class Calculator implemnets two interfaces ActionListener
// and ItemListener
class Calculator implements ActionListener, ItemListener {
// creating instances of objects
CalcFrame fr;
MenuBar mb;
Menu view, font, about;
MenuItem bold, regular, author;
CheckboxMenuItem basic, scientific;
CheckboxGroup cbg;
Checkbox radians, degrees;
TextField display;
Button key[] = new Button[20]; // creates a button object array of 20
Button clearAll, clearEntry, round;
Button scientificKey[] = new Button[10]; // creates a button array of 8
// declaring variables
boolean addButtonPressed, subtractButtonPressed, multiplyButtonPressed;
boolean divideButtonPressed, decimalPointPressed, powerButtonPressed;
boolean roundButtonPressed = false;
double initialNumber;// the first number for the two number operation
double currentNumber = 0; // the number shown in the screen while it is being pressed
int decimalPlaces = 0;
// main function
public static void main (String args[]) {
// constructor
Calculator calc = new Calculator();
calc.makeCalculator();
}
public void makeCalculator() {
// size of the button
final int BWIDTH = 25;
final int BHEIGHT = 25;
// create frame for the calculator
fr = new CalcFrame("Basic Calculator");
// set the size
fr.setSize(210,350);
// create a menubar for the frame
mb = new MenuBar();
// add menu the menubar
view = new Menu("View");
font = new Menu ("Font");
about = new Menu("About");
// create instance of object for View menu
basic = new CheckboxMenuItem("Basic",true);
// add a listener to receive item events when the state of an item changes
basic.addItemListener(this);
scientific = new CheckboxMenuItem("Scientific");
// add a listener to receive item events when the state of an item changes
scientific.addItemListener(this);
// create instance of object for font menu
bold = new MenuItem("Arial Bold");
bold.addActionListener(this);
regular = new MenuItem("Arial Regular");
regular.addActionListener(this);
// for about menu
author = new MenuItem("Author");
author.addActionListener(this);
// add the items in the menu
view.add(basic);
view.add(scientific);
font.add(bold);
font.add(regular);
about.add(author);
// add the menus in the menubar
mb.add(view);
mb.add(font);
mb.add(about);
// add menubar to the frame
fr.setMenuBar(mb);
// override the layout manager
fr.setLayout(null);
// set the initial numbers that is 1 to 9
int count = 7;
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
// this will set the key from 1 to 9
key[count] = new Button(Integer.toString(count));
key[count].addActionListener(this);
// set the boundry for the keys
key[count].setBounds(30*(col + 1), 30*(row + 4),BWIDTH,BHEIGHT);
key[count].setSize(30, 25);
// add to the frame
fr.add(key[count++]);
}
count -= 6;
}
// Now create, addlistener and add to frame all other keys
//0
key[0] = new Button("0");
key[0].addActionListener(this);
key[0].setBounds(30,210,BWIDTH,BHEIGHT);
key[0].setSize(30, 25);
fr.add(key[0]);
//decimal
key[10] = new Button(".");
key[10].addActionListener(this);
key[10].setBounds(80,240,BWIDTH,BHEIGHT);
key[10].setSize(30, 25);
fr.add(key[10]);
//equals to
key[11] = new Button("=");
key[11].addActionListener(this);
key[11].setBounds(30,270,BWIDTH,BHEIGHT);
key[11].setBackground(Color.pink);
key[11].setSize(145, 25);
fr.add(key[11]);
//multiply
key[12] = new Button("*");
key[12].addActionListener(this);
key[12].setBounds(139,120,BWIDTH,BHEIGHT);
key[12].setBackground(Color.orange);
key[12].setSize(37, 30);
fr.add(key[12]);
//divide
key[13] = new Button("/");
key[13].addActionListener(this);
key[13].setBounds(139,150,BWIDTH,BHEIGHT);
key[13].setBackground(Color.orange);
key[13].setSize(37, 30);
fr.add(key[13]);
//addition
key[14] = new Button("+");
key[14].addActionListener(this);
key[14].setBounds(140,210,BWIDTH,BHEIGHT);
key[14].setSize(35, 55);
key[14].setBackground(Color.cyan);
fr.add(key[14]);
//subtract
key[15] = new Button("-");
key[15].addActionListener(this);
key[15].setBounds(139,180,BWIDTH,BHEIGHT);
key[15].setSize(37, 30);
key[15].setBackground(Color.orange);
fr.add(key[15]);
//reciprocal
key[16] = new Button("1/x");
key[16].addActionListener(this);
key[16].setBounds(150,120,BWIDTH,BHEIGHT);
fr.add(key[16]);
//power
key[17] = new Button("x^n");
key[17].addActionListener(this);
key[17].setBounds(60,210,BWIDTH,BHEIGHT);
key[17].setSize(30, 24);
fr.add(key[17]);
//change sign
key[18] = new Button("+/-");
key[18].addActionListener(this);
key[18].setBounds(109,240,BWIDTH,BHEIGHT);
key[18].setSize(30, 25);
fr.add(key[18]);
//factorial
key[19] = new Button("x!");
key[19].addActionListener(this);
key[19].setBounds(90,210,BWIDTH,BHEIGHT);
key[19].setSize(30, 25);
fr.add(key[19]);
// CA
clearAll = new Button("CA");
clearAll.addActionListener(this);
clearAll.setBounds(30, 240, BWIDTH+20, BHEIGHT);
clearAll.setBackground(Color.orange);
clearAll.setSize(50, 25);
fr.add(clearAll);
// CE
clearEntry = new Button("CE");
clearEntry.addActionListener(this);
clearEntry.setBounds(30, 270, BWIDTH+20, BHEIGHT);
fr.add(clearEntry);
// round
round = new Button("Round");
round.addActionListener(this);
round.setBounds(30, 300, BWIDTH+20, BHEIGHT);
round.setSize(145, 25);
round.setBackground(Color.pink);
fr.add(round);
// set display area
display = new TextField("0");
display.setBounds(30,90,150,20);
display.setBackground(Color.white);
// key for scientific calculator
// Sine
scientificKey[0] = new Button("Sin");
scientificKey[0].addActionListener(this);
scientificKey[0].setBounds(180, 120, BWIDTH + 10, BHEIGHT);
scientificKey[0].setVisible(false);
fr.add(scientificKey[0]);
// cosine
scientificKey[1] = new Button("Cos");
scientificKey[1].addActionListener(this);
scientificKey[1].setBounds(180, 150, BWIDTH + 10, BHEIGHT);
scientificKey[1].setVisible(false);
fr.add(scientificKey[1]);
// Tan
scientificKey[2] = new Button("Tan");
scientificKey[2].addActionListener(this);
scientificKey[2].setBounds(180, 180, BWIDTH + 10, BHEIGHT);
scientificKey[2].setVisible(false);
fr.add(scientificKey[2]);
// PI
scientificKey[3] = new Button("Pi");
scientificKey[3].addActionListener(this);
scientificKey[3].setBounds(180, 210, BWIDTH + 10, BHEIGHT);
scientificKey[3].setVisible(false);
fr.add(scientificKey[3]);
// aSine
scientificKey[4] = new Button("aSin");
scientificKey[4].addActionListener(this);
scientificKey[4].setBounds(220, 120, BWIDTH + 10, BHEIGHT);
scientificKey[4].setVisible(false);
fr.add(scientificKey[4]);
// aCos
scientificKey[5] = new Button("aCos");
scientificKey[5].addActionListener(this);
scientificKey[5].setBounds(220, 150, BWIDTH + 10, BHEIGHT);
scientificKey[5].setVisible(false);
fr.add(scientificKey[5]);
// aTan
scientificKey[6] = new Button("aTan");
scientificKey[6].addActionListener(this);
scientificKey[6].setBounds(220, 180, BWIDTH + 10, BHEIGHT);
scientificKey[6].setVisible(false);
fr.add(scientificKey[6]);
// E
scientificKey[7] = new Button("E");
scientificKey[7].addActionListener(this);
scientificKey[7].setBounds(220, 210, BWIDTH + 10, BHEIGHT);
scientificKey[7].setVisible(false);
fr.add(scientificKey[7]);
// to degrees
scientificKey[8] = new Button("todeg");
scientificKey[8].addActionListener(this);
scientificKey[8].setBounds(180, 240, BWIDTH + 10, BHEIGHT);
scientificKey[8].setVisible(false);
fr.add(scientificKey[8]);
// to radians
scientificKey[9] = new Button("torad");
scientificKey[9].addActionListener(this);
scientificKey[9].setBounds(220, 240, BWIDTH + 10, BHEIGHT);
scientificKey[9].setVisible(false);
fr.add(scientificKey[9]);
cbg = new CheckboxGroup();
degrees = new Checkbox("Degrees", cbg, true);
radians = new Checkbox("Radians", cbg, false);
degrees.addItemListener(this);
radians.addItemListener(this);
degrees.setBounds(185, 75, 3 * BWIDTH, BHEIGHT);
radians.setBounds(185, 95, 3 * BWIDTH, BHEIGHT);
degrees.setVisible(false);
radians.setVisible(false);
fr.add(degrees);
fr.add(radians);
fr.add(display);
fr.setVisible(true);
} // end of makeCalculator
public void actionPerformed(ActionEvent ae) {
String buttonText = ae.getActionCommand();
double displayNumber = Double.valueOf(display.getText()).doubleValue();
// if the button pressed text is 0 to 9
if((buttonText.charAt(0) >= '0') & (buttonText.charAt(0) <= '9')) {
if(decimalPointPressed) {
for (int i=1;i <=decimalPlaces; ++i)
currentNumber *= 10;
currentNumber +=(int)buttonText.charAt(0)- (int)'0';
for (int i=1;i <=decimalPlaces; ++i) {
currentNumber /=10;
}
++decimalPlaces;
display.setText(Double.toString(currentNumber));
}
else if (roundButtonPressed) {
int decPlaces = (int)buttonText.charAt(0) - (int)'0';
for (int i=0; i< decPlaces; ++i)
displayNumber *=10;
displayNumber = Math.round(displayNumber);
for (int i = 0; i < decPlaces; ++i) {
displayNumber /=10;
}
display.setText(Double.toString(displayNumber));
roundButtonPressed = false;
}
else {
currentNumber = currentNumber * 10 + (int)buttonText.charAt(0)-(int)'0';
display.setText(Integer.toString((int)currentNumber));
}
}
// if button pressed is addition
if(buttonText == "+") {
addButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
// if button pressed is subtract
if (buttonText == "-") {
subtractButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
// if button pressed is divide
if (buttonText == "/") {
divideButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
// if button pressed is multiply
if (buttonText == "*") {
multiplyButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
// if button pressed is reciprocal
if (buttonText == "1/x") {
// call reciprocal method
display.setText(reciprocal(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
// if button is pressed to change a sign
if (buttonText == "+/-") {
// call changesign meyhod to change the sign
display.setText(changeSign(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
// factorial button
if (buttonText == "x!") {
display.setText(factorial(displayNumber));
currentNumber = 0;
decimalPointPressed = false;
}
// power button
if (buttonText == "x^n") {
powerButtonPressed = true;
initialNumber = displayNumber;
currentNumber = 0;
decimalPointPressed = false;
}
// now for scientific buttons
if (buttonText == "Sin") {
if (degrees.getState())
display.setText(Double.toString(Math.sin(Math.PI * displayNumber/180)));
else {
display.setText(Double.toString(Math.sin(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "Cos") {
if (degrees.getState())
display.setText(Double.toString(Math.cos(Math.PI * displayNumber/180)));
else{
display.setText(Double.toString(Math.cos(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "Tan") {
if (degrees.getState())
display.setText(Double.toString(Math.tan(Math.PI * displayNumber/180)));
else {
display.setText(Double.toString(Math.tan(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "aSin") {
if (degrees.getState())
display.setText(Double.toString(Math.asin(displayNumber)* 180/Math.PI ));
else {
display.setText(Double.toString(Math.asin(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "aCos") {
if (degrees.getState())
display.setText(Double.toString(Math.acos(displayNumber)* 180/Math.PI ));
else {
display.setText(Double.toString(Math.acos(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
if (buttonText == "aTan") {
if (degrees.getState())
display.setText(Double.toString(Math.atan(displayNumber)* 180/Math.PI ));
else {
display.setText(Double.toString(Math.atan(displayNumber)));
currentNumber = 0;
decimalPointPressed = false;
}
}
// this will convert the numbers displayed to degrees
if (buttonText == "todeg")
display.setText(Double.toString(Math.toDegrees(displayNumber)));
// this will convert the numbers displayed to radians
if (buttonText == "torad")
display.setText(Double.toString(Math.toRadians(displayNumber)));
if (buttonText == "Pi") {
display.setText(Double.toString(Math.PI));
currentNumber =0;
decimalPointPressed = false;
}
if (buttonText == "Round")
roundButtonPressed = true;
// check if decimal point is pressed
if (buttonText == ".") {
String displayedNumber = display.getText();
boolean decimalPointFound = false;
int i;
decimalPointPressed = true;
// check for decimal point
for (i =0; i < displayedNumber.length(); ++i) {
if(displayedNumber.charAt(i) == '.') {
decimalPointFound = true;
continue;
}
}
if (!decimalPointFound)
decimalPlaces = 1;
}
if(buttonText == "CA"){
// set all buttons to false
resetAllButtons();
display.setText("0");
currentNumber = 0;
}
if (buttonText == "CE") {
display.setText("0");
currentNumber = 0;
decimalPointPressed = false;
}
if (buttonText == "E") {
display.setText(Double.toString(Math.E));
currentNumber = 0;
decimalPointPressed = false;
}
// the main action
if (buttonText == "=") {
currentNumber = 0;
// if add button is pressed
if(addButtonPressed)
display.setText(Double.toString(initialNumber + displayNumber));
// if subtract button is pressed
if(subtractButtonPressed)
display.setText(Double.toString(initialNumber - displayNumber));
// if divide button is pressed
if (divideButtonPressed) {
// check if the divisor is zero
if(displayNumber == 0) {
MessageBox mb = new MessageBox ( fr, "Error ", true, "Cannot divide by zero.");
mb.show();
}
else
display.setText(Double.toString(initialNumber/displayNumber));
}
// if multiply button is pressed
if(multiplyButtonPressed)
display.setText(Double.toString(initialNumber * displayNumber));
// if power button is pressed
if (powerButtonPressed)
display.setText(power(initialNumber, displayNumber));
// set all the buttons to false
resetAllButtons();
}
if (buttonText == "Arial Regular") {
for (int i =0; i < 10; ++i)
key[i].setFont(new Font("Arial", Font.PLAIN, 12));
}
if (buttonText == "Arial Bold") {
for (int i =0; i < 10; ++i)
key[i].setFont(new Font("Arial", Font.BOLD, 12));
}
if (buttonText == "Author") {
MessageBox mb = new MessageBox ( fr, "Calculator ver 1.0 beta ", true, "Author: Inder Mohan Singh.");
mb.show();
}
} // end of action events
public void itemStateChanged(ItemEvent ie) {
if (ie.getItem() == "Basic") {
basic.setState(true);
scientific.setState(false);
fr.setTitle("Basic Calculator");
fr.setSize(200,270);
// check if the scientific keys are visible. if true hide them
if (scientificKey[0].isVisible()) {
for (int i=0; i < 8; ++i)
scientificKey[i].setVisible(false);
radians.setVisible(false);
degrees.setVisible(false);
}
}
if (ie.getItem() == "Scientific") {
basic.setState(false);
scientific.setState(true);
fr.setTitle("Scientific Calculator");
fr.setSize(270,270);
// check if the scientific keys are visible. if true display them
if (!scientificKey[0].isVisible()) {
for (int i=0; i < 10; ++i)
scientificKey[i].setVisible(true);
radians.setVisible(true);
degrees.setVisible(true);
}
}
} // end of itemState
// this method will reset all the buttonPressed property to false
public void resetAllButtons() {
addButtonPressed = false;
subtractButtonPressed = false;
multiplyButtonPressed = false;
divideButtonPressed = false;
decimalPointPressed = false;
powerButtonPressed = false;
roundButtonPressed = false;
}
public String factorial(double num) {
int theNum = (int)num;
if (theNum < 1) {
MessageBox mb = new MessageBox (fr, "Facorial Error", true,
"Cannot find the factorial of numbers less than 1.");
mb.show();
return ("0");
}
else {
for (int i=(theNum -1); i > 1; --i)
theNum *= i;
return Integer.toString(theNum);
}
}
public String reciprocal(double num) {
if (num ==0) {
MessageBox mb = new MessageBox(fr,"Reciprocal Error", true,
"Cannot find the reciprocal of 0");
mb.show();
}
else
num = 1/num;
return Double.toString(num);
}
public String power (double base, double index) {
return Double.toString(Math.pow(base, index));
}
public String changeSign(double num) {
return Double.toString(-num);
}
}
class MessageBox extends Dialog implements ActionListener {
Button ok;
MessageBox(Frame f, String title, boolean mode, String message) {
super(f, title, mode);
Panel centrePanel = new Panel();
Label lbl = new Label(message);
centrePanel.add(lbl);
add(centrePanel, "Center");
Panel southPanel = new Panel();
ok = new Button ("OK");
ok.addActionListener(this);
southPanel.add(ok);
add(southPanel, "South");
pack();
addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent we) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent ae) {
dispose();
}
}
If you want the JAR file, let me know and I'll upload it