Question
A tutor sent me this code but it is not working for me. Can some please tell me whats wrong with the code. String sideA,
A tutor sent me this code but it is not working for me. Can some please tell me whats wrong with the code.
String sideA, sideB;
sideA=txt_sidea.getText();
sideB=txt_sideb.getText();
Double hypotenuse;
if(!sideA.isEmpty() && !sideB.isEmpty())
{
try
{
//find the square root of (side1^2 + side2^2)
hypotenuse = Math.sqrt(Math.pow(Double.parseDouble(sideA), 2) + Math.pow(Double.parseDouble(sideB), 2));
txt_sidec.setText(hypotenuse.toString());
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,"Side A must be a number Side B must be a number", "Invalid data", JOptionPane.ERROR_MESSAGE);
}
}
else
{
JOptionPane.showMessageDialog(null,"Enter Side A and Side B", "Invalid data", JOptionPane.ERROR_MESSAGE);
}
The whole code is below
Explanation:
import javax.swing.JOptionPane;
public class mainForm extends javax.swing.JFrame {
public mainForm() {
initComponents();
}
@SuppressWarnings("unchecked")
//
private void initComponents() {
txt_sidea = new javax.swing.JTextField();
txt_sideb = new javax.swing.JTextField();
txt_sidec = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
btn_calc = new javax.swing.JButton();
btn_exit = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Side A");
jLabel2.setText("Side B");
jLabel3.setText("Side C");
btn_calc.setText("Calculate");
btn_calc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_calcActionPerformed(evt);
}
});
btn_exit.setText("Exit");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(50, 50, 50)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
The question is
i need help with creat ing a GUI for an that lets the user calculate the hypotenuse of a right triangle.
The GUI with valid data
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started