| | |
problem in java cobe
![]() |
•
•
Join Date: Nov 2004
Posts: 6
Reputation:
Solved Threads: 0
hi
i need to help me this is my calculator code but it's not work with me and i don't know what is the problem in this code can you help me to solve it and tell me what is the problem .
thank you .
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class calculator2 extends Applet
{
TextField display;
int saveNum = 0;
int memNum = 0;
char op = '=';
char lastOp = '=';
boolean newNum = true;
public void init ()
{
setBackground(Color.pink);
display = new TextField("0", 6);
display = setEditable(false);//
add(display);
Panel p = new Panel ();
p.setLayout(new GridLayout (5, 4));
for (int i = 1; i <= 9; i++)
{
p.add(new Button("" + i));
if (i==3)
p.add(new Button("+"));
if (i==6)
p.add(new Button("-"));
}
p.add(new Button("*"));
p.add(new Button("%"));
p.add(new Button("0"));
p.add(new Button("±")); //grabbed the sign from the character map
p.add(new Button("/"));
p.add(new Button("M*"));
p.add(new Button("RM"));
p.add(new Button("C"));
p.add(new Button("="));
add(p);
}
public boolean action(Event evt, Object arg)
{
if (arg instanceof String)
{
String s = (String) arg;
if (s.charAt(0) >= '0' && s.charAt(0) <='9')
{
if (newNum)
display.setText(display.getText() + s);
newNum = false;
}
else
{
if (s.charAt(0) == '-' && newNum)
{
display.setText("-");
newNum = false;
}
else {
op = s.charAt(0);
calc (Integer.parseInt(display.getText()));
newNum = true;
}
lastOp = op;
}
return true;
}
return super.action(evt, arg);
}
private TextField setEditable(boolean b)
{
return null;
}
public void calc(int n)
{
switch (op)
{
case '+'://saveNum += n;
case '-'://saveNum -= n;
case '*'://saveNum *= n;
case '/'://saveNum /= n;
case '%'://saveNum %= n;
saveNum = n;
return;
}
if (op == '=')
op = lastOp;
switch (op)
{
case '+':
saveNum += n;
break;
case '-':
saveNum -= n;
break;
case '*':
saveNum *= n;
break;
case '/':
saveNum /= n;
break;
case '%':
saveNum %= n;
break;
case 'M':
memNum = n;
saveNum = n;
break;
case 'R':
saveNum = memNum;
break;
case 'C':
saveNum = 0;
break;
case '±':
saveNum = -n;
break;
}
// display.setText("" + saveNum);
}}
i need to help me this is my calculator code but it's not work with me and i don't know what is the problem in this code can you help me to solve it and tell me what is the problem .
thank you .
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class calculator2 extends Applet
{
TextField display;
int saveNum = 0;
int memNum = 0;
char op = '=';
char lastOp = '=';
boolean newNum = true;
public void init ()
{
setBackground(Color.pink);
display = new TextField("0", 6);
display = setEditable(false);//
add(display);
Panel p = new Panel ();
p.setLayout(new GridLayout (5, 4));
for (int i = 1; i <= 9; i++)
{
p.add(new Button("" + i));
if (i==3)
p.add(new Button("+"));
if (i==6)
p.add(new Button("-"));
}
p.add(new Button("*"));
p.add(new Button("%"));
p.add(new Button("0"));
p.add(new Button("±")); //grabbed the sign from the character map
p.add(new Button("/"));
p.add(new Button("M*"));
p.add(new Button("RM"));
p.add(new Button("C"));
p.add(new Button("="));
add(p);
}
public boolean action(Event evt, Object arg)
{
if (arg instanceof String)
{
String s = (String) arg;
if (s.charAt(0) >= '0' && s.charAt(0) <='9')
{
if (newNum)
display.setText(display.getText() + s);
newNum = false;
}
else
{
if (s.charAt(0) == '-' && newNum)
{
display.setText("-");
newNum = false;
}
else {
op = s.charAt(0);
calc (Integer.parseInt(display.getText()));
newNum = true;
}
lastOp = op;
}
return true;
}
return super.action(evt, arg);
}
private TextField setEditable(boolean b)
{
return null;
}
public void calc(int n)
{
switch (op)
{
case '+'://saveNum += n;
case '-'://saveNum -= n;
case '*'://saveNum *= n;
case '/'://saveNum /= n;
case '%'://saveNum %= n;
saveNum = n;
return;
}
if (op == '=')
op = lastOp;
switch (op)
{
case '+':
saveNum += n;
break;
case '-':
saveNum -= n;
break;
case '*':
saveNum *= n;
break;
case '/':
saveNum /= n;
break;
case '%':
saveNum %= n;
break;
case 'M':
memNum = n;
saveNum = n;
break;
case 'R':
saveNum = memNum;
break;
case 'C':
saveNum = 0;
break;
case '±':
saveNum = -n;
break;
}
// display.setText("" + saveNum);
}}
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
Well, for one you have a function:
private TextField setEditable(boolean b) {
return null;
}
And you use it in your init method as such:
display = setEditable(false);//
add(display);
SO this tells me that you set TextField 'display' to NULL, then try to add it to your display TextField. I suspect you want to set your 'display' TextField to editable, so you could do the following:
display.setEditable(false);
Let me know if this is what you are talking about.
private TextField setEditable(boolean b) {
return null;
}
And you use it in your init method as such:
display = setEditable(false);//
add(display);
SO this tells me that you set TextField 'display' to NULL, then try to add it to your display TextField. I suspect you want to set your 'display' TextField to editable, so you could do the following:
display.setEditable(false);
Let me know if this is what you are talking about.
![]() |
Similar Threads
- Problem with Java forum (DaniWeb Community Feedback)
- problem with java.util.Collections (Java)
- Problem with Java Swing (Java)
- problem in java (Java)
- Java Problem (Windows NT / 2000 / XP)
- Problem with JAVA and XP & a few missing choices. *bangs head on desk* (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Thinking OOP!: From BASIC to JAVA
- Next Thread: Got Fractions? Method help w/ logic issue
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking bidirectional binary binarytree birt bluetooth chat chatprogramusingobjects class client code columns component database derby design designadrawingapplicationusingjavajslider eclipse encryption error errors expand fractal game givemetehcodez graphics gui homework html ide if_statement image inheritance integer intellij interface j2me java javadesktopapplications javaprojects jlabel jme jni jpanel jtextfield julia linux list map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring netbeans newbie nullpointerexception open-source problem program programming project property recursion reference ria scanner search server set sms sort sourcelabs splash sql sqlite static stop string subclass support swing testautomation threads tree ui unicode validation windows





