Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: methods help

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default methods help

    I need to write a program that prompts the user to enter 4 integers (x1, x2, y1, y2) and the program should output the circles area, diameter, circumference, and area. I have to create these methods, distance, radius, circumference and area.

    I have the code done but everytime i enter the integers, it doesnt work. Can anybody help me with this?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
     
    public class Circle extends JFrame {
     
       // declare variables
       private double radius = 0, circ = 0, diameter = 0, area = 0, distance = 0; 
       private double X1, X2, Y1, Y2;
       private ButtonHandler buttonHandler;
     
     
       // declare GUI components
       JLabel lX1, lX2, lY1, lY2, lWelcome, lRadius, lCirc, lDiameter, lArea;
       JTextField tX1, tX2, tY1, tY2, tRadius, tCirc, tDiameter, tArea;
       JButton bReset, bCompute;
       Container c;
       JPanel pCenter;
     
       //set and get methods
       public double distance (double X1, double X2, double Y1, double Y2)
       {
       	distance =  Math.sqrt((X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1));
       	return distance;
       }
     
       public double radius (double X1, double Y1, double X2, double Y2)
       {
    	return distance(X1, Y1, X2, Y2);
       }
     
       public double circ (double radius)
       {
    	circ = 2*Math.PI * radius;
    	return circ;
       }
     
       public double area (double radius)
       {
    	area = Math.PI * radius * radius; 
    	return area;
       }   
       {
     
     
     
     
             //create GUI components
             lX1 = new JLabel("Enter X1:");
             lX2 = new JLabel("Enter X2:");
             lY1 = new JLabel("Enter Y1:");
             lY2 = new JLabel("Enter Y2:");
             lRadius = new JLabel("Radius =");
             lCirc = new JLabel("Circumference =");
             lDiameter = new JLabel("Diameter =");
             lArea = new JLabel("Area =");
             lWelcome = new JLabel("Circle", SwingConstants.CENTER);
     
             tX1 = new JTextField("");
             tX2 = new JTextField("");
             tY1 = new JTextField("");
             tY2 = new JTextField("");
             tRadius = new JTextField("");
             tCirc = new JTextField("");
             tDiameter = new JTextField("");
             tArea = new JTextField("");
     
             tX1.setEditable(true);
             tX2.setEditable(true);
             tY1.setEditable(true);
             tY2.setEditable(true);
             tRadius.setEditable(false);
             tCirc.setEditable(false);
             tDiameter.setEditable(false);
             tArea.setEditable(false);
     
             bReset = new JButton("Reset");
             bCompute = new JButton("Compute");
     
             c = getContentPane();
             pCenter = new JPanel();
       }
     
             public Circle() {
             //add GUI components
             pCenter.setLayout(new GridLayout(9,2));
             pCenter.add(lX1);
             pCenter.add(tX1);
             pCenter.add(lX2);
             pCenter.add(tX2);
             pCenter.add(lY1);
             pCenter.add(tY1);
             pCenter.add(lY2);
             pCenter.add(tY2);
             pCenter.add(lRadius);
             pCenter.add(tRadius);
             pCenter.add(lCirc);
             pCenter.add(tCirc);
             pCenter.add(lDiameter);
             pCenter.add(tDiameter);
             pCenter.add(lArea);
             pCenter.add(tArea);
             pCenter.add(bReset);
             pCenter.add(bCompute);
     
             c.setLayout(new BorderLayout());
             c.add(lWelcome, BorderLayout.NORTH);
             c.add(pCenter, BorderLayout.CENTER);
     
             //register with buttons
             buttonHandler = new ButtonHandler();
             bReset.addActionListener(buttonHandler);
             bCompute.addActionListener(buttonHandler);
     
             //render the window
             setTitle("Circle Info");
             setSize(500, 300);
             setLocation(100, 100);
             setResizable(false);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             setVisible(true);
       }
     
       private class ButtonHandler implements ActionListener {
          public void actionPerformed(ActionEvent ae) {
             if (ae.getSource().equals(bReset)) {
                tX1.setText("");
                tX2.setText("");
                tY1.setText("");
                tY2.setText("");
                tRadius.setText("");
                tCirc.setText("");
                tDiameter.setText("");
                tArea.setText("");
             }
             else {
                 try {
                   X1 = Double.parseDouble(tX1.getText());
                   X2 = Double.parseDouble(tX2.getText());
                   Y1 = Double.parseDouble(tY1.getText());
                   Y2 = Double.parseDouble(tY2.getText());
                   radius = Double.parseDouble(tRadius.getText());
                   circ = Double.parseDouble(tCirc.getText());
                   diameter = Double.parseDouble(tDiameter.getText());
                   area = Double.parseDouble(tArea.getText());
                 }
     
     
                  catch (Exception e) {
                       JOptionPane.showMessageDialog(null, "Invalid input, please try again");
                       return;
                     }
             }
          }
       }          
     
     public static void main(String[] args) {
           Circle myApp = new Circle();
       } 
     }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: methods help

    Your action listener is trying to read in the radius, diameter, circumference, and area of the circle rather than computing the values and setting the text. Use the setText() method to set the text once you have computed to desired quantities.

    Also, if you're suppose to enter integers, it would be better to use the Integer.parseInt() method rather than the Double.parseDouble() method.

Similar Threads

  1. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM
  2. Help with GUIs please & methods
    By killerknight141 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 07:12 AM
  3. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM
  4. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  5. Working with Methods
    By duckman in forum Object Oriented Programming
    Replies: 3
    Last Post: November 9th, 2009, 08:27 PM