Setting the argument/parameter in C# - c#

I've been working on an assignment for class, beginners C#. I've hit a point where I do not know what to do next. This is the question and it involves argument/parameters...
Define the output of the "area" property calculation such that a user can initialize an instance of the "Circle" class by setting the argument/parameternamed"radius" (in the Constructor) and subsequently call a method named "ShowArea" to display the area of the new circle instance using the formula: (where r = radius, A = area, π= pi)
This is what I have so far:
namespace IndividualAssignment2
{
public class Shape
{
public virtual int area { get; set; }
}
public class Circle : Shape
{
double radius;
public override int area { get; set; }
double ShowArea = 3.14 * Math.Pow(radius,2);
}
public sealed class Square : Shape
{
int height;
}
}
How would I implement this into my code? My double ShowArea is incorrect because radius is underlined. I think understanding this question would help with that issue. Thank you.

If I'm understanding you correctly, ShowArea is a method and not a field. Which means your Circle class should be something like:
public class Circle : Shape
{
double _radius;
// Constructor for the Circle that has radius as a parameter
public Circle(double radius)
{
_radius = radius;
}
// Method that returns the area of the circle using radius value from constructor
public double ShowArea()
{
return Math.Pi * Math.Pow(_radius, 2.0);
}
}

Your class design must be reviewed.
public abstract class Shape
{
public abstract double Area { get; }
}
public class Circle : Shape
{
public Circle(double radius)
{
Radius = radius;
}
private double Radius { get; set; }
public override double Area => 3.14 * Math.Pow(Radius, 2);
}
public class Square : Shape
{
public Square(double edge)
{
Edge = edge;
}
private double Edge { get; set; }
public override double Area => Math.Pow(Edge, 2);
}

Your declaration of the method ShowArea is not correct. You are declaring a field instead.
You should read up more on methods. You were also tasked to declare a constructor with a parameter to set the radius, which I don't find it in your code.

Related

There is no argument that corresponds to the required formal paramter

I have only just started learning C# so excuse this basic question. I am experimenting with C# inheritance and want to inherit the properties from Shape class into the Rectangle class. The Rectangle class below gives me an error:
"There is no argument that corresponds to the required formal
parameter 'height' of 'Shape.Shape(double.double)'
Is anyone able to tell me why this is happening?
class Shape
{
public double Height { get; set; }
public double Width { get; set; }
public Shape (double height, double width)
{
Height = height;
Width = width;
}
public double calculateArea()
{
double Area = (Height * Width);
return Area;
}
}
class Rectangle : Shape
{
public Rectangle(double height, double width)
{
Height = height;
Width = width;
}
static void Main(string[] args)
{
Rectangle rectangle = new Rectangle(15, 19);
double areaOfRectangle = rectangle.calculateArea();
Console.WriteLine(areaOfRectangle);
}
}
In c#, the base keyword is used to access base class members such as properties, methods, etc. in the derived class. so you have to act like below:
public Rectangle(double height, double width) : base(height, width) { ... }
for learning more about base you can follow this. good luck.

how to select drawn shapes like circle or rectangle in panel with mouse in c#

I'm trying to make a minipaint application which has three buttons(rectangle, circle and line). I'm having problem with selecting and moving shapes with mouse. For example I have this rectangle class which inherits color, thickness from shape:
class rectangle : shape
{
public int length { get; set; }
public int width { get; set; }
public override void Draw(Graphics g)
{
g.DrawRectangle(new Pen(color), new Rectangle(startx, starty, width,length));
}
}
Now, I want my panel1_MouseDown to select a rectangle in my panel whenever I click on any part of rectangle.
All drawn shapes are added to a list named lstShapsOnForm and drawable is an abstract class that has abstract method of draw and property x y.
abstract class Drawable
{
public int x { get; set; }
public int y { get; set; }
public abstract void draw(Graphics g);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
foreach (Drawable o in lstShapsOnForm)
{
if (e.Location.X >= o.x || e.Location.X < o.x)
propertyGrid1.SelectedObject = o;
}
}
How should I make this work?
Simplest thing is to make it the responsibility of the shape to know if they have been clicked in, so add a IsInside method eg:
abstract class Drawable
{
public int x { get; set; }
public int y { get; set; }
public abstract void draw(Graphics g);
public abstract bool IsInside(int x, int y);
}
Then to test:
var shapeHit = lstShapsOnForm.FirstOrDefault(s => s.IsInside(e.Location.X, e.Location.Y));
if (shapeHit != null)
propertyGrid1.SelectedObject = shapeHit;
For the rectangle, assuming you know the width and height (you should if you drew it), and assuming that the x and y properties are the top left corner, then it would be somehting like this:
public override bool IsInside(int mouseX, int mouseY)
{
return
mouseX >= x &&
mouseX <= x + width &&
mouseY >= y &&
mouseY <= y + height;
}
For the circle and line ..... I suggest you google 'how to determine if x/y point is inside circle / on line'. It will involve testing using the radius (for the circle) and the 2 line points (for the line)

