I have a class Figure, which describes the area and perimeter. I also have a lot of shapes inherited from this class. How can I define the area and perimeter for each shape? Can I do this in the constructor (as I did) or do I need to override the setter of the parent? Or do I have to do something else? Tell me please. Thank you in advance

Here is the parent class for all shapes:

public class Figure { private double area; private double perimeter; public double getArea() { return area; } public void setArea(double area) { this.area = area; } public double getPerimeter() { return perimeter; } public void setPerimeter(double perimeter) { this.perimeter = perimeter; } } 

Here is my circle:

 public class Cirlce extends Figure { private double radius; public Cirlce(double radius) { this.radius = radius; super.setArea(radius*2*Math.PI); } } 
  • I understand that objects can be changed? Or not ? How do you want to implement? - Alex Tremasov
  • The question of whether this can be done ... or the question of the appropriateness of such an approach? - Vitaliy Shebanits
  • @Gin Tasan yes, objects can be changed - Vladislav Estrin
  • @ Vitaliy Shebanits is both) - Vladislav Estrin
  • one
    if the area for all is considered the same, then yes - michael_best

2 answers 2

Circle class:

UPD: The code has been redone, at the request of the author, now the variables of the class Figure cannot be changed, are set once during creation

It is better not to put any calculations in the constructor, since various exceptions can be thrown in there, and it is better not to put methods there, since calling some methods in which, for example, some variables can not be predefined or initialized. However, the method can be called if you are 100% aware that there will not be the problems described above. Such a method is desirable should only define the object, and manipulate only the variables it receives, otherwise, as he said earlier, there may be a problem. In mathArea () and mathPerimeter (), you will need to create exception handling. There is a so-called public static method to which the radius value is passed, and if it does not comply with the conditions, an object is not created, without this method in the constructor, in the setRadius (..) method; if a negative value is passed, the object would be CREATED, and would return default double area and parameter values, which is not acceptable. There is a NullPointerException exception when trying to create and modify an object, as shown in qew.java, and it is necessary for each shape to set individual exception handling in methods, during creation, etc., where this is actually possible. There are also other advantages of such an architecture, if you are interested in which, write down

  public class Circle extends Figure { private double radius; public static Circle createCircle(double radius) { if(radius <= 0) {System.out.println("WHY ? Stop it...");return null;} else return new Circle(radius); } public Circle(double radius) { setRadius(radius); } private double mathArea() { return Math.pow(radius,2)*Math.PI; } private double mathPerimeter() { return 2*radius*Math.PI; } private void setRadius(double radius){ this.radius = radius; super.setPerimeter(mathPerimeter()); super.setArea(mathArea()); } } 

Class qew.java, I have with the main method: Test, an attempt to create an object with a negative radius.

 public class qew { public static void main(String[] args) { Circle c = Circle.createCircle(4); System.out.println(c.getArea()); System.out.println(c.getPerimeter()); } } 

Class Figure: The class is abstract, for why we need its instances, it must provide MANDATORY variables that 100% will be used for its descendants, otherwise, you need to define them in classes - descendants. This also applies to methods.

  abstract public class Figure { private double area; private double perimeter; public double getArea() { return area; } public void setArea(double area){ this.area = area; }; public double getPerimeter() { return perimeter; } public void setPerimeter(double perimeter){ this.perimeter = perimeter; }; } 
  • I may not understand something or incorrectly ask a question, but: we have a class Figure, in it the double area and double perimeter parameters are declared. I want it to have some method that writes the value to these parameters and that this method be redefined in each subsequent class of a specific figure. And it also seems to me that you should not do a check on setRadius - it will be entered once, when you create an object, and after that it will not be changed. Method set will not be in each figure separately - Vladislav Estrin
  • @ VladislavEstrin, 1 - you said that the objects will be changeable, since they are changeable, you can change the values ​​of variables in them, so this option was proposed, 2 - If you override a method that writes values, for example, setArea (), for example in the Circle class, you will not be able to set the value of the area in the Figure class, since it is private, and if you make them protected, does it make sense to have them in the Figure class? This is inappropriate (for some other reason). However, you can write these values, if in the class of the successor (Circle), call the class method Figure - Alex Tremasov
  • I apologize for misleading you. How in the end will be the best thing to do? This implementation is generally part of a large task drive.google.com/file/d/1FjSOE2QHqfOB8u_BPMN-Z1ky00_R3g7v/… - Vladislav Estrin
  • @ VladislavEstrin, and wanted to ask, what do you mean by checking setRadius? The createCircle () method is designed to ensure that if a user enters an incorrect number, the object is not created and the memory is not wasted. And if the object is created only once, then now I will update the code - Alex Tremasov
  • I also want to ask such a question: is there any sense at all in the class Figure? If we do not redefine its methods, but simply create separate methods for calculating the area and perimeter in each class, can it be easier in the same class to create the area and perimeter parameters, defining them in the constructor right away? I was completely confused) - Vladislav Estrin

You can create an abstract class Figure and set the "area" and "perimeter" fields in it. And then create abstract methods getPerimetr and getArea and in each class which is inherited from the figure redefine them.

 public abstract class Figure { private double area; private double perimeter; public double getArea(); public double getPerimeter(); } public class Cirlce extends Figure { private double radius; public Cirlce(double radius) { this.radius = radius; } @Override public double getArea(){ return radius*radius*Math.PI; @Override public double getPerimeter(){ return radius*2*Math.PI; } } public class Rectangle extends Figure { private double length; private double width; public Rectangle(double length, double width) { this.length = length; this.width = width; } @Override public double getArea(){ return length*width; @Override public double getPerimeter(){ return 2*(length*width); } } 

And to calculate in the designer costs nothing

  • why then the double area and double perimeter parameters in the Fugure class? - Vladislav Estrin
  • Because the area and perimeter is in all the figures. - Sergey Buvak
  • so we don't refer to these parameters anywhere, we write nothing on them - Vladislav Estrin
  • public double getArea (); This entry is incorrect, or the method must be abstract. And if it is abstract, it will not work to redefine it. You wrote some nonsense in general - Vladislav Estrin