Can anybody help me out here?
My final is tomorrow and I still have another assignment to complete. I know that's not your problem, just looking for some help with this TemperatureDemo. A few hints would help.
I think I have to invoke the following methods to get the result the assignment calls for:
PHP Code:
// This is comparing statements to see if the first is greater than the second
public boolean greaterThan(Temperature obj) {
return (getCelsius() > obj.getCelsius());
}
// This is comparing statements to see if the first is less than the second
public boolean lessThan(Temperature obj) {
return (getCelsius() < obj.getCelsius());
}
// Returns temp value
public double getTemperatureTemp() {
return temp;
}
// Returns scale character
public char getTemperatureScale() {
return scale;
}
Am I close?
Thanks,
~Jo
Calculator Class - Mathematical Equations???
I have modified this program from a simple "adding machine" and changed it to perform subtraction, multiplication & division however I have not studied any mathematical equations yet. Does anyone know how to make the Subtract, Multiply & Divide buttons work properly on this application?
PHP Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
GUI for adding, subtractin, multiplying and dividing
a series of numbers.
*/
public class Calculator extends JFrame implements ActionListener
{
public static final int WIDTH = 400;
public static final int HEIGHT = 200;
private JTextField inputOutputField;
private double sum = 0;
private double difference = 0;
private double product = 0;
private double quotient = 0;
public static void main(String[] args)
{
Calculator guiCalculator = new Calculator( );
guiCalculator.setVisible(true);
}
public Calculator( )
{
setTitle("Simple Calculator");
addWindowListener(new WindowDestroyer( ));
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane( );
contentPane.setLayout(new BorderLayout( ));
JPanel buttonPanel = new JPanel( );
buttonPanel.setBackground(Color.GRAY);
buttonPanel.setLayout(new FlowLayout( ));
JButton addButton = new JButton("Add");
addButton.addActionListener(this);
buttonPanel.add(addButton);
JButton subtractButton = new JButton("Subtract");
subtractButton.addActionListener(this);
buttonPanel.add(subtractButton);
JButton multiplyButton = new JButton("Multiply");
multiplyButton.addActionListener(this);
buttonPanel.add(multiplyButton);
JButton divideButton = new JButton("Divide");
divideButton.addActionListener(this);
buttonPanel.add(divideButton);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
JPanel textPanel = new JPanel( );
textPanel.setBackground(Color.BLUE);
textPanel.setLayout(new FlowLayout( ));
inputOutputField = new JTextField("Numbers go here.", 30);
inputOutputField.setBackground(Color.WHITE);
textPanel.add(inputOutputField);
contentPane.add(textPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand( ).equals("Add"))
{
sum = sum +
stringToDouble(inputOutputField.getText( ));
inputOutputField.setText(Double.toString(sum));
}
else if (e.getActionCommand( ).equals("Subtract"))
{
difference =
stringToDouble(inputOutputField.getText());
inputOutputField.setText(Double.toString(difference));
}
else if (e.getActionCommand( ).equals("Multiply"))
{
product =
stringToDouble(inputOutputField.getText( ));
inputOutputField.setText(Double.toString(product));
}
else if (e.getActionCommand( ).equals("Divide"))
{
quotient =
stringToDouble(inputOutputField.getText( ));
inputOutputField.setText(Double.toString(quotient));
}
else if (e.getActionCommand( ).equals("Reset"))
{
sum = 0;
inputOutputField.setText("0.0");
}
else
inputOutputField.setText("Error in adder code.");
}
private static double stringToDouble(String stringObject)
{
return Double.parseDouble(stringObject.trim( ));
}
}