C# GetName() , GetArea() for custom class "Shape" and it's derived classes

I have a task :
There is an hierarchy: "Shape" - base class, "Triangle", "Circle", "Rectangle" - derived classes of "Shape", "IsoscelesTriangle" - derived class of "Triangle", "Square" - derived class of "Rectangle". "Shape" has methods: GetArea() - returns the area of a geometric shape, GetName() - returns the name of a geometric shape. For each derived class area and name can be determined. Console program demonstrates the principle of polymorphism using the output messages of name and area.
so at this moment my main looks like this:
Problem1_1.Shape triangle1 = new Problem1_1.Triangle("triangle1", 5, 10);
double triangle1Area = triangle1.GetArea();
string triangle1Name = triangle1.GetName();
Console.WriteLine(triangle1Name, triangle1Area);
Problem1_1.Shape isoTriangle1 = new Problem1_1.IsoscelesTriangle("iso triangle", 2, 10);
double isoTriangle1Area = isoTriangle1.GetArea();
string isoTriangle1Name = isoTriangle1.GetName();
Console.WriteLine(isoTriangle1Name, isoTriangle1Area);
Problem1_1.Shape circle1 = new Problem1_1.Circle("circle1", 5);
double circle1Area = circle1.GetArea();
string circle1Name = circle1.GetName();
Console.WriteLine(circle1Name, circle1Area);
Problem1_1.Shape rect1 = new Problem1_1.Rectangle("rectangle1", 2, 10);
double rect1Area = rect1.GetArea();
string rect1Name = rect1.GetName();
Console.WriteLine(rect1Name, rect1Area);
Problem1_1.Shape square1 = new Problem1_1.Square("sq1", 2, 3);
double square1Area = square1.GetArea();
string square1Name = square1.GetName();
Console.WriteLine(square1Name, square1Area);
my custom classes namespace looks like this:
namespace Problem1_1
{
public abstract class Shape
{
protected Shape(string name)
{
Name = name;
}
public string Name { get; }
public virtual string GetName()
{
return "Shape: " + Name;
}
public abstract double GetArea();
}
public class Triangle : Shape
{
private double side;
private double height;
public Triangle(string name, double side, double height) : base (name)
{
this.side = side;
this.height = height;
}
public override double GetArea()
{
double area = (side * height) / 2;
return area;
}
}
public class Circle : Shape
{
private double radius;
public Circle(string name, double radius) : base (name)
{
this.radius = radius;
}
public override double GetArea()
{
double area = radius * radius * Math.PI;
return area;
}
}
public class Rectangle : Shape
{
private double side1;
private double side2;
public Rectangle(string name, double side1, double side2) : base (name)
{
this.side1 = side1;
this.side2 = side2;
}
public override double GetArea()
{
double area = side1 * side2;
return area;
}
}
public class IsoscelesTriangle : Triangle
{
public IsoscelesTriangle(string name, double side, double height) : base (name, side, height) { }
public override double GetArea()
{
return base.GetArea();
}
}
public class Square : Rectangle
{
private double side1;
public Square(string name, double side1, double side2) : base(name, side1, side2)
{
this.side1 = side1;
}
public override double GetArea()
{
double area = side1 * side1;
return area;
}
}
}
so what r the issues.
First, my console message doesn't give me the area. It gives me only the name. What should I change to get both?
Second, I don't know how to handle the Square class. If it is derived from rectangle how can I get only 1 value to calculate area. If I set it like this :
public Square(string name, double side1) : base(name, side1)
I'm getting an error.
As far as I know there is no overload of Conosole.WriteLine(); which allows you to print two or more varialble like that, you need to use formatting like,
Console.WriteLine("{0} : {1}", triangleName, triangleArea);
Console.WriteLine($"{triangle1Name} : {triangle1Area}");
And can you explain second question a little more?
Why you pass two values for the side of a Square in its constructor? You just need one side to define a Square.
Problem1_1.Shape square1 = new Problem1_1.Square("sq1", 2);
double square1Area = square1.GetArea();
string square1Name = square1.GetName();
Console.WriteLine($"Name={square1Name}, Area={square1Area}");
Then change the constructor of Square to be
public class Square : Rectangle
{
private double side1;
public Square(string name, double side1) : base(name, side1, side1)
{
this.side1 = side1;
}
}
As you can see, you could also remove the override for GetArea and let the base class of Square (Rectangle) return its calculations.
I suggest also to make the two side variables inside the Rectangle class protected so you can also remove the variable side1 inside the Square class
public class Rectangle : Shape
{
protected double side1;
protected double side2;
...
}
Finally, for the output problem, you just need to use Console.WriteLine correctly as shown above.

