Answered! Code Completion 11.105 Combo box with functions Complete this function calculator so that it can compute the functions abs, sqrt, and log10…

Code Completion 11.105 Combo box with functions

Complete this function calculator so that it can compute the functions abs, sqrt, and log10. When the user selects a function name in the combo box, update the output label.

Here is a sample program output:

CalcFrame Viewer sqrt 10 abs O X 3.1622776601683795

Complete the following code:

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalcFrame extends JFrame
{
   private JTextField input;
   private JLabel output;
   private JComboBox function;

   public CalcFrame()
   {
      function = new JComboBox();
      // Your work here
      input = new JTextField(10);
      input.setText("10");
      output = new JLabel("Select a function");
      setLayout(new FlowLayout());
      add(function);
      add(input);
      add(output);
   }
}

The following class is used to check your work:

import javax.swing.JFrame;

public class CalcFrameViewer
{
   public static void main(String[] s)
   {
      CalcFrame frame = new CalcFrame();
      frame.setSize(400, 75);
      frame.setTitle("CalcFrameViewer");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

Expert Answer

 JAVA CODE:

import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CalcFrame extends JFrame
{
private JTextField input;
private JLabel output;
private JComboBox function;

public CalcFrame()
{
function = new JComboBox();
function.addItem(“abs()”);
function.addItem(“sqrt()”);
function.addItem(“log10()”);
input = new JTextField(10);
input.setText(“10”);
output = new JLabel(“Select a function”);
setLayout(new FlowLayout());
add(function);
add(input);
add(output);

function.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int num =Integer.parseInt(input.getText());
if (function.getSelectedIndex()==0) {
output.setText(” ” + Math.abs(num));
} else if (function.getSelectedIndex()==1) {
output.setText(” ” + Math.sqrt(num));
} else if (function.getSelectedIndex()==2) {
output.setText(” “+ Math.log10(num));
}
}
});

}
}

OUTPUT:

Answered! Code Completion 11.105 Combo box with functions Complete this function calculator so that it can compute the functions abs, sqrt, and log10... 1

Still stressed from student homework?
Get quality assistance from academic writers!