Click to See Complete Forum and Search --> : help with defining methods


hockey87
March 9th, 2010, 06:58 PM
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;
double dblRadius = radius;

// 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();
}
}

dlorde
March 10th, 2010, 04:39 AM
You say "it doesn't work", but you don't give any details about what is supposed to happen, or what actually happens (what goes wrong), so unless someone is prepared to guess what you mean, you may not get much help.

However, looking at the code, you have 4 read-only text fields that you set empty (""), then you try to parse them to a double. An empty string is not parseable to a double, so that will cause a problem.

Programming is an explanatory activity...
R. Harper