How to hide the parent class method from the child class object?

I want to hide the parent class method for the the child class object. My Circle object display two methods to me when I going to access that methods:
Here objShape is the object of the Shape class which is the abstract class.
I want to hide method which is display on 2nd image.
Here Circle class extends the Oval class and Oval class extends the Shape class.
Shape class coding
abstract public class Shape
{
public abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);
public abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);
}
Oval class Coding
class Oval : Shape
{
public override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dAns = Math.PI * dNum1 * dNum2;
return dAns;
}
public override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dTmp1, dTmp2, dAns;
dTmp1 = 2 * (dNum1 * dNum1);
dTmp2 = 2 * (dNum2 * dNum2);
dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);
return dAns;
}
}
Circle Class Code
class Circle : Oval
{
public double Area(double dNum1)
{
double dResult = base.Area(dNum1, dNum1);
return dResult;
}
public double Perimeter(double dNum1)
{
double dAns = base.Perimeter(dNum1, dNum1);
return dAns;
}
}
So, Here how can I hide the direct access of the Oval class method for the Circle class object. In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object.
In 2nd image it display the suggestion and I can use that method, but I don't want that suggestion and I don't want use that method with Circle class object.
Then you shouldn't make Circle derive from Oval, basically. Don't forget that it would be perfectly acceptable to have a variable of type Oval which has a value at execution time referring to an instance of Circle:
Oval x = new Circle();
Console.WriteLine(x.Area(10, 20));
Fundamentally, any operation that makes sense for the base class should make sense for the derived class as well, in a sensible inheritance hierarchy.
If you want to keep your inheritance hierarchy, you should consider a redesign such that the radius etc are part of the state of the object, instead of being passed in as parameters. For example:
public abstract class Shape
{
public abstract double Area { get; }
public abstract double Perimeter { get; }
}
public class Oval
{
private double Width { get; }
private double Height { get; }
public override double Area { get { ... } };
public override double Perimeter { get { ... } };
public Oval(double width, double height)
{
Width = width;
Height = height;
}
}
public class Circle
{
public double Radius { get { return Height; } }
public Circle(double radius) : base(radius)
{
}
}
Just have Circle inherit shape as opposed to oval if you dont want the oval methods exposed by circle.
You can achieve this by changing your access specifier of Shape and Oval class methods to protected.
abstract public class Shape
{
protected abstract double Area(double dNum1 = 0.0, double dNum2 = 0.0);
protected abstract double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0);
}
and
class Oval : Shape
{
protected override double Area(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dAns = Math.PI * dNum1 * dNum2;
return dAns;
}
protected override double Perimeter(double dNum1 = 0.0, double dNum2 = 0.0)
{
double dTmp1, dTmp2, dAns;
dTmp1 = 2 * (dNum1 * dNum1);
dTmp2 = 2 * (dNum2 * dNum2);
dAns = (Math.PI / 2) * Math.Sqrt(dTmp1 + dTmp2);
return dAns;
}
}
I think this is what you are looking for..

c# how do I do a private set property?

I am creating a VECTOR object like so, but I am initializing it in the constructor:
public VECTOR position { get; private set; }
I am doing this operation:
position.x += 2;
VECTOR has a variable x defined as:
public double x { get; set; }
I get the error when I do the +=2 operation that says:
Cannot modify the return value of 'Projectile.position' because it is
not a variable
I want to be able to modify the position vector in the current class, and I want it to be accessible but not modifiable in other classes.
Probably, your problem is with the Vector class actually being a struct. Assume you have the following declarations:
public class Projectile
{
public VECTOR position { get; private set; }
public Projectile()
{
position = new VECTOR();
}
}
public struct VECTOR
{
public double x {get; set;}
}
You cant edit properties of the position property directly because you are accessing a copy of that field (explained here).
If you don`t want to convert your VECTOR into a class you can add a method that updates the position of your projectile:
public void UpdatePosition(double newX)
{
var newPosition = position;
newPosition.x = newX;
position = newPosition;
}
That will create a copy of the position, then update its x property and override the stored position. And the usage would be similar to this:
p.UpdatePosition(p.position.x + 2);
Expose the X property (and others) of VECTOR in a class.
public class myObject {
private VECTOR position;
public double X { get{return position.x;}set{position.x=value;}}
}
Usage example:
myObject.X += 2;

Categories