1

I'm not really sure how to word the title nor this but I'll try to be as specific as I can. I have created a class that contains a constructor that allows me to create objects of "Circle" type, and this circle has an argument radius. Here is that class:

public class Circle {
    private double radius;

    public Circle(double radius) {
        this.radius = radius; // this.radius refers to the instance variable above
    }

    public Circle() {
    }

    public double getArea() {
        double area = radius * radius * Math.PI;
        return area;
    }

    public double getCircumference() {
        double circumference = 2 * Math.PI * radius; 
        return circumference;
    }
}

I think the methods contained in this code are straight forward. I create an object given a radius, and it computes the circumference and area.

I also have another class, CircleTester, where I have created 2 objects of Circle type, c1 and c2, with a radius of 5 and 10, respectively.

public class CircleTester {
    public static void main (String[] args) {
        Circle c1 = new Circle(5);
        double circumference1 = c1.getCircumference(); 
        double area1 = c1.getArea();
        System.out.println("Radius of c1: " + c1.radius);
        System.out.printf("The circle c1 with circumference %.3f has area %.3f%n", circumference1, area1);

        Circle c2 = new Circle(10); 
        double area2 = c2.getArea(); //objectReference.methodName()
        double circumference2 = c2.getCircumference(); //objectReference.methodName()
        System.out.println("Radius of c2: " + c2.radius);
        System.out.printf("The circle c2 with circumference %.3f has area %.3f%n", circumference2, area2);
    }
}

I'm having errors with the following 2 lines:

System.out.println("Radius of c2: " + c2.radius);

System.out.println("Radius of c2: " + c2.radius);

The program is failing to recognize c2.radius as the instance variable "radius" is declared as private in the circle class.

My question is is there any way to allow the CircleTester program to read the values of the radius of each object, without changing the instance variable from private to public? c1.radius and c2.radius do not work - the error is "Circle.radius is not visible" (because it is private)**

The reason I don't want to make it public is because I've been told by my tutor that declaring an instance variable as public can be seen as bad programming, and we could possibly be penalized in the exam.

I'm sorry if this is vague or stupid - explaining has never been my strong point. I'm also fairly new to Java so I am not 100% sure if I'm using all my terms correctly.

0

6 Answers 6

3

Just make a getter method:

public double getRadius() {
  return radius;
}
Sign up to request clarification or add additional context in comments.

Comments

1

The simplest (and standard) way is to declare a "getter" method in the Circle class, i.e.

public double getRadius() {
    return radius;
}

Alternatively, you could use reflection, but it is not meant to be used for cases like the one described in the question. So, a getter method is the solution.

4 Comments

I should have mentioned I did try exactly that, declaring it in the circle class, and while the circle class loaded, the CircleTester didn't. I'm not sure what I did wrong (what would I replace c1.radius with - just c1.getRadius right?
Yes, c1.getRadius() will do the trick.
Sorry, I should have added, the error code I get when amending c1.radius to c1.getRadius is "getRadius cannot be resolved or is not a field"
Ah, forgot to add the () to indicate no arguments, many thanks.
1

The best way is to create a getter for you variable radius in your class Circle

public double getRadius(){
     return radius;
}

and use

System.out.println("Radius of c2: " + c2.getRadius());

Comments

0

When you define an object, consider all the things you might like to ask of the object. For example, for a circle object you may want to ask the area, radius, diameter, circumference ... so

Don't get seduced into exposing the internal parameters of an object directly.

public class Circle {
    private double radius;

    public Circle(Double radius) { this.radius = radius;}

    public double getArea() { return radius*radius*Math.PI; }
    public double getRadius() { return radius;}
    public double getDiameter() { return 2*radius;}
    public double getCircumference() { return Math.PI*getDiameter();}
}

16 Comments

You could put your objects in an array or list and iterate over it while incrementing a counter
Not tested: List<Circle> circleList = new ArrayList<Circle>(); circleList.add(new Circle(5)); circleList.add(new Circle(10)); for (int i = 0; i < circleList.size(); i++) { double area2 = circleList.get(i).getArea(); double circumference2 = circleList.get(i).getCircumference(); System.out.println("Radius of c"+(i+1)+": " + circleList.get(i).radius); System.out.printf("The circle c"+(i+1)+" with circumference %.3f has area %.3f%n", circumference2, area2); }
well you created an empty array. you have to fill it up with Circles. I recommend to do this before u running the for. its a bit of work :) like this circ[<insert_fieldnumber>] = new Circlev2(<insert_value>);
the circ[index] = new Circle(1.00) thing is exactly what i tried to discribe ;)
No problem, maybe you remember this talk later ;)
|
0

Just use getter to get the variable:

public double getRadius() {
    return radius;
}

This is in fact a good programming practice. You could do a setter similarly if you wanted to alter this value.

4 Comments

Would you agree that declaring an instance as public is bad programming however - or is my tutor just one of them guys who talks out his a**? Thanks for the speedy response to my question!
Avoid ad hominem attacks first. Second, I would recommend not exposing as public anything other than constants in a class. You may want to change the internal working sometime, better to hide that behind method calls.
I have another question that's not exactly related to this. Instead of typing "Radius of c1: ..." and "The circumference of c1 ....", is there any way that I can refer to the object instead of explicitly typing "c1" and "c2" and so on for every object I create? Not sure if that makes sense. Sorry for adding it to this comment, I can't answer my question just yet
Declaring an instance as public exposes it to code outside it's class which could modify it but if it's a simple homework, then it really doesn't matter. Depends on where you're going with it. You need to type out "Radius of c1:.." because you need to somehow reference the instance of the specific object you are interested in
0

While a getter method is the best way to go, here's an approach using reflection. I wouldn't recommend using this unless you want your Circle object to be effectively immutable.

Circle circle = new Circle(10.0);

Field radius;
try {
    radius = circle.getClass().getDeclaredField("radius");
    radius.setAccessible(true); // required since field is private
    System.out.println(radius.getDouble(circle));
    radius.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
}

Comments