java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class HexBinCalculator
{
   public static void main(String[] args)
   {
      EventQueue.invokeLater(new Runnable()
         {
            public void run()
            {
               CalculatorFrame frame = new CalculatorFrame();
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
               frame.setVisible(true);
            }
         });
   }
}

/**
 * A frame with a calculator panel.
 */
class CalculatorFrame extends JFrame
{
   public CalculatorFrame()
   {
      setTitle("Hex to Binary Calculator Calculator.");
      HexBinPanel panel = new HexBinPanel();
      add(panel);
      pack();
   }
}

/**
 * A panel with calculator buttons and a result display.
 */
class HexBinPanel extends JPanel
{
   public HexBinPanel()
   {
	    setLayout(new BorderLayout());
	   
	  result = 0;
      lastCommand = "=";
      start = true;

      // add the display

      display = new JButton("0");
      display.setEnabled(false);
      add(display, BorderLayout.NORTH);

      ActionListener insert = new InsertAction();
      ActionListener command = new CommandAction();

     
      panel = new JPanel();
      ButtonGroup radioBInput= new ButtonGroup();
      JRadioButton   octIn = new JRadioButton("Oct In", false);
      JRadioButton hexIn = new JRadioButton("Hex In",  true);
      JRadioButton decIn = new JRadioButton("Dec In",false);
      radioBInput.add(octIn);
      radioBInput.add(hexIn);
      radioBInput.add(decIn);
      
      octIn.addActionListener(new ActionListener(){     	  
    	  		public void actionPerformed(ActionEvent e){  inputType = 8;}      }			);
      hexIn.addActionListener(new ActionListener(){     	  
	  		public void actionPerformed(ActionEvent e){  inputType = 16;}      }			);
      decIn.addActionListener(new ActionListener(){     	  
	  		public void actionPerformed(ActionEvent e){  inputType = 10;}      }			);
      
      panel.add(octIn);
      panel.add(hexIn);
      panel.add(decIn);
 //     now the output
      ButtonGroup radioBOutput= new ButtonGroup();
      
      JRadioButton octOut = new JRadioButton("Oct Out", false);
      JRadioButton hexOut= new JRadioButton("Hex Out", false);
      JRadioButton decOut = new JRadioButton("Dec Out",false);
      JRadioButton binOut = new JRadioButton("Binary Out",true);
      //
      octOut.addActionListener(new ActionListener(){     	  
	  		public void actionPerformed(ActionEvent e){  outputType = 8;}      }			);
      hexOut.addActionListener(new ActionListener(){     	  
		public void actionPerformed(ActionEvent e){  outputType = 16;}      }			);
	decOut.addActionListener(new ActionListener(){     	  
		public void actionPerformed(ActionEvent e){  outputType = 10;}      }			);
	binOut.addActionListener(new ActionListener(){     	  
		public void actionPerformed(ActionEvent e){  outputType = 2;}      }			);

	radioBOutput.add(octOut);
	radioBOutput.add(hexOut);
	radioBOutput.add(decOut);
	radioBOutput.add(binOut);
	
	panel.add(octOut);
      panel.add(hexOut);
      panel.add(decOut);
      panel.add(binOut);
      
      panel.setLayout(new GridLayout(8, 3));

      //addButton(" ", insert);
      //addButton(" ", insert);
      addButton("F", insert);
      addButton("E", insert);
      addButton("D", insert);

      addButton("C", insert);
      addButton("B", insert);
      addButton("A", insert);

      addButton("9", insert);
      addButton("8", insert);
      addButton("7", insert);

      addButton("4", insert);
      addButton("5", insert);
      addButton("6", insert);

      addButton("3", insert);
      addButton("2", insert);
      addButton("1", insert);
     
      addButton("0", insert);
      addButton("/", command);
      addButton("-", command);
      
     //addButton(".", insert);
      addButton("*", command);
      addButton("=", command);
      addButton("+", command);

      add(panel, BorderLayout.CENTER);
   }

   /**
    * Adds a button to the center panel.
    * @param label the button label
    * @param listener the button listener
    */
   private void addButton(String label, ActionListener listener)
   {
      JButton button = new JButton(label);
      button.addActionListener(listener);
      panel.add(button);
   }

   /**
    * This action inserts the button action string to the end of the display text.
    */
   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);
      }
   }

   /**
    * This action executes the command that the button action string denotes.
    */
   private class CommandAction implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         String command = event.getActionCommand();

         if (start)
         {
            if (command.equals("-"))
            {
               display.setText(command);
               start = false;
            }
            else lastCommand = command;
         }
         else
         {
            calculate(Double.parseDouble(display.getText()));
            lastCommand = command;
            start = true;
         }
      }
   }

   /**
    * Carries out the pending calculation.
    * @param x the value to be accumulated with the prior result.
    */
   
//***********	STUDENTS  HERE IS WHERE YOU DO THE WORK   
     public void calculate(double x)
   {
    // NOTE: Result is a double
    
    System.out.println("Input  :  "+ inputType) ;
     System.out.print(", Output :  "+ outputType) ;
     
     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;
      display.setText("" + result);
   }
     
   private int inputType = 16; 
   private int outputType = 2;
   private JButton display;
   private JPanel panel;
   private double result;
   private String lastCommand;
   private boolean start;
}
