• 0

have to do this program in java...


Question

I have to do this program in java that converts fahrenheit to celsius and vice versa. I get how to do this, but I need to use 2 classes, and multiple procedures.

I don't really understand why i have to use two classes, but can someone give me an example, because im guessing in the first class you can input something like the fahren to cel conversion, and the second class cel to fahre but this doesn't seem right. I'm going to use the message box to input numbers, and display the numbers in the message panel.

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

When you think about mutltiple classes, think about what the objects / classes should do. In your case, you could code the gui in one class and have a conversion class.

Typically, gui's are programmed across multiple classes as well, as you could also have 2 conversion classes, if not more.

I hope this helps.

Link to comment
Share on other sites

  • 0

Ok I can understand that. Now given that I do a gui in 1, and the conversion in another, how would the conversion class understand the variables from the gui class? Is there a code snippet I would need, because just thinking about it, it wouldn't be possible if I could use the same (input) from the gui class, and convert the (input) in another.

Link to comment
Share on other sites

  • 0
Ok I can understand that. Now given that I do a gui in 1, and the conversion in another, how would the conversion class understand the variables from the gui class? Is there a code snippet I would need, because just thinking about it, it wouldn't be possible if I could use the same (input) from the gui class, and convert the (input) in another.

You would pass them into methods, such as

float FarhenheitToCelsuis(float f, float c)

float CelsuisToFarhenheit(float c, float f)

Link to comment
Share on other sites

  • 0

Hm Im not to familiar with a code like that, is there an easier way of doing this? I know this is probaby to easy for you and if you have coding experience you might overlook basic fundementals when trying to explain..

In class prompt I have

package temp;
import javax.swing.*;



public class fahrenheit {
  public static void main(String[] args) {
  String num1 = JOptionPane.showInputDialog("Please enter the 1st number to be converted");
  String num2 = JOptionPane.showInputDialog("Please enter the 2nd number to be converted");

}
}

Now in class two I called it conversion, the class will have num1 and the math to convert to cel, but how exactly would I pass that into a method?

Link to comment
Share on other sites

  • 0

Building on the previous post...

Imagine you are in your gui class...

You might do something like this...

TempConverter tempConvert = new TempConverter();

float celsius = tempConvert.FarhenheitToCelsuis(88); // 88 would be the f temp

Now in your TempConverter class you would have a method signature similar to this...

public float FarhenheitToCelsuis ( float f ); // where the float after public indicates you are returning a float

As the previous poster indicated, you would have another method for the reverse conversion.

This is why I said you could have multiple classes for something even this simple. Classes should only do what they are supposed to do. One class can do all the conversions or you can have classes for all the conversions along with helper classes if needed.

Link to comment
Share on other sites

  • 0

Why are you taking 2 numbers for input?

You only want to do one conversion at a time right?

For example, I want to convert 80 degrees fah to celsius. I don't need to input 2 numbers. I just return the one I want. Take one number in and have a mechanism for telling which conversion should be done.

This could be done by selecting a checkbox or radiobutton, inputting a c or f into a textfield, or anyway you can think of.

Link to comment
Share on other sites

  • 0

Your conversion class shouldn't show output or take input.

Basically, create a class for the GUI. Then create a class with two methods:

public static float FarToCel(float Far)
public static float CelToFar(float Cel)

The body does the conversions and returns the result. The GUI then uses this value and displays it.

I'm sure this is what your teacher is trying to get you to do... :p

Also, make the conversion methods static. That way you can use them without instantiating the conversion class.

Link to comment
Share on other sites

  • 0

package fahenheittocelsius;
import java.text.DecimalFormat;



public class conversion {
  public conversion(double var1,double var2) {
    double fah2cel = (5.0 / 9) * (var1 - 32);
    double cel2fah = (9.0 / 5) * (var2 + 32);
  }
  public static void main(String[] args) {




  }

}

package fahenheittocelsius;
import javax.swing.*;


public class input {


  public static void main(String[] args) {
    double var1= Double.parseDouble(JOptionPane.showInputDialog("Enter your 1st number"));
    double var2=Double.parseDouble(JOptionPane.showInputDialog("Enter your 2nd number"));
   conversion tempconvert = new conversion(var1, var2);






  }

}

Ok now I tried implementing the decimalformat, but it isn't working for me i dont realy know how to use it right im trying to use ("0.00") format

Link to comment
Share on other sites

  • 0
Ok now I tried implementing the decimalformat, but it isn't working for me i dont realy know how to use it right im trying to use ("0.00") format

I don't see any code using decimalformat, just an import statement. Also, you don't need a main in your conversion class unless you want to make it run by itself.

Link to comment
Share on other sites

  • 0

As far as the decimal format goes, i just did this

double dec = 2.2;
DecimalFormat df = new DecimalFormat("0.00");    
    
System.out.println(df.format(dec));

I'm still confused as to why you are taking in 2 numbers. How do you know which one is fahrenheit and which one is celsius?

You are just asking the user to input a first and second number, but there is no distinction, or am I missing the purpose here? Why don't you have them input a number of one type and then just convert it and output it?

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.