Question

please help me debug this Create a GUI for an application that lets the user calculate...

please help me debug this

Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle.

Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides:

alidate the user input so that the user must enter a double value for side A and B of the triangle. To do that, you can use the Validation class presented in chapter 18 to validate the user’s input.

package hypotenuse;
import javax.swing.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Hypotenuse extends JFrame implements ActionListener{
   private JFrame F = new JFrame("Calculate Hypoteneus");

   private JTextField N1 = new JTextField(10);
   private JTextField N2 = new JTextField(10);
   private JTextField Result = new JTextField(10);

   private JLabel A = new JLabel("Side A");
   private JLabel B = new JLabel("Side B");
   private JLabel C1 = new JLabel("Side C(hypoteneus)");

   private JButton Add = new JButton("Solve Hypoteneus");
   private JButton Add1 = new JButton("Solve");
   private JButton Add2 = new JButton("Clear");

   public Hypotenuse()
   {
       JPanel P = new JPanel();
    
       P.setlayout(new FlowLayout(FlowLayout.CENTER, 20,10));
       P.add(A);
       P.add(N1);
       P.add(B);
       P.add(N2);
       P.add(c1);
       Result.setEditable(false);
       P.add(Result);
    
       JPanel P1 = new JPanel();
       P1.setlayout(new FlowLayout());
       P1.add(Add, BorderLayout.EAST);
       P1.add(Add1, BorderLayout.EAST);
       P1.add(Add2, BorderLayout.EAST);
       P1.add(new JButton("Cancel"),BorderLayout.EAST);
       Add.addActionListener(this);
       Add1.addActionListener(this);
       Add2.addActionListener(this);
    
       F.setlayout(new BorderLayout());
       F.add(P, BorderLayout.CENTER);
       F.add(P, BorderLayout.SOUTH);
       F.setVisible(true);
       F.setSize(700,150);
    
       }
       public void actionPerformed(ActionEvent e){
    
           if(e.getSource()==Add)
           {
               double Num1 = (Double.parseDouble(N1.getText()));
               double Num2 = (Double.parseDouble(N2.getText()));
            
               double R = (Num1 * Num1) + (Num2 * Num2);
               R = Math.sqrt(R);
            
               Result.setText(String.valueOf(R));
               N1.setText(String.valueOf(Num1*Num1));
               N2.setText(String.valueOf(Num2*Num2));
            
               N1.setEditable(false);
               N2.setEditable(false);
            
           }
           if(e.getSource()==Add2)
           {
               N1.setEditable(true);
               N2.setEditable(true);
            
               N1.setText(null);
               N2.setText(null);
               Result.setText(null);
           }
       }
       public static void main(String args[])
       {
           new Hypotenuse();
       }
            
   }            
}             

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

You had few mistakes

  • P.setlayout instead of P.setLayout in multiple places wherever you are called setLayout()
  • P.add(c1) instead of P.add(C1)

Please find the screen of program execution and the corrected code below

Hypotenuse.java

import javax.swing.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Hypotenuse extends JFrame implements ActionListener{
    private JFrame F = new JFrame("Calculate Hypoteneus");

    private JTextField N1 = new JTextField(10);
    private JTextField N2 = new JTextField(10);
    private JTextField Result = new JTextField(10);

    private JLabel A = new JLabel("Side A");
    private JLabel B = new JLabel("Side B");
    private JLabel C1 = new JLabel("Side C(hypoteneus)");

    private JButton Add = new JButton("Solve Hypoteneus");
    private JButton Add1 = new JButton("Solve");
    private JButton Add2 = new JButton("Clear");

    public Hypotenuse()
    {
        JPanel P = new JPanel();
        P.setLayout(new FlowLayout(FlowLayout.CENTER, 20,10));
        P.add(A);
        P.add(N1);
        P.add(B);
        P.add(N2);
        P.add(C1);
        Result.setEditable(false);
        P.add(Result);

        JPanel P1 = new JPanel();
        P1.setLayout(new FlowLayout());
        P1.add(Add, BorderLayout.EAST);
        P1.add(Add1, BorderLayout.EAST);
        P1.add(Add2, BorderLayout.EAST);
        P1.add(new JButton("Cancel"),BorderLayout.EAST);
        Add.addActionListener(this);
        Add1.addActionListener(this);
        Add2.addActionListener(this);

        F.setLayout(new BorderLayout());
        F.add(P, BorderLayout.CENTER);
        F.add(P, BorderLayout.SOUTH);
        F.setVisible(true);
        F.setSize(700,150);

    }
    public void actionPerformed(ActionEvent e){

        if(e.getSource()==Add)
        {
            double Num1 = (Double.parseDouble(N1.getText()));
            double Num2 = (Double.parseDouble(N2.getText()));

            double R = (Num1 * Num1) + (Num2 * Num2);
            R = Math.sqrt(R);

            Result.setText(String.valueOf(R));
            N1.setText(String.valueOf(Num1*Num1));
            N2.setText(String.valueOf(Num2*Num2));

            N1.setEditable(false);
            N2.setEditable(false);

        }
        if(e.getSource()==Add2)
        {
            N1.setEditable(true);
            N2.setEditable(true);

            N1.setText(null);
            N2.setText(null);
            Result.setText(null);
        }
    }
    public static void main(String args[])
    {
        new Hypotenuse();
    }

}

