Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By jfabian

Thread: Java exercise

  1. #1
    Mbka is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Java exercise

    Quote Originally Posted by Mbka View Post
    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. #3
    Mbka is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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. #4
    Mbka is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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. #5
    jfabian is offline Member
    Join Date
    Jun 2012
    Posts
    22
    Rep Power
    0

    Default Re: Java exercise

    Quote Originally Posted by Mbka View Post
    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.
    Fubarable likes this.

  6. #6
    Mbka is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  8. #8
    Mbka is offline Member
    Join Date
    Jun 2012
    Posts
    5
    Rep Power
    0

    Default 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. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default 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.

    ** This space for rent **

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Java exercise

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java exercise- help plese
    By adrian10288 in forum New To Java
    Replies: 1
    Last Post: 11-25-2011, 03:47 PM
  2. Java exercise I REALLY need help on
    By velvetymold in forum New To Java
    Replies: 2
    Last Post: 03-27-2011, 02:02 AM
  3. finishing up a DB/Java exercise
    By hayden06f4i in forum New To Java
    Replies: 47
    Last Post: 12-12-2010, 09:57 PM
  4. Exercise for java 3d
    By armiri in forum Java 2D
    Replies: 2
    Last Post: 05-13-2010, 11:14 PM
  5. Exercise for java 3d
    By armiri in forum Java Software
    Replies: 3
    Last Post: 05-13-2010, 11:13 PM

Posting Permissions

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