package Lab9;

import java.math.BigDecimal;
import java.math.RoundingMode;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Lab9 extends JFrame {

    Lab9() {
        // set frame title
        this.setTitle("temperature converter");
        // set frame bounds
        // ww: set size
        this.setPreferredSize(new Dimension(1000, 200));
        // ww: center the window
        this.setLocationRelativeTo(null);
        // set frame layout
        //
        // TODO
        this.setLayout(new FlowLayout());

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        JFrame frame = new Lab9();

        JTextField fahrenheitInput = new JTextField("", 10);
        JButton convertFromFahrenheitButton = new JButton("convert");
        JTextField celsiusInput = new JTextField("", 10);
        JButton convertFromCelsiusButton = new JButton("convert");

        frame.add(new JLabel("Fahrenheit"));
        frame.add(fahrenheitInput);
        frame.add(convertFromFahrenheitButton);
        frame.add(new JLabel("Celsius"));
        frame.add(celsiusInput);
        frame.add(convertFromCelsiusButton);
        // create labels, textfields, and buttons
        // TODO
        //
        // add them to the frame
        // TODO
		
        // create listener for fahrenheit to celsius conversion
        ActionListener f2c = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    BigDecimal x;
                    try {
                        x = new BigDecimal(fahrenheitInput.getText().trim());
                    } catch(NumberFormatException exn) {
                        celsiusInput.setText("undefined");
                        return;
                    }
                    BigDecimal y = x.subtract(new BigDecimal(32))
                        .multiply(new BigDecimal(5))
                        .divide(new BigDecimal(9), RoundingMode.HALF_UP);
                    celsiusInput.setText(y.toPlainString());
                }
            };
        convertFromFahrenheitButton.addActionListener(f2c);
        fahrenheitInput.addActionListener(f2c);


        ActionListener c2f = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    BigDecimal x;
                    try {
                        x= new BigDecimal(celsiusInput.getText().trim());
                    } catch(NumberFormatException exn) {
                        fahrenheitInput.setText("undefined");
                        return;
                    }
                    BigDecimal y = x.multiply(new BigDecimal(9))
                        .divide(new BigDecimal(5), RoundingMode.HALF_UP)
                        .add(new BigDecimal(32));
                    fahrenheitInput.setText(y.toPlainString());
                }
            };
        convertFromCelsiusButton.addActionListener(c2f);
        celsiusInput.addActionListener(c2f);
        
        // add listener f2c to the right button
        // TODO
		
        // create listener for celsius to fahrenheit conversion
        // add listener c2f to the right button
        // TODO
		
        // make frame visible
        // TODO
        frame.pack();
        frame.setVisible(true);
    }
}

@@ -1706,36 +1706,32 @@
  %7B%0D%0A            
-    
 public void acti
@@ -1768,36 +1768,32 @@
                 
-    
 BigDecimal x;%0D%0A 
@@ -1799,36 +1799,32 @@
                 
-    
 try %7B%0D%0A         
@@ -1834,20 +1834,16 @@
         
-  
-  
 x = new 
@@ -1897,36 +1897,32 @@
                 
-    
 %7D catch(NumberFo
@@ -1955,36 +1955,32 @@
                 
- 
-   
 celsiusInput.set
@@ -1991,36 +1991,32 @@
 (%22undefined%22);%0D%0A
-    
                 
@@ -2040,35 +2040,27 @@
             
-    %7D%0D%0A    
+%7D%0D%0A
             
@@ -2130,20 +2130,16 @@
         
-    
 .multipl
@@ -2172,36 +2172,32 @@
                 
-  
-  
 .divide(new BigD
@@ -2227,28 +2227,24 @@
 .HALF_UP);%0D%0A
-    
             
@@ -2297,35 +2297,27 @@
             
-    %7D%0D%0A    
+%7D%0D%0A
         %7D;%0D%0A
@@ -2487,36 +2487,32 @@
  %7B%0D%0A            
-    
 public void acti
@@ -2549,36 +2549,32 @@
                 
-    
 BigDecimal x;%0D%0A 
@@ -2580,36 +2580,32 @@
                 
-    
 try %7B%0D%0A         
@@ -2615,20 +2615,16 @@
         
-  
-  
 x= new B
@@ -2674,36 +2674,32 @@
                 
-    
 %7D catch(NumberFo
@@ -2732,36 +2732,32 @@
                 
- 
-   
 fahrenheitInput.
@@ -2771,36 +2771,32 @@
 (%22undefined%22);%0D%0A
-    
                 
@@ -2820,35 +2820,27 @@
             
-    %7D%0D%0A    
+%7D%0D%0A
             
@@ -2901,36 +2901,32 @@
                 
-    
 .divide(new BigD
@@ -2979,20 +2979,16 @@
         
-  
-  
 .add(new
@@ -3002,28 +3002,24 @@
 imal(32));%0D%0A
-    
             
@@ -3075,35 +3075,27 @@
             
-    %7D%0D%0A    
+%7D%0D%0A
         %7D;%0D%0A

Fix anonymous class idents