• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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;
}
0
vipa2k
Asked:
vipa2k
1 Solution
 
RomanRegaCommented:
There are two main issue.
The first one is that you cannot do the conversion like this:
result = (5 / 9) * (x - 32);

5 and 9 are intrepeted as Int, so you get a Int as result, which is 0 in this case, beaing 5 smallr than 9.
You need to be more explicit about the kind of data:

result = (5.0 / 9.0) * (x - 32.0);
or better
result = (5d / 9d) * (x - 32d); //The d stand for double (you can use f for float too)

The second issue is that the operation you have to press Celsius before to enter the number to convert, ans then press =, like this:
"Celsius" 100 =

I dont know if that is exacly what you want. But maybe is not. In that case you need to work on the design concept of your work a little more.

Hope i've been of help


0
 
smithberryCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now