Create an abstract class called shape Shape will have an abstract property calle
ID: 3842026 • Letter: C
Question
Create an abstract class called shape
Shape will have an abstract property called area
Shape will have a static backing field called _count
Shape will have a readonly property that will return the number of shapes created (the _count)
Shape will have a constructor that will increment the count field
Create a concrete class called rectangle
Rectangle will implement shape
Will have 2 fields to hold the width and height
Will have a constructor that will take in a width and height and will assign them to the fields
Rectangle will implement area and will return the width x height
Create a concrete class called circle
Implements shape
One field: diameter
Constructor takes in and set the diameter
Returns area ( 3.14 * (½ * diameter)* (½ * diameter)
Write code that
will create 3 rectangles and return their area
output the number of shapes created
will create 2 circles and return the area
output the number of shapes created
I need answet in C# visual studio windows form application. Need picture of designer too.
Please answer ASAP
thanks
Explanation / Answer
w4.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace W4___MDORFMAN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnEx_Click(object sender, EventArgs e)
{
// Creating 3 Rectangle Objects with Fixed Width & Height
rectangle r1 = new rectangle(10, 10);
rectangle r2 = new rectangle(20, 90);
rectangle r3 = new rectangle(160, 300);
// Outputing Area and How many Rectangles were Created
Console.WriteLine("Rectangle Area: " + r1.area());
Console.WriteLine("Rectangle Area: " + r2.area());
Console.WriteLine("Rectangle Area: " + r3.area());
Console.WriteLine("Rectangle Objects Created: " + r1.count);
// Creating 2 Circle Objects with Fixed Diameters
circle s1 = new circle(160);
circle s2 = new circle(250);
// Outputing Area and How many Circles were Created
Console.WriteLine("Circle Area: " + s1.area());
Console.WriteLine("Circle Area: " + s2.area());
Console.WriteLine("Cirle Objects Created: " + s1.count);
}
}
// Abstract Shape Class
abstract class shape
{
// Abstract Property
public abstract double area();
// Static Backing Field
static double _count;
// Readonly Property
// Returns _count (# of Shapes)
public double count
{
get
{
return _count;
}
}
// Constructor
// Increments Count
public shape()
{
_count++;
}
}
// Rectangle Class (Inherits from Shape)
class rectangle : shape
{
// 2 Fields
private double width;
private double height;
//Constructor rectangle
// recieves 2 int's assigns them to fields
public rectangle(double w, double h)
{
width = w;
height = h;
}
// Implements the area method area from shape class
// returns width * height
public override double area()
{
return (width * height);
}
}
// Circle Class (Inherits from Shape Class)
class circle : shape
{
// Fields
private double diameter;
// Constructor
// Recieves a double and sets the diameter
public circle (double d)
{
diameter = d;
}
// Implemtation of the Area Method from the
// Abstract Shape Class returns the area
public override double area()
{
return ((3.14 * ( 0.5 * diameter) * (0.5 * diameter)));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.