1. Member
Join Date
Jun 2012
Posts
5
Rep Power
0

## Java exercise

This is a calculator but for the experts here you can read it. Can someone help me with adding two new functions like percentage and square root. I don't have the knowledge to make it right .
I'll be very thankful if someone make it happen and help with this exercise.

Java Code:
```//CalculateurNC
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CalculateurNC extends Applet {
String arg="";
int rezVal=0;
Button number[] = new Button[10];
Button func[] = new Button[6];
Panel num_panel;
Panel func_panel;
Panel rez_panel;
TextField rez;
char oper;

public void init(){
oper = ' ';
setLayout(null);

num_panel = new Panel();
num_panel.setLayout(new GridLayout(4,3));
for(int i=9; i>=0; i--){
number[i] = new Button((new String()).valueOf(i));
}

func[0] = new Button("C");

func[1] = new Button("=");
num_panel.setBounds(20,100,180,180);

func_panel = new Panel();
func_panel.setLayout(new GridLayout(4,1));
func[2] = new Button("+");

func[3] = new Button("-");

func[4] = new Button("*");

func[5] = new Button("^");

func_panel.setBounds(210,100,65,180);

rez_panel = new Panel();
rez = new TextField("0",10);
rez_panel.setBounds(20,20,240,40);

}

class BN implements ActionListener{
public void actionPerformed(ActionEvent e) {
arg += e.getActionCommand();
rez.setText(arg);
}
}

class OPER implements ActionListener{
public void actionPerformed(ActionEvent e) {
int i,help,argi;
if(arg.length()==0)argi=0;
else argi = (new Integer(arg)).intValue();
switch(oper){
case '+': rezVal += argi;break;
case '-': rezVal -= argi;break;
case '*': rezVal *= argi;break;
case '=': oper = ' ';break;
case '^': for(i=1,help=rezVal; i < argi;i++)
rezVal *=help;
break;
case ' ': rezVal= argi;
}
rez.setText(rezVal+"");
arg = "";
oper = e.getActionCommand().charAt(0);
}
}

class C implements ActionListener{
public void actionPerformed(ActionEvent e) {
arg = "";
rezVal =0;
oper = ' ';
rez.setText(rezVal+"");
}
}

// To close the application:
static class WL extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}

// A main() for the application:
public static void main(String[] args) {
CalculateurNC applet = new CalculateurNC();
Frame aFrame = new Frame("CalculateurNC");
aFrame.setSize(400,400);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}```
Last edited by Fubarable; 06-26-2012 at 09:46 PM. Reason: code tags added

2. ## Re: Java exercise

Originally Posted by Mbka
This is a calculator but for the experts here you can read it. Can someone help me with adding two new functions like percentage and square root. I don't have the knowledge to make it right .
I'll be very thankful if someone make it happen and help with this exercise.
You should at least try to add this functionality first, then we can discuss problems with your attempt if it doesn't work. Else, how will we know just where you're stuck?

3. Member
Join Date
Jun 2012
Posts
5
Rep Power
0

## Re: Java exercise

That's the problem I don't know were to add the functionality and how.
Last edited by Mbka; 06-26-2012 at 09:41 PM.

4. Member
Join Date
Jun 2012
Posts
5
Rep Power
0

## Re: Java exercise

I forgot to write that I need to add two new buttons like percentage and square root in the code but I'm saying it again, I don't know how to make it.
I'll be very thankful to everybody who help me to make it right.

5. Member
Join Date
Jun 2012
Posts
22
Rep Power
0

## Re: Java exercise

Originally Posted by Mbka
I forgot to write that I need to add two new buttons like percentage and square root in the code but I'm saying it again, I don't know how to make it.
I'll be very thankful to everybody who help me to make it right.
Mbka did you write the code ?
Another thing is, if you just want to add two new buttons, there's no much to do. You just need to copy the same syntax you already have for the others buttons and add the respective the functionality, on the other hand if you don't know how to do it. I extremly recommen to take a walk on Google, and search some GUI, SWING, APPLET INFO.

6. Member
Join Date
Jun 2012
Posts
5
Rep Power
0

## Re: Java exercise

Yes I just need to add two new buttons like percentage and square root. The code is at the beginning. I didn't study any program code that why I write here so somebody to do it right. I have pretty much basic knowledge.

7. Moderator
Join Date
Apr 2009
Posts
13,324
Rep Power
23

## Re: Java exercise

I'm sorry, if you wrote that code (which has quite a few buttons of its own already) why on earth are you incapable of adding two new ones, and at least attaching the listener to them even if there isn't any code that will do the calculation.

Oh, and you really need to format that code.

8. Member
Join Date
Jun 2012
Posts
5
Rep Power
0

## Re: Java exercise

I didn't wrote that code. I got it like that my task is to add two new buttons like percentage and square root, but I don't know how to do it, so I'm asking for help and a friend told me to try in this forum.

9. Moderator
Join Date
Apr 2009
Posts
13,324
Rep Power
23

## Re: Java exercise

You have lots of examples in that code.

10. ## Re: Java exercise

Please go through the Forum Rules -- particularly the third paragraph.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•