I think my problem is I am treating the char as if it were an int, you are probably supposed to do it a different way, but I am new to java, and I just don't know yet what I am supposed to do. Here is the assignment and the code. Any help/knowledge is greatly appreciated.
A Temperature class that has two parameters, a temperature value (floating-point) number and a character for the scale, either 'C' or 'F'.
Class will have four constructor methods, one for each instance variable (assume zero degrees if no value is specified and C if no scale is specified, one with two parameters for the two instance variables, and a default constructor (set to zero degrees Celsius).
Class will also have two accesor methods, one to return the temperature in C and one to return the temperature in F, using the formulas from the textbook, and round to the nearest tenth of a degree.
It will have three reset methods, one to set the value, one to set the scale ('F' or 'C') and one to set both.
It will have three comparison methods, one to test whether two temperatures are equal, one to test whether one temperature is greater than another, and one to test whether one temperature is less than another.
A suitable toString() method.
A driver program that tests all the methods.
Use each of the constructors, include at least one true and one false case for each of th comparison methods, and test at least the following temperature equalities: 0.0 degrees C = 32.0 degrees F, -40.0 degrees C = -40.0 degrees F, and 100.0 degrees C = 212 degrees F.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Temp1 extends JFrame implements ActionListener {
private JTextField jtfNum, jtfCF, jtfResult, jtfResultCF;
private JButton jbtCelF, jbtFahC, jbtClear, jbtClearBoth, jbtClearNum, jbtClearCF, jbtClose;
private float num1, num2;
private char cf;
public static void main(String [] args) {
Temp1 temp = new Temp1();
temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
temp.setSize(800,100);
temp.setTitle("Temperature C/F Converter");
temp.setVisible(true);
}
public Temp1() {
JPanel p1 = new JPanel();
p1.setLayout( new FlowLayout());
p1.add(new JLabel("Degrees : "));
p1.add(jtfNum = new JTextField(10));
p1.add(new JLabel("C/F : "));
p1.add(jtfCF = new JTextField(2));
p1.add(new JLabel("Result : "));
p1.add(jtfResult = new JTextField(10));
jtfResult.setEditable(false);
p1.add(jtfResult = new JTextField(2));
jtfResultCF.setEditable(false);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout());
p2.add(jbtCelF = new JButton("Convert to Fahrenheit"));
p2.add(jbtFahC = new JButton("Convert to Celsius"));
p2.add(jbtClose = new JButton("Exit Program"));
p2.add(jbtClearNum = new JButton("Clear Degrees"));
p2.add(jbtClearCF = new JButton("Clear C/F"));
p2.add(jbtClearBoth = new JButton("Clear Degrees and C/F"));
p2.add(jbtClear = new JButton("Clear All"));
getContentPane().setLayout( new BorderLayout());
getContentPane().add(p1, BorderLayout.NORTH);
getContentPane().add(p2, BorderLayout.CENTER);
jbtCelF.addActionListener(this);
jbtFahC.addActionListener(this);
jbtClearNum.addActionListener(this);
jbtClearCF.addActionListener(this);
jbtClearBoth.addActionListener(this);
jbtClear.addActionListener(this);
jbtClose.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if (e.getSource() instanceof JButton) {
if ("Convert to Fahrenheit".equals(actionCommand))
calculate1();
if ("Convert to Celsius".equals(actionCommand))
calculate2();
else
if ("Clear All".equals(actionCommand)){
jtfCF.setText(" ");
jtfNum.setText(" ");
jtfResult.setText(" ");
}
if ("Clear Degrees and C/F".equals(actionCommand)){
jtfCF.setText(" ");
jtfNum.setText(" ");
}
if ("Clear Degrees".equals(actionCommand)){
jtfNum.setText(" ");
}
if ("Clear C/F".equals(actionCommand)){
jtfCF.setText(" ");
}
}
else
if ("Exit Program".equals(actionCommand))
System.exit(0);
}
private void calculate1() {
float celfah = ((5/9)*(num1 - 32));
float num1 = (Float.parseFloat(jtfNum.getText().trim()));
char cf = (Char.parseChar(jtfCF.getText.trim()));
if (num1 == ())
jtfNum.setText("0");
if (cf == ())
jtfCF.setText("C");
if (cf == "F")
jtfResultCF.setText("F");
jtfResult.setText(" + num 1 + ");
if (cf == "C")
jtfResult.setText(" + celfah + ");
jtfResultCF.setText("F");
}
private void calculate2() {
float fahcel = ((9/5)*(num2) + 32);
float num2 = (Float.parseFloat(jtfNum.getText().trim()));
char cf = (Char.parseChar(jtfCF.getText.trim()));
if (num1 == ())
jtfNum.setText("0");
if (cf == ())
jtfCF.setText("C");
if (cf == ("C"))
jtfResultCF.setText("C");
jtfResult.setText(" + num2 + ");
if (cf == "F")
jtfResult.setText(" + fahcel + ");
jtfResultCF.setText("C");
}
}