Currently need help answering this question, please follow the steps exactly and
ID: 3843381 • Letter: C
Question
Currently need help answering this question, please follow the steps exactly and if possible explain each step
Create a class hierarchy as follows: The base class is called shape and has the following data members 1. A private data member - name (string) to hold the name of the shape. 2. A private data member - color (string) to hold the color of the shape. 3. A public member function display that does not return anything but displays the characteristics of the shape (color, name, area, and other information as applicable based on the shape). 4. A public member function CalArea that returns double. It calculates the area of the shape. For the class shape, this member function should display an error message stating that the shape is unknown and return 0. Create the following derived classes using public inheritance as follows: 1. A circle (derived from the shape class) a. A private data member called radius (double) to hold the radius of the circle. b. A public member function called CalCircumference which returns double and calculates the area of the circle. 2. A rectangle (derived from the shape class) a. Two private data members called width[int) and height(int). b. A public member function called CalPerimeter which calculates the perimeter of the rectangle. Create all necessary "get" and "set" functions to access the private data members. Also, redefine the member function display to produce appropriate messages based on the object type. The function should display name, color, width, height, area, and perimeter for the class rectangle and name, color, radius, area, and circumference for the class circle. For the class shape, the function displays name and color. Also, redefine CalArea to calculate the area properly for each object type.Explanation / Answer
public class shape
{
private String name;
private String color;
public Shape()
{
}
public shape(String name ,String color)
{
this.name = name;
this.color= color;
}
public void display()
{
System.out.println("name"+name+"color"+color + "area" + CalArea());
}
public double CalArea(String name,int l ,int w)
{
double area;
if((name).equals("rectangle"))
{
area = l * w ;
}
if((name).equals("circle"))
{
area = 3.14 * l * l;
}
return area;
}
}
public class Rectangle
{
private int length;
private int width;
public Rectangle()
{
}
public Rectrangle(int l ,int w)
{
length = l;
width = w;
}
public void setLength(int l)
{
length = l;
}
public void setWidth(int w)
{
width = w;
}
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
public double CalPerimeter(int l,int w)
{
double perimeter;
perimeter = 2*(l + w);
return perimeter;
}
}
class Circle
{
private int radius;
public Circle()
{
}
public Circle(int radius)
{
this.radius = radius ;
}
public void setRadius(int r)
{
radius = r
}
public int getRadius()
{
return radius;
}
public CalCircumference(int r)
{
double circumference;
circumference = 2 * 3.14 * r ;
return circumference;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.