0

I have:

public class Circle {

//private instance variable
private double radius = 1;//Declaring "1" as the default value
private String color = "red";//Declaring "red" as the default color and as a string.
// default constructor w/out an argument
public Circle() {
}

public Circle(double r){ //constructor that uses double argument which is assigned to radius
    radius = r;
    color = "red";
}
//public method "getRadius"
public double getRadius() {
    return radius;
}
//public method "getArea", used to get the area of the circle.
public double getArea() {
    //method returns the Area of a circle using the below formula
    return Math.PI * radius * radius;
}
}

and

public class TestCircle {
// Testing function
public static void main(String[] args) {
    Circle c1 = new Circle(); // initialize with default constructor
    Circle c2 = new Circle(5); // initialize with constructor that takes radius as argument

    //prints the results of the program.
    System.out.println("*****************************************");
    System.out.println("Details of circle 1: ");
    System.out.println("Radius: " + c1.getRadius());
    System.out.println("Area: " + c1.getArea());
    System.out.println("Color: " + color);
    System.out.println("*****************************************");
    System.out.println("Details of circle 2: ");
    System.out.println("Radius: " + c2.getRadius());
    System.out.println("Area: " + c2.getArea());
    System.out.println("Color: " + c2.getColor());
    System.out.println("*****************************************");

I am also trying to get the color of the circle "red" to print out as well. Now the kicker is I had the following in my code and she said that their is another way of doing it.

//Constructor that uses a string argument which is assigned to color
public Circle(String c) {
    color = C;
}
//public method "getColor", used to get the color of the circle.
public String getColor() {
    return color;
}
}

FYI....I asked her if I should just do System.out.println("red"); and she said no.

sluger233
  • 21
  • 7

1 Answers1

2

You need a getter for your String color attribute in Circle class, then use it as you are already doing with radius and area.

Apart of that, I would recommend you creating setters for the fields in Circle class in order to change the values of the attributes per instance.

(No code will be given since this is a homework).


In the strange case you don't want to use any getter/setter at all (which is really odd in real world applications), you may change the modifier of your attributes to enable access to them directly from other classes. Here's the Java modifier access level:

Modifier    Class Package Subclass   World
public      Y     Y       Y          Y
protected   Y     Y       Y          N
no modifier Y     Y       N          N
private     Y     N       N          N

So, you may change String color from private to public and any class can access to this attribute and use it or change its value with no problems. Note that by doing this you break encapsulation of your class.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Its not homework. I am not in school. I already have my bachelors in computer science. – sluger233 May 06 '14 at 16:34
  • "She" refers to a friend. We are both giving each other challenges to make our selves better at programming. We want to become programmers and have the basic foundation – sluger233 May 06 '14 at 16:38
  • @LuiggiMendoza You obviously dont know the answer which is why you would say something like that. Its just a question where I am stuck is all. I am sure its going to be an "oh" moment when its figured out. – sluger233 May 06 '14 at 16:39
  • The string color is private the variable in Circle() is not (you should really specify it instead of being default). – mortsahl May 06 '14 at 16:50
  • And, by the way, it should really be ... private static final String COLOR = "red" – mortsahl May 06 '14 at 16:51
  • @sluger233 answer updated. Still, no code will be provided, the tests to make it work applying these concepts are up to you. – Luiggi Mendoza May 06 '14 at 16:55
  • @luiggiMendoza Thank You. I dont want code per say your answer should be good enough and I do appreciate it. I will check it out – sluger233 May 06 '14 at 17:14