I am working on an assignment with very specific instructions, which, if it is of any intrest, I will post below the code I have created. But, in short, I am to create a BasicShape
abstract class, as well as a Circle
and Rectangle
subclasses. Each subclass has a method for calculating the area of the shape. the area is calculated using member variables. However, in my code below, these member variables are never assigned a value. I am confused as to how to assign it to them, because Circle
and Rectangle
methods also require arguments to be passed to them. These arguments x
, y
, r
for Circle
and w
, l
for Rectangle
are specified in the main program when a new instance of a shape is made, but these values also seem to do nothing, since the output is always 0. What relationship do the arguments passed into the methods have to the member variables? How is it that the member variables should be assigned values when the values are given via arguments set when a Circle
or Rectangle
instance is called?
Here is my code:
abstract class BasicShape
{
protected double area;
public double getArea()
{
Console.WriteLine("Area: {0}", area);
return area;
}
public virtual void calcArea()
{
}
}
class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;
public Circle(int x, int y, double r)
{
calcArea();
}
public int genCenterX()
{
return centerX;
}
public int genCenterY()
{
return centerY;
}
public override void calcArea()
{
area = 3.14159 * radius * radius;
Console.WriteLine("The area of the circle is: {0}", area);
}
}
class Rectangle : BasicShape
{
private int width;
private int length;
public Rectangle(int w, int l)
{
calcArea();
}
public int getWidth()
{
return width;
}
public int getLength()
{
return length;
}
public override void calcArea()
{
area = length * width;
Console.WriteLine("The area of the rectangle is: {0}", area);
}
}
public class TestShapes
{
static void Main(string[] args)
{
Circle circle1 = new Circle(2, 2, 5);
Rectangle rectangle = new Rectangle(6, 7);
Console.ReadLine();
}
}
Here are the instructions for the assignment:
Define a pure abstract base class called BasicShape. The BasicShape class should have the following members:
Private Member Variable: area, a double used to hold the shape’s area.
Public Member Methods: getArea(): This method should return the value in the member variable area. calcArea(): This method should be a pure virtual method.
Next, define a class named Circle. It should be derived from the BasicShape class. It should have the following members:
Private Member Variable: centerX, an integer used to hold the x coordinate of the circle’s center. centerY, an integer used to hold the y coordinate of the circle’s center. radius, a double used to hold the circle’s radius.
Public Member Methods:
Circle(int x, int y, int r): accepts values for centerX, centerY, and radius. Should call the overridden calcArea method described below.
genCenterX: returned the value in centerX
genCenterY: returned the value in centerY
calcArea(): calculates the area of the circle (area = 3.14159 * radius * radius) and stored the result in the inherited member area.
Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members: Private Member Variable:
width, an integer used to hold the width of the rectangle length, an integer used to hold the length of the rectangle
Public Member Methods:
Rectangle(int w, int l): accepts values for the width and length. Should call the overridden calcArea method described below.
getWidth(): returns the value in width. getLength(): returns the value in length calcArea(): calculates the area of the circle (area = length * width) and stored the result in the inherited member area.
After you have created these classes, create a main program that defined a Circle object and a Rectangle object.
Demonstrate that each object properly calculates and reports its area.