this creates another problem for me then since I am supposed to have the methods
ID: 3771311 • Letter: T
Question
this creates another problem for me then since I am supposed to have the methods overloaded-so they should have the same name but different parameter lists //Rename any one Area method class AreaClass { static final double pi = Math.PI; // Circle method static double Area(double radius) { return pi * radius * radius; } // rectangles method ??call it an int? static double Area(double width, double length) { return width * length; } // A class can't have two methods with same name and same number and type of // parameters // rename Area() method to something else //Here you both Area methods have same name and same number and same type of arguments i.e double,double // Cylinders method static double CylinderArea(double radius, double height) { return pi * radius * radius * height; } }
Explanation / Answer
It is not possible to have same name and same type of parameters for two functions. This violated the concept of polymorphism.
Therefore, I've added one parameter of "type", where you can distinguish between the type of area to calculate.
//Rename any one Area method
class AreaClass {
static final double pi = Math.PI;
// Circle method
static double Area(double radius) {
return pi * radius * radius;
}
// rectangles method ??call it an int?
static double Area(double width, double length,String type) {
if(type == "Rectangle")
return width * length;
else if(type == "Cylinder")
return pi * width * width * length;
//more types can be implemented
else
return 0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.