Results 1 to 10 of 10
Thread: Java exercise
- 06-26-2012, 07:22 PM #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)); number[i].addActionListener(new BN()); num_panel.add(number[i]); } func[0] = new Button("C"); func[0].addActionListener(new C()); num_panel.add(func[0]); func[1] = new Button("="); func[1].addActionListener(new OPER()); num_panel.add(func[1]); num_panel.setBounds(20,100,180,180); add( num_panel); func_panel = new Panel(); func_panel.setLayout(new GridLayout(4,1)); func[2] = new Button("+"); func[2].addActionListener(new OPER()); func_panel.add(func[2]); func[3] = new Button("-"); func[3].addActionListener(new OPER()); func_panel.add(func[3]); func[4] = new Button("*"); func[4].addActionListener(new OPER()); func_panel.add(func[4]); func[5] = new Button("^"); func[5].addActionListener(new OPER()); func_panel.add(func[5]); func_panel.setBounds(210,100,65,180); add(func_panel); rez_panel = new Panel(); rez = new TextField("0",10); rez_panel.add(rez); rez_panel.setBounds(20,20,240,40); add(rez_panel); } 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.addWindowListener(new WL()); aFrame.add(applet, BorderLayout.CENTER); 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
-
Re: Java exercise
- 06-26-2012, 07:35 PM #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.
- 06-27-2012, 12:54 AM #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.
- 06-27-2012, 03:02 AM #5
Member
- Join Date
- Jun 2012
- Posts
- 22
- Rep Power
- 0
Re: Java exercise
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.
- 06-27-2012, 01:21 PM #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.
- 06-27-2012, 01:59 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 06-27-2012, 03:40 PM #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.
- 06-27-2012, 03:54 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Java exercise
Start by adding one button.
You have lots of examples in that code.Please do not ask for code as refusal often offends.
- 06-28-2012, 08:58 PM #10
Re: Java exercise
Please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Java exercise- help plese
By adrian10288 in forum New To JavaReplies: 1Last Post: 11-25-2011, 03:47 PM -
Java exercise I REALLY need help on
By velvetymold in forum New To JavaReplies: 2Last Post: 03-27-2011, 02:02 AM -
finishing up a DB/Java exercise
By hayden06f4i in forum New To JavaReplies: 47Last Post: 12-12-2010, 09:57 PM -
Exercise for java 3d
By armiri in forum Java 2DReplies: 2Last Post: 05-13-2010, 11:14 PM -
Exercise for java 3d
By armiri in forum Java SoftwareReplies: 3Last Post: 05-13-2010, 11:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks