Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1

    Default HELP - Java Calculator Debug


    Mga bro, good day!

    Naa koy nahimo nga Calculator application using Java and I encountered a problem: Di mugawas ang sakto nga result because ang result nga mugawas kay pirmi 0.0

    Note:
    - Most sa mga code ani kay ako gahimo, although I borrowed some ideas from other programmers' calculator
    - The calculator by itself is not yet complete and I wish to add more operations when this problem is already solved..

    So, here's the code:

    1st Class: Calculator
    Code:
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    
    public class Calculator extends JFrame implements ActionListener {
        final int MAX_INPUT_LENGTH = 20;
    
        private JPanel jplButtons, jplMaster, jplOperations;
        private JTextField output;
        private JButton jbtnButtons[];
    
        private Operation operation;
    
        private double operandOne;
        private double operandTwo;
        private String operator = "";
    
        private boolean clearOnNextDigit = false;
        
        public Calculator() {
            jplMaster = new JPanel();
    
            jbtnButtons = new JButton[18];
            jplButtons = new JPanel();
            jplButtons.setLayout(new GridLayout(4, 5, 2, 2));       
            for (int i = 9; i >= 0; i--) {
                jbtnButtons[i] = new JButton(String.valueOf(i));
                jplButtons.add(jbtnButtons[i]);
                jbtnButtons[i].addActionListener(this);
            }
    
            jplOperations = new JPanel();
            jplOperations.setLayout(new GridLayout(4, 2, 2, 2));
            jbtnButtons[10] = new JButton("+");
            jbtnButtons[11] = new JButton("-");
            jbtnButtons[12] = new JButton("*");
            jbtnButtons[13] = new JButton("/");
            jbtnButtons[14] = new JButton("%");
            jbtnButtons[15] = new JButton("=");
            jbtnButtons[16] = new JButton("CE");
            jbtnButtons[17] = new JButton("C");
            for (int i = 10; i < jbtnButtons.length; i++){
                jplOperations.add(jbtnButtons[i]);
                jbtnButtons[i].addActionListener(this);
            }
    
            output = new JTextField(MAX_INPUT_LENGTH);
            setOutput("0");
    
            jplMaster.setLayout(new BorderLayout());
            jplMaster.add(output, BorderLayout.NORTH);
            jplMaster.add(jplButtons, BorderLayout.CENTER);
            jplMaster.add(jplOperations, BorderLayout.EAST);
            
            add(jplMaster);
               
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
    
        public String getOutput() {
            return output.getText();
        }
    
        public void setOutput(String string) {
            output.setText(string);
        }
    
        public void addDigit (int digit) {
            if (clearOnNextDigit) {
                output.setText("");
            }
    
            String currentDisplay = getOutput();
    
            if (currentDisplay.equals("0")){
                currentDisplay = "";
            }
    
            setOutput(currentDisplay + digit);
            clearOnNextDigit = false;
        }
    
        public void processOperator(String operator) {
            operandOne = getNumberInDisplay();
            clearOnNextDigit = true;
            this.operator = operator;
        }
    
        public double getNumberInDisplay() {
            String input = getOutput();
            return Double.parseDouble(input);
        }
    
        public void clearAll() {
            setOutput("0");
            operator = "0";
            operandOne = operandTwo = 0;
            clearOnNextDigit = true;
        }
    
        public void clearExisting() {
            setOutput("0");
            clearOnNextDigit = true;
        }
    
        public static void main(String args[]) {
            Calculator calculator = new Calculator();
    
            calculator.setResizable(false);
            calculator.setVisible(true);
            calculator.setTitle("My Calculator");
            calculator.setSize(241, 217);
            calculator.setLocation(400, 250);
            calculator.setBackground(Color.gray);
        }
    
        public void actionPerformed(ActionEvent e) {
            String inputNumber = getOutput();
    
            for (int i = 0; i < jbtnButtons.length; i++) {
                if (e.getSource() == jbtnButtons[i] && inputNumber.length() < MAX_INPUT_LENGTH){
                    switch (i) {
                        case 0:
                            addDigit(i);
                            break;
                        case 1:
                            addDigit(i);
                            break;
                        case 2:
                            addDigit(i);
                            break;
                        case 3:
                            addDigit(i);
                            break;
                        case 4:
                            addDigit(i);
                            break;
                        case 5:
                            addDigit(i);
                            break;
                        case 6:
                            addDigit(i);
                            break;
                        case 7:
                            addDigit(i);
                            break;
                        case 8:
                            addDigit(i);
                            break;
                        case 9:
                            addDigit(i);
                            break;
                        case 10:
                            processOperator("+");
                            break;
                        case 11:
                            processOperator("-");
                            break;
                        case 12:
                            processOperator("*");
                            break;
                        case 13:
                            processOperator("/");
                            break;
                        case 14:
                            processOperator("%");
                            break;
                        case 15:
                            operandTwo = getNumberInDisplay();
                            operation = new Operation(operandOne, operandTwo, operator);
                            setOutput(Double.toString(operation.processOperation()));
                            clearOnNextDigit = true;
                            break;
                        case 16:
                            clearExisting();
                            break;
                        case 17:
                            clearAll();
                            break;
                    }
                }
            }
        }
    }
    2nd Class: Operation
    Code:
    public class Operation {
      private double firstOperand;
      private double secondOperand;
      private String operator;
      private Operation operate;
    
      public Operation() {
      }
    
      public Operation(double firstOperand, double secondOperand, String operator) {
        this.firstOperand = firstOperand;
        this.secondOperand = secondOperand;
        this.operator = operator;
      }
    
      public double calculate() {
        return 0;
      }
    
      public double getFirstOperand() {
        return firstOperand;
      }
    
      public double getSecondOperand() {
        return secondOperand;
      }
    
      public double processOperation(){
         if (operator.equals("+")){
             operate = new Addition();
         }
         else if (operator.equals("-")){
             operate = new Subtraction();
         }
         else if (operator.equals("*")){
             operate = new Multiplication();
         }
         else if (operator.equals("/")){
             operate = new Division();
         }
         else {
             operate = new Modulo();
         }
             
        return operate.calculate();
      }
      
      class Addition extends Operation {
          @Override
          public double calculate() {
              return (super.getFirstOperand() + super.getSecondOperand());
          }
      }
    
      class Subtraction extends Operation {
         @Override
         public double calculate() {
            return (super.getFirstOperand() - super.getSecondOperand());
         }
      }
    
      class Multiplication extends Operation {  
          @Override
          public double calculate() {
              return (super.getFirstOperand() * super.getSecondOperand());
          }
      }
    
      class Division extends Operation {
          @Override
          public double calculate() {
              return (super.getFirstOperand() / super.getSecondOperand());
          }
      }
    
      class Modulo extends Operation {
          @Override
          public double calculate() {
              return (super.getFirstOperand() % super.getSecondOperand());
          }
      }
    }
    Mga bro, I hope maka tabang mo in any way. I very much appreciate any suggestion and comments. Thanks!

  2. #2

    Default Re: HELP - Java Calculator Debug

    up for this.. help my classmate here! hehehe sakpan njd tka dri charles wahahah

  3. #3

    Default Re: HELP - Java Calculator Debug

    Idol Jacob! Haha

  4. #4

    Default Re: HELP - Java Calculator Debug

    Mga bro, I was thinking maybe naa sa Operation class ang sayup? I really don't know what's wrong sa akong code since wala man ning prompt ug error during compile time ug during run time.

  5. #5

    Default Re: HELP - Java Calculator Debug

    taas ra keyu imu code para sa calculator nga app

  6. #6

    Default Re: HELP - Java Calculator Debug

    kudos to whoever made this.

    anyway, the problem with this is that you didnt pass any value to your subclass and thats why it always results to 0.0.

    overload calculate :

    Code:
      public double calculate() {
        return 0;
      }
      
      public double calculate(double a, double b) {
        return 0;
      }
    pass the values :

    Code:
    public double processOperation(){
         if (operator.equals("+")){
             operate = new Addition();
         }
         else if (operator.equals("-")){
             operate = new Subtraction();
         }
         else if (operator.equals("*")){
             operate = new Multiplication();
         }
         else if (operator.equals("/")){
             operate = new Division();
         }
         else {
             operate = new Modulo();
         }
         
        return operate.calculate(getFirstOperand(),getSecondOperand());
      }
    blah blah blah :

    Code:
    class Addition extends Operation {
          @Override
          public double calculate(double a, double b) {
              return (a + b);
          }
      }

    Quote Originally Posted by i4chub2x View Post
    taas ra keyu imu code para sa calculator nga app
    yeah, @TS make your code a lil bit less fancy

  7. #7

    Default Re: HELP - Java Calculator Debug

    Quote Originally Posted by i4chub2x View Post
    taas ra keyu imu code para sa calculator nga app
    Gituyo na nako bro kay grabe kaayo maka demand ug mga methods among teacher. Dapat daw kanang murag calculator sa windows. In fact, kuwang pa daw ug mga operations like sqrt, 1/x, +/-, etc..

    Gusto man gud maka kita among teacher nga nag implement jud mi ug modularity. Haha

    Quote Originally Posted by Badekdek View Post
    kudos to whoever made this.

    anyway, the problem with this is that you didnt pass any value to your subclass and thats why it always results to 0.0.

    overload calculate :

    Code:
      public double calculate() {
        return 0;
      }
      
      public double calculate(double a, double b) {
        return 0;
      }
    pass the values :

    Code:
    public double processOperation(){
         if (operator.equals("+")){
             operate = new Addition();
         }
         else if (operator.equals("-")){
             operate = new Subtraction();
         }
         else if (operator.equals("*")){
             operate = new Multiplication();
         }
         else if (operator.equals("/")){
             operate = new Division();
         }
         else {
             operate = new Modulo();
         }
         
        return operate.calculate(getFirstOperand(),getSecondOperand());
      }
    blah blah blah :

    Code:
    class Addition extends Operation {
          @Override
          public double calculate(double a, double b) {
              return (a + b);
          }
      }
    yeah, @TS make your code a lil bit less fancy
    Thanks bro! I'll revise my codes when I get home.
    I can finally now continue my frustrating project! Haha

  8. #8

    Default Re: HELP - Java Calculator Debug

    unsa ni nga calculator? scientific?
    print screen ang desired output..hehe..

    btaw..awa daw ni:

    simple calculator
    http://adalzieg.webs.com/CalculatorGui.java

    scientific calculator:
    http://adalzieg.webs.com/Calculator.java


    project namo sauna

  9. #9

    Default Re: HELP - Java Calculator Debug

    nag instantiate ka ug new nga object sa imo processOperation so dili makuha ang values sa imo current Operation inig super nimo. Sayop imo pag gamit sa overloading.

    Having many classes doesn't mean mga na modularize gyud nimo imo code. Sobraan ra kaayo asta operation gihimo nimo ug objects. Always remember nga in Java try to reduce creating new objects kay mao ni main factor maka pahinay sa inyo application.

    Operation module alone is enough. Ayaw na himoi pa ug lain. If ako pa ana kay i static method ko na imo operation para mas paspas and dili na ka sige ug create ug new object everytime mag sige ka ug calculate. This way everytime naa kay operator pisliton moingon na lang ka nga Operator.setfirstOperator(value, operator)
    Operator.set2ndOperator(value)
    Operator.calculate()

    Pro if mo insist gyud inyo teacher nga daghanon kaayo inyo classes for the sake nga nag kat.on mo ug Object Oriented and using your existing code. dapat sa imo case 15 kay didto ka nag create ug new Addition, Division etc. pero still its all wrong doing it this way. Mag depend man gud na ang modules if daghan pwede implementation sa imo module but Addition module is always a + b and dili gyud na mo change bisan unsaon pa na nimo so wlay pulos imo i lain pa na object imo Addition.

  10. #10

    Default Re: HELP - Java Calculator Debug

    nyak ahak dugaya na diay ani nga thread oi. lol @_@

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. help... JAVA basic programming!!
    By pakz in forum Programming
    Replies: 17
    Last Post: 09-28-2009, 03:10 PM
  2. HELP: JAVA and NETBEANS
    By rastaman81 in forum Programming
    Replies: 13
    Last Post: 06-01-2009, 10:19 AM
  3. i so need help JAVA and jsp
    By forsaleonly in forum Programming
    Replies: 16
    Last Post: 01-20-2009, 03:19 PM
  4. Need some help - Java Networking
    By stealthghost in forum Networking & Internet
    Replies: 9
    Last Post: 12-22-2008, 07:35 PM
  5. nid help..java programming
    By ronninalpha in forum Programming
    Replies: 3
    Last Post: 09-13-2006, 07:55 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
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top