• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

calculator program help please

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
have been writing this program for a while now but cant get to compile and run, iv been useing guides of the net and use textpad to code program, and when i compile i get error's.


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

public class Calculate2 extends JFrame implements ActionListener
{
private boolean isResult = false;
private boolean debug = false;

JTextField display = new JTextField(20);
JMenuItem file = new JMenuItem("File");
JMenuItem about = new JMenuItem("About");
Stack stack = new Stack();

public Calculate2()
{
JMenu exit = new JMenu("Exit");
JMenu help = new JMenu("Help");
JMenuBar menuBar = new JMenuBar();

setTitle("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container pane = getContentPane();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));

// set up the menu bar
setJMenuBar(menuBar);
menuBar.add(exit);
menuBar.add(help);
exit.add(file);
help.add(about);


// make the buttons do something
about.addActionListener(this);


// setup the buttons
JButton plus = new JButton("+");
JButton minus = new JButton("-");
JButton mult = new JButton("*");
JButton div = new JButton("/");
JButton equal = new JButton("=");
// ------------------------------
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton three = new JButton("3");
JButton four = new JButton("4");
JButton five = new JButton("5");
JButton six = new JButton("6");
JButton seven = new JButton("7");
JButton eight = new JButton("8");
JButton nine = new JButton("9");
JButton zero = new JButton("0");

// ------------------------------

JButton dot = new JButton("sin");
JButton posneg = new JButton("cos");
JButton onex = new JButton("tan");
JButton percent = new JButton("^");
JButton sqrt = new JButton("sqrt");

plus.addActionListener(this);
minus.addActionListener(this);
mult.addActionListener(this);
div.addActionListener(this);
equal.addActionListener(this);

// ---------------------------

one.addActionListener(this);
two.addActionListener(this);
three.addActionListener(this);
four.addActionListener(this);
five.addActionListener(this);
six.addActionListener(this);
seven.addActionListener(this);
eight.addActionListener(this);
nine.addActionListener(this);
zero.addActionListener(this);

// ---------------------------

dot.addActionListener(this);
posneg.addActionListener(this);
onex.addActionListener(this);
percent.addActionListener(this);
sqrt.addActionListener(this);

JPanel buttons = new JPanel(new GridLayout(4,5));
//JPanel clear = new JPanel(new FlowLayout(1,3));
// ----------------
buttons.add(seven);
buttons.add(eight);
buttons.add(nine);
buttons.add(div);
buttons.add(sqrt);
// -----------------
buttons.add(four);
buttons.add(five);
buttons.add(six);
buttons.add(mult);
buttons.add(percent);
// -----------------
buttons.add(one);
buttons.add(two);
buttons.add(three);
buttons.add(minus);
buttons.add(onex);
// -----------------
buttons.add(zero);
buttons.add(posneg);
buttons.add(dot);
buttons.add(plus);
buttons.add(equal);

JPanel textStuff = new JPanel();
textStuff.add(display);
textStuff.add(equal);

textStuff.add(display);
pane.add(textStuff);
pane.add(buttons);
setSize(300, 250);
display.setText("");
}

public boolean isOperator(String a)
{
if(a == "+" || a == "-" || a == "/" || a == "*" || a == "^" ||
a == "=" || a == "ln" || a == "sin" || a == "cos" ||
a == "tan" || a == "e^" || a == "sqrt")
return true;
else
return false;
}

public boolean isNumber(String a)
{
//if(a == "pi")
//return true;
char b;
for(int i=0; i < a.length(); i++)
{
b = a.charAt(i);
if(b != '0' && b != '1' && b != '2' && b != '3' &&
b != '4' && b != '5' && b != '6' && b != '7' &&
b != '8' && b != '9')
return false;
}
return true;
}

public void actionPerformed(ActionEvent e)
{
try {
// wrapping in a try-catch

String button = e.getActionCommand();
if(button == "Undo") {
if(stack.size() < 1) {
display.setText("");
} else if(stack.size() < 1) {
// do nothing
} else {
isResult = false;
display.setText("");
stack.pop();
if(isNumber(String.valueOf(stack.peek())))
display.setText(String.valueOf(stack.peek()));
printStack();
}
} else if(button == "Clear") {
stack.clear();
display.setText("");
printStack();
} else if(button == "Quit") {
System.exit(0);
} else if(button == "0" || button == "1" ||
button == "2" || button == "3" ||
button == "4" || button == "5" ||
button == "6" || button == "7" ||
button == "8" || button == "9") {
if(isResult == true) {
display.setText(button);
isResult = false;
} else
display.setText(display.getText() + button);
} else if(button == "+" || button == "-"
|| button == "/" || button == "*"
|| button == "^") {
if(display.getText().length() != 0) {
if(isResult) {
display.setText("");
isResult = false;
stack.push(button);
} else {
stack.push(display.getText());
stack.push(button);
display.setText("");
printStack();
}
} else
System.out.println("ERROR: Invalid input!");
} else if(button == "=") {
if(isResult) {
// do nothing
} else if(stack.size() > 0) {
if(display.getText().length() > 0) {
stack.push(display.getText());
display.setText("");
}
Enumeration enu = stack.elements();
double value = Double.parseDouble(
String.valueOf(enu.nextElement()));
//System.out.println("Value: " + value);
String aba = String.valueOf(enu.nextElement());
//System.out.println("aba: " + aba);
while(enu.hasMoreElements())
{
if(! isOperator(aba)) {
if(debug)
System.out.println("ERROR!");
}
if(aba == "+")
value += Double.parseDouble(
String.valueOf(
enu.nextElement()));
if(aba == "-")
value -= Double.parseDouble(
String.valueOf(
enu.nextElement()));
if(aba == "/")
value /= Double.parseDouble(
String.valueOf(
enu.nextElement()));
if(aba == "*")
value *= Double.parseDouble(
String.valueOf(
enu.nextElement()));
if(aba == "^")
value = Math.pow(value,
Double.parseDouble(
String.valueOf(
enu.nextElement())));
if(aba == "ln")
value = Math.log(value);
if(aba == "sin")
value = Math.sin(value);
if(aba == "cos")
value = Math.cos(value);
if(aba == "e^")
value = Math.exp(value);
if(aba == "tan")
value = Math.tan(value);
if(aba == "sqrt")
value = Math.sqrt(value);
//System.out.println("Got: " + value);
if(enu.hasMoreElements())
aba = String.valueOf(enu.nextElement());
}
display.setText(String.valueOf(value));
isResult = true;
printStack();
} else
System.out.println("ERROR: Invalid input!");
} else if(button == "pi") {
display.setText(Double.toString(Math.PI));
printStack();
// unary operators
} else if(button == "ln" || button == "sin" ||
button == "cos" || button == "e^" ||
button == "tan" || button == "sqrt") {
if(stack.size() > 0) {
if(isNumber(String.valueOf(stack.peek()))) {
stack.push(button);
if(button == "ln")
display.setText(String.valueOf(
Math.log(
Double.parseDouble(
String.valueOf(
stack.peek())))));
if(button == "sin")
display.setText(String.valueOf(
Math.sin(
Double.parseDouble(
String.valueOf(
stack.peek())))));
if(button == "cos")
display.setText(String.valueOf(
Math.cos(
Double.parseDouble(
String.valueOf(
stack.peek())))));
if(button == "e^")
display.setText(String.valueOf(
Math.exp(
Double.parseDouble(
String.valueOf(
stack.peek())))));
if(button == "tan")
display.setText(String.valueOf(
Math.tan(
Double.parseDouble(
String.valueOf(
stack.peek())))));
if(button == "sqrt")
display.setText(String.valueOf(
Math.sqrt(
Double.parseDouble(
String.valueOf(
stack.peek())))));
printStack();
} else
System.out.println(
"ERROR: Nothing to evaluate!");
} else if(display.getText().length() > 0) {
stack.push(display.getText());
stack.push(button);
if(button == "ln")
display.setText(String.valueOf(
Math.log(Double.parseDouble(
display.getText()))));
if(button == "sin")
display.setText(String.valueOf(
Math.sin(Double.parseDouble(
display.getText()))));
if(button == "cos")
display.setText(String.valueOf(
Math.cos(Double.parseDouble(
display.getText()))));
if(button == "e^")
display.setText(String.valueOf(
Math.exp(Double.parseDouble(
display.getText()))));
if(button == "tan")
display.setText(String.valueOf(
Math.tan(Double.parseDouble(
display.getText()))));
if(button == "sqrt")
display.setText(String.valueOf(
Math.sqrt(Double.parseDouble(
display.getText()))));
printStack();
} else
System.out.println("ERROR: Invalid input!");
} else {
System.out.println("ERROR: No handler for " + button);
}

// ending try-catch
} catch(Exception exp) {
if(debug)
System.out.println("ERROR: " + exp.getMessage());
}
}

public void printStack()
{
if(debug)
{
if(stack.size() == 0)
System.out.println("Stack is empty.");
else {
System.out.println("Stack contains " + stack.size()
+ " elements:");
for(int i=0; i < stack.size(); i++)
System.out.println(i+1 + ": " + stack.get(i));
}
}
}

public static void main(String[] args)
{
Calculate2 calc = new Calculate2();
calc.setVisible(true);
calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
[ April 03, 2006: Message edited by: dave ash ]
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you write all this before you ever tried compiling it? if so, THAT is your biggest mistake. most coders will write as little as possible before doing the compile/fix/test cycle. writing almost 400 lines and THEN finding out it won't compile is definatly going to give you ulcers.

now, having said that, WHAT EXACTLY is the compile errors you're getting? those messages give you BIG hints on what to look at. there could be any number of things going wrong, and since none of us have the same environment as you, we may not see the same errors even if we did try compiling it ourselves.
 
fred rosenberger
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i did notice that the code you posted doesn't have a valid main. you are missing the closing curly brace.
 
dave ash
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi the errors i am getting are:
Note: C:\Documents and Settings\Owner\Desktop\Calculate2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

and when i run i get:
Exception in thread "main" java.lang.NoClassDefFoundError: Calculate2

does anyone know what i am missing
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey dave,

ur code seems to be ok.

are u compling it in a correct folder, where ur code exists. Thats why may compiler is giving you noclassdef found error.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dave ash:
hi the errors i am getting are:
Note: C:\Documents and Settings\Owner\Desktop\Calculate2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

and when i run i get:
Exception in thread "main" java.lang.NoClassDefFoundError: Calculate2

does anyone know what i am missing


The messages during compilation are warning you about possible type safety issues. This is because you're using a Stack, which now uses generics with Java 5.0, and you didn't specify a type. If these are the only messages you got, these warnings should not prevent compilation. However...

The runtime exception indicates that Java can't find the compiled Calculate2.class. Have you verified that there really is a class file, and are you executing from the directory that contains that class file?
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compile and calculate it, it works.

Please edit your initial post and use code-tags - this isn't good style in the intermediate section.
[ April 04, 2006: Message edited by: Stefan Wagner ]
 
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic