java - How to calculate area of child class from parent class -


here questions:

  1. when i'm printing area of both shapes, getting correct area ellipse, 0 circles. why?

  2. i have multiple radii of circle. how can keep on adding area, computed previous radius? in short, need sum area of radii.

  3. whenever pass radius of circle, need circle area (computed radius) should added ellipse area well. how that?

this current code:

public class ellipse {     private double area;     private double axis1;     private double axis2;      public ellipse() {         area = 0;     }      public ellipse (double axis1, double axis2) {         this.axis1 = axis1;         this.axis2 = axis2;     }      public void calculatearea() {         area += axis1 * axis2;     }      public double getarea() {         return area;     } }  public class circle extends ellipse {     private double radius;     private double area;      public circle(){         area = 0;     }      public circle(double radius) {         super(radius, radius);     }       public double getarea() {         return area;     } }  public class driver {     public static void main (string args[]) {         ellipse ellipse = new ellipse(10, 20);         ellipse.calculatearea();         circle circle = new circle(50);         circle.calculatearea();         system.out.println("area of circle : " + ellipse.getarea());         system.out.println("area of circle : " + circle.getarea());     }  } 

in getarea() method under class circle returning value of area under circle.

however, calculate result in area variable under ellipse class.

do this: change

private double area; 

to

protected double area; 

and in getarea() method under circle class:

public double getarea(){     return super.area; } 

hope helps.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -