Polymorphism - method printing problem in Java

I am having problems with my code. So I have a GeoFig and Circle object that extends GeoFig. GeoFig contains private fields such as area, radius, and circumference. GeoFig also contains methods for setting and retrieving these fields. I tried to create a Circle object and create get methods but they don't work. Any advice? Here is my code below:

abstract public class GeoFig {

    private double radius;
    private double area;
    private double circumference;

    public void setRadius(double radius){
        this.radius = radius;
    }

    public double getRadius(){
        return this.radius;
    }

    public void setCircArea(double radius){
        this.area = Math.pow(radius, 2) * Math.PI;
    }

    public double getCircArea(){
        return this.area;
    }

    public void setCircumference(double radius){
        circumference = 2*this.radius * Math.PI;
    }

    public double getCircumference(){
        return this.circumference;
    }

    abstract String getMetrics();

    public static void main(String[] args){
        Circle circ = new Circle(2);
        circ.getRadius();
    }
}

public class Circle extends GeoFig {

    public Circle(double radius) {
        setRadius(radius);
        setCircArea(radius);
        setCircumference(radius);
    }

    @Override
    public String getMetrics() {
        return "Object of class Circle " +
            "\nRadius:" + getRadius() +
            "\nArea: " + getCircArea() + 
            "\nCircumference: " + getCircumference();
    }
}

      

+3


source to share


3 answers


This worked for me. He prints2.0



abstract class GeoFig {

private double radius;
private double area;
private double circumference;

    public void setRadius(double radius){
        this.radius = radius;
    }
    public double getRadius(){
        return this.radius;
    }

    public void setCircArea(double radius){
        this.area = Math.pow(radius, 2) * Math.PI;
    }
    public double getCircArea(){
    return this.area;
    }

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

    abstract String getMetrics();


    public static void main(String[] args){
        test circ = new test(2);
        System.out.println(circ.getRadius()); }
    }






public class test extends GeoFig {

public test(double radius){
    setRadius(radius);
    setCircArea(radius);
    setCircumference(radius);
}

@Override
public String getMetrics(){
    return "Object of class Circle " +
            "\nRadius:" + getRadius() +
            "\nArea: " + getCircArea() + 
            "\nCircumference: " + getCircumference();
}

}

      

0


source


I would suggest creating a method to display the content of the object you are trying to create eg. Create a ToSTRING method to view the contents of the object.

public void display () {



double radius = getRadius() ;
double area= getArea() ;
double circumference= getCircumference() ;
 System.out.println("Radius" + radius + "Area" + area + "Circumference" + circumference) ;

      

} If you are using Eclipse for Windows control + alt s, you can create a ToSTRING method. Mac command + alt + s. hope it helps

0


source


If you want to print the result try

System.out.println(circ.getRadius());

      

instead

circ.getRadius();

      

The problem with what you are doing now is that it gets the radius, but it does nothing with that value - you need to tell it to print the radius value after getting it.

0


source







All Articles