I'm taking an object oriented programming class and I'm having some difficulty understanding how to build a circle object that lets the user declare what the radius is.
I created a data class and in it I put my instance variable, my getter and setter methods, my constructor, and then the basic computational function methods to compute the area and perimeter of the circle with a given radius. Here is that class:
package shapesoo;
public class CircleDataClass {
private double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public CircleDataClass(double radius) {
this.radius = radius;
}
public double getArea(){
return Math.PI * radius * radius;
}
public double getCircumference(){
return 2 * Math.PI * radius;
}
}
Then, I am creating a test class that builds the circle with the given radius and in my main method I create the new circle object with:
CircleDataClass myCircle = new CircleDataClass(radius);
I don't have radius declared anywhere in this test class so that's why I am getting a run-time error. But what I want is a user to input the value for that radius parameter that I have in my constructor and then have that radius passed to this circle object. Do I create a separate method in my main class that asks for the value of the radius? I think I am getting confused with what getters/setters/cosntructors are doing and how to pass the radius variable around to different classes.
EDIT: If I put this in, is the instance variable from my data class even used?
public static void main(String[] args) {
String shapeType = getShape();
if (shapeType.equalsIgnoreCase("Circle")){
String r = JOptionPane.showInputDialog("What is the radius: ");
double radius = Double.parseDouble(r);
CircleDataClass myCircle = new CircleDataClass(radius);
}
}
I know how to do this without using object-oriented principles and I am aware this must seem elementary to many of you but I would appreciate any help on it.
Aucun commentaire:
Enregistrer un commentaire