Area & Perimeter Output Are the Same - c#

public class Circle : Shape
{
private float perimeter, area;
public override float getPerimeter(float Dimension)
{
perimeter = (float)(2 * Math.PI) * (Dimension / 2);
return perimeter;
}
public override float getArea(float Dimension)
{
area = (float)Math.PI * ((Dimension / 2) * (Dimension / 2));
return area;
}
}
public class Square : Shape
{
private float perimeter, area;
public override float getPerimeter(float Dimension)
{
perimeter = (float)(Dimension * 4);
return perimeter;
}
public override float getArea(float Dimension)
{
area = (float)(Dimension * Dimension);
return area;
}
}
This is only the abstract class but there seems to be a problem here when run the program: the Area and Perimeter output are both the same, and I don't know why.

Related

Orienting an ellipse in 3D space

I am trying to create an ellipse in 3D space. Ive used
https://www.youtube.com/watch?v=mQKGRoV_jBc
https://www.youtube.com/watch?v=Or3fA-UjnwU&t=390s
https://www.youtube.com/watch?v=lKfqi52PqHk
to create an ellipse. Now the ellipse is constructed via a xAxis and a yAxis. I need to move the middlepoint of the ellipse a given distance in a certain direction and then tilt the hole plane on which the ellipse is constructed at a certain angle.
Now my idea is to take the transform.position of the constructing empty and offset it in the direction needed and at the distance needed. The idea goes then further by simply rotating the constructing empty the given angle. Unfortunelty I have 0 clue how to. Ive provided you with the Ellipse class creating the Ellipse and the EllipseRenderer-script so you have an idea whats going on.
[System.Serializable]
public class Ellipse
{
float xAxis;
float yAxis;
public Ellipse(float xAxis, float yAxis)
{
this.xAxis = xAxis;
this.yAxis = yAxis;
}
public Vector2 Evaluate(float orbitalProgression)
{
float angle = Mathf.Deg2Rad * 360 * orbitalProgression;
float x = Mathf.Sin(angle) * xAxis;
float y = Mathf.Cos(angle) * yAxis;
return new Vector2(x, y);
}
}
,
public class OrbitMotion : MonoBehaviour
{
public Transform orbitingObject;
public Ellipse orbitPath;
[Range(0f,1f)]
public float orbitProgress = 0f;
public float orbitPeriod = 3f;
public bool orbitActive = false;
void Start()
{
if (orbitingObject == null)
{
orbitActive = false;
return;
}
SetOrbitingObjectPosition();
StartCoroutine(AnimateOrbit());
}
void SetOrbitingObjectPosition()
{
Vector2 orbitPos = orbitPath.Evaluate(orbitProgress);
orbitingObject.position = new Vector3(orbitPos.x, 0, orbitPos.y);
}
IEnumerator AnimateOrbit()
{
if (orbitPeriod < 0.1f)
{
orbitPeriod = 0.1f;
}
float orbitspeed = 1 / orbitPeriod;
while (orbitActive)
{
orbitProgress += Time.deltaTime * orbitspeed;
orbitProgress %= 1f;
SetOrbitingObjectPosition();
yield return null;
}
}
}
and
[RequireComponent(typeof(LineRenderer))]
public class EllipseRenderer : MonoBehaviour
{
[Range(3, 36)]
public int segments = 7;
public LineRenderer lr;
public Ellipse ellipse;
private void Awake()
{
lr = GetComponent<LineRenderer>();
CalculateEllipse();
}
void CalculateEllipse()
{
Vector3[] points = new Vector3[segments + 1];
for (int i = 0; i < segments; i++)
{
Vector2 position2D = ellipse.Evaluate((float)i / (float)segments);
points[i] = new Vector3(position2D.x, position2D.y, 0);
}
points[segments] = points[0];
lr.positionCount = segments + 1;
lr.SetPositions(points);
}
public Vector3 CalculatePosition()
{
}
private void OnValidate()
{
CalculateEllipse();
}
}

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.

C# Create a Rectangle class that holds width and height.

This is for C#
Create a Rectangle class that holds width and height. Provide a constructor that accepts width and height. The Rectangle class contains three methods, to calculate the perimeter, to calculate the area, and to check whether it is square or not respectively.
So far I got this done..
public double width;
public double height;
public Rectangle(double w, double h)
{
width = w;
height = h;
}
public double perimeter()
{
return 2 * (width + height);
}
public double area()
{
return width * height;
}
public boolean isSquare()
{
if (width == height)
{
return true;
}
else
{
return false;
}
}
}
}
I get an error for this line
}
public boolean isSquare()
{
What is the problem?
You should use bool instead of boolean. Try this code:
public bool isSquare()
{
if (width == height)
{
return true;
}
else
{
return false;
}
}
Hope it helps!

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..

Why am I getting a value of zero for my circle?

Here is my Circle class code.
class Circle
{
private double radius;
private double area;
public Circle(double radius)
{
this.radius = radius;
}
public double Area
{
set { area = Math.PI * Math.Pow(radius, 2); }
get { return area; }
}
}
This is test code.
Circle circle1 = new Circle(3);
MessageBox.Show("Circle 1 Area: " + circle1.Area);
So for some reason, when I use the MessageBox.Show(), it seems to give me values of zero instead. I gave the circle a value of 3 so shouldn't my constructor set the value of the radius that?
Because you haven't ever called the setter on Area. Perhaps you want something like this instead?
class Circle
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public double Area
{
get { return Math.PI * Math.Pow(radius, 2); }
}
}
This will compute the Area every time it is requested.
Your Area property should be:
public double Area
{
get { return Math.PI * Math.Pow(radius, 2); }
}
and you don't need the area field.
I'm not sure you need a set in this instance (You didn't use it)
try
get { return Math.PI * Math.Pow(radius, 2); }

Categories