Question
need java code Show constructor overloading in a Java program by the following steps. Create a class called Area in Java. Create objects for this
need java code
Show constructor overloading in a Java program by the following steps.
Create a class called Area in Java. Create objects for this Area class and call constructor Area. Create constructor Area for each mathematical figure like square, rectangle, sphere, triangle to calculate the area.
For calculating area of square, create a constructor inside the class that takes an integer as its parameter for representing side of a square. Calculate the area of square and print the value.
For calculating area of rectangle, create a constructor inside the class that takes 2 integer values as its parameters for representing breadth and width of a rectangle. Calculate the area of rectangle and print the value.
For calculating area of triangle, create a constructor that takes 2 double values as its parameters for representing base and height of a triangle. Calculate the area of triangle and print the value.
For calculating area of sphere, create a constructor that takes 1 double value as its parameter for representing radius of a sphere. Calculate the area of sphere and print the value.
Inside the main() method, create 4 objects for the class Area and for each constructor pass the corresponding value to call the constructor of each shape.
For example: Inside main() method, Area a1 = new Area(5); // This is how object creation and constructor calling is done for square.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started