Thank you,

Good Luck !!!

Add a comment
Know the answer?
Add Answer to:
please help me debug this Create a GUI for an application that lets the user calculate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

  • Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here...

    Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...

  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import...

    Simple java GUI language translator. English to Spanish, French, or German import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class translatorApp extends JFrame implements ActionListener {    public static final int width = 500;    public static final int height = 300;    public static final int no_of_lines = 10;    public static final int chars_per_line = 20;    private JTextArea lan1;    private JTextArea lan2;    public static void main(String[] args){        translatorApp gui = new translatorApp();...

  • Convert the following GUI application into an Applet. Do not delete any part of the GUI...

    Convert the following GUI application into an Applet. Do not delete any part of the GUI program, just comment out the part of the program not required. Another Java code is required // Java code: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class PizzaShopping extends JFrame { JCheckBox ckb[]; JLabel jlb1; JRadioButton radio[], radio1[]; static String[] pizzaToppings = "Tomato,Green peppper,Black olives,Mushrooms,Extra cheese,Pepproni,Sausage" .split(","); static String[] pizzaSizes = "Small: $6.50, Medium: $8.50, Large: $10".split(","); static String[] pizzatypes = "Thin Crust, Mediam...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • Can you help me to link two frames in Java Swing and then if you click...

    Can you help me to link two frames in Java Swing and then if you click "Proceed button" it will link to the Second Frame...Thank you :) First frame code: //First import javax.swing.*; import java.awt.event.*; import java.io.*; public class Sample extends JFrame implements ActionListener { JMenuBar mb; JMenu file; JMenuItem open; JTextArea ta; JButton proceed= new JButton("Proceed"); Sample() { open = new JMenuItem("Open File"); open.addActionListener(this); file = new JMenu("File"); file.add(open); mb = new JMenuBar(); mb.setBounds(0, 0, 400, 20); mb.add(file); ta...

  • import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel...

    import javax.swing.*; import java.awt.event.*; import java.awt.*; public class BookReview extends JFrame implements ActionListener {       private JLabel titleLabel;       private JTextField titleTxtFd;       private JComboBox typeCmb;       private ButtonGroup ratingGP;       private JButton processBnt;       private JButton endBnt;       private JButton clearBnt;       private JTextArea entriesTxtAr;       private JRadioButton excellentRdBnt;       private JRadioButton veryGoodRdBnt;       private JRadioButton fairRdBnt;       private JRadioButton poorRdBnt;       private String ratingString;       private final String EXCELLENT = "Excellent";       private final String VERYGOOD = "Very Good";       private final String FAIR = "Fair";       private final String POOR = "Poor";       String...

  • Can someone modify my code so that I do not get this error: Exception in thread...

    Can someone modify my code so that I do not get this error: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1043) at java.awt.Container.add(Container.java:363) at RatePanel.<init>(RatePanel.java:64) at CurrencyConverter.main(CurrencyConverter.java:16) This code should get the amount of money in US dollars from user and then let them select which currency they are trying to convert to and then in the textfield print the amount //********************************************************************* //File name: RatePanel.java //Name: Brittany Hines //Purpose: Panel for a program that convers different currencyNamerencies to use dollars //*********************************************************************...

  • I need help with the get quote sql that is all Build a rate quote GUI....

    I need help with the get quote sql that is all Build a rate quote GUI. The system will first allow us to select a customer (assume they are already in the DB), an employee giving the quote, and then select part(s) and/or motorcycles. You system should also include the shipping information in the quote. After all of the items have been selected for the rate quote, we should give the user a “bottom line” total that includes shipping (we...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT