Link to home
Start Free TrialLog in
Avatar of vipa2k
vipa2k

asked on

my method won't work right

my calculate method is acting up ... for example i can't use the fahrenheit conversion proberly... when I click nothing happens... what did i do wrong ?



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class calc{
      public static void main (String [] args){
            screen go = new screen();
            go.show();
      }
}

class screen extends JFrame {
      //The overall Frame
      public screen(){
            setTitle("conversion calculator");
            setSize(WIDTH, HEIGHT);
      //declaring the number block      
            buttons panel = new buttons();

      //the Content screen
            Container contentPane = getContentPane();
            contentPane.add(panel);

      }
      public static final int WIDTH = 800;
      public static final int HEIGHT = 200;
}

class buttons extends JPanel{
      public buttons(){
            setLayout(new BorderLayout());
            
            result = 0;
            lastCommand = "=";
            start = true;
            
            
            
            
            // display the text field
            display = new JTextField("0");
            display.setEditable(false);
            add(display, BorderLayout.CENTER);
            
            ActionListener insert = new insertAction();
            ActionListener command = new CommandAction();
            
            
            // conversion function layout
            
            
            panel = new JPanel();
            panel.setLayout(new GridLayout(4,3));
            
            
            
            addButton("Celsius", command);
            addButton("In", insert);
            addButton("mm", insert);
            
            addButton("Fahrenheit", command);
            addButton("Ft", insert);
            addButton("cm", insert);
            
            addButton("Clear", command);
            addButton("Yd", insert);
            addButton("m", insert);
            
            addButton("Convert", command);
            addButton("Mi", insert);
            addButton("km", insert);
            
            add(panel, BorderLayout.EAST);
            
            
            
            
            // grid layout
            panel = new JPanel();
            panel.setLayout(new GridLayout(4,4));
            
            addButton("7", insert);
            addButton("8", insert);
            addButton("9", insert);
            addButton("/", command);
            
            addButton("4", insert);
            addButton("5", insert);
            addButton("6", insert);
            addButton("*", command);
            
            addButton("1", insert);
            addButton("2", insert);
            addButton("3", insert);
            addButton("-", command);
            
            addButton("0", insert);
            addButton(".", insert);
            addButton("=", command);
            addButton("+", command);
            
            add(panel, BorderLayout.WEST);
            
      }
      private void addButton(String label, ActionListener listener){
            JButton button = new JButton(label);
            button.addActionListener(listener);
            panel.add(button);
      }
      
      private class insertAction implements ActionListener{
            public void actionPerformed(ActionEvent event){
                  String input = event.getActionCommand();
                  if (start){
                        display.setText("");
                        start = false;
                  }
                  display.setText(display.getText() + input);
            }
      }
      private class CommandAction implements ActionListener{
            public void actionPerformed(ActionEvent evt){
                  String command = evt.getActionCommand();
                  if (start){
                        if (command.equals("-")){
                              display.setText(command);
                              start = false;
                        }
                        else
                              lastCommand = command;
                  }
                  else{
                        calculate(Double.parseDouble(display.getText()));
                        lastCommand = command;
                        start = true;
                  }
            }
      }
      public void calculate(double x){
            if (lastCommand.equals("+")) result += x;
            else if (lastCommand.equals("-")) result -= x;
            else if (lastCommand.equals("*")) result *= x;
            else if (lastCommand.equals("/")) result /= x;
            else if (lastCommand.equals("=")) result = x;
            else if (lastCommand.equals("Celsius")) result = (5 / 9) * (x - 32);
            else if (lastCommand.equals("Fahrenheit")) result = (9 / 5) * (x + 32);
            else if (lastCommand.equals("Clear")) result = 0;
            
            
            display.setText("" + result);
      }
      private JTextField display;
      private JPanel panel;
      private JPanel panel2;
      private double result;
      private String lastCommand;
      private boolean start;
}
ASKER CERTIFIED SOLUTION
Avatar of RomanRega
RomanRega

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smithberry
smithberry

To follow on from the previous response I think the correct formula for Celsius / Fahrenheit conversions is actually:

result = (9.0d / 5.0d * x) + 32.0d;

that is the addition part is done after the other parts of the conversion.

Hope that is some help.