How do I introduce an initialization constructor? C# CS0236 - c#

I'm getting multiple errors but I don't know why. The errors are introduced after the GetArea method.
namespace Lesson02
{
class Rectangle
{
static void Main(string[] args)
{
}
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
public Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);

You can't just put execution
Console.WriteLine("Area of Rectagle: {0}", area);
within the class scope as if it's declaration. Move it to the Main method:
namespace Lesson02
{
class Rectangle
{
// Method, here we execute
static void Main(string[] args)
{
// Executions are within the method
Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
}
// Declarations
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
}
}

As mentioned in the comment, you have class body mixed up with program code. It's also a bad idea to have everything in one class.
Your Rectangle class should be separate:
public class Rectangle
{
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
}
And your program code separate:
public class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
}
}

Either make class Rectangle as public otherwise change
public Rectangle rect = new Rectangle(10.0, 20.0); as Rectangle rect = new Rectangle(10.0, 20.0);
public class Rectangle
{
private double length;
private double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
public double GetArea()
{
return length * width;
}
}
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10.0, 20.0);
double area = rect.GetArea();
Console.WriteLine("Area of Rectagle: {0}", area);
}

Related

Creating and comparing objects based on user input

I'm pretty new to C# and I have a task to solve.
I need to create a console app that asks the user to make 2 figures(either rectangle or hexagon again selected by the user input) to enter their properties and compare which one is bigger. For some reason I get not all code paths return a value in SelectAndCreateFigure even after using the else statement returning null.
This is my code:
figure class:
namespace PU_task
{
public class Figure
{
public virtual double Area()
{
return 0;
}
}
}
hexagon class:
using System;
namespace PU_task
{
public class Hexagon :Figure
{
private double side;
public double Side { get => side; set => side = value; }
public Hexagon()
{
}
public override double Area()
{
double area = 3 * Math.Sqrt(3 * Math.Pow(side, 2)) / 2;
return area;
}
public Figure CreateFigure(double side)
{
Hexagon hexagon = new Hexagon
{
Side = side
};
return hexagon;
}
}
}
rectangle class:
namespace PU_task
{
public class Rectangle : Figure
{
private double length;
private double width;
public double Length { get => length; set => length = value; }
public double Width { get => width; set => width = value; }
public Rectangle()
{
}
public override double Area()
{
double area = length * width;
return area;
}
public Figure CreateFigure(double width,double length)
{
Rectangle rectangle = new Rectangle
{
Width = width,
Length = length
};
return rectangle;
}
}
}
The code in my main file:
using System;
namespace PU_task
{
internal class Program
{
static void Main(string[] args)
{
Figure figure1 = new Figure();
figure1.SelectAndCreateFigure();
Figure figure2 = new Figure();
figure2.SelectAndCreateFigure();
SmallerArea(figure1, figure2);
}
public Figure SelectAndCreateFigure(Figure figure)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine().ToLower();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
Rectangle rectangle = new Rectangle();
rectangle.CreateFigure(width, length);
return rectangle;
}
else if (input == "hexagon")
{
Console.WriteLine("Enter the the side length:");
double side = double.Parse(Console.ReadLine());
Hexagon hexagon = new Hexagon();
hexagon.CreateFigure(side);
}
else return null;
}
public void SmallerArea(Figure figure1, Figure figure2)
{
if (figure1.Area() > figure2.Area())
{
Console.WriteLine(figure1.Area());
}
else
{
Console.WriteLine(figure2.Area());
}
}
}
}
You have an issue with initializing your objects with the correct data. Each class needs a constructor to fill in the data. Also C# supports properties, that can either store values (like the side of the hexagon), or that can return calculated values (like the area of the figure).
A basic skeleton for the above functionality is shown below:
public abstract class Figure
{
public abstract double Area { get; }
}
public class Rectangle : Figure
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public double Length { get; }
public double Width { get; }
public override double Area => Length * Width;
}
public class Hexagon : Figure
{
public Hexagon(double side)
{
Side = side;
}
public double Side { get; }
public override double Area => 3 * Math.Sqrt(3 * Math.Pow(Side, 2)) / 2;
}
class Program
{
static void Main(string[] args)
{
Figure rect = new Rectangle(12.0, 5.5);
Figure hex = new Hexagon(8.0);
if (rect.Area > hex.Area)
{
Console.WriteLine("Rectangle is bigger");
}
else
{
Console.WriteLine("Hexagon is bigger");
}
}
}
It is up to you to design the required functionality based on the class hierarchy above.
try this
static void Main(string[] args)
{
double hexagonArea=0;
double rectangleArea=0;
CreateFigures(ref hexagonArea, ref rectangleArea);
Console.WriteLine("rectangle area - "+rectangleArea.ToString());
Console.WriteLine("hexagon area - "+hexagonArea.ToString());
if (rectangleArea > hexagonArea)
Console.WriteLinge ("rectangle area is bigger!");
else Console.WriteLinge ("hexagon area is bigger!");
}
public void CreateFigures(ref double hexagonArea, ref double rectangleArea)
{
for (int i = 0; i < 2; i++)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
rectangleArea= CreateRectangle(width, length);
}
else if (input == "hexagon")
{
Console.WriteLine("Enter width:");
double side = double.Parse(Console.ReadLine());
hexagonArea = CreateHexagon(side);
}
}
}
public void CreateRectangle(double length,double width)
{
Rectangle rectangle = new Rectangle
{
Width = width,
Length = length
};
return rectangle.Area();
}
public double CreateHexagon(double side)
{
Hexagon hexagon = new Hexagon
{
Side = side
};
return hexagon.Area();
}
The reason you getting 'not all code paths return a value' error in SelectAndCreateFigure is because in your else if statement you not returning figure. this will solve that particular error.
public Figure SelectAndCreateFigure(Figure figure)
{
Console.WriteLine("Select figure type:");
string input = Console.ReadLine().ToLower();
if (input == "rectangle")
{
Console.WriteLine("Enter length:");
double length = double.Parse(Console.ReadLine());
Console.WriteLine("Enter width:");
double width = double.Parse(Console.ReadLine());
Rectangle rectangle = new Rectangle();
rectangle.CreateFigure(width, length);
return rectangle;
}
else if (input == "hexagon")
{
Console.WriteLine("Enter the the side length:");
double side = double.Parse(Console.ReadLine());
Hexagon hexagon = new Hexagon();
hexagon.CreateFigure(side);
return hexagon;
}
else return null;
}

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.

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!

Why can't I call this constructor with two arguments in C#?

I'm new to C# and I'm learning about scope. Just making a simple program to calculate the length of a line based on the coordinates of the two endpoints in the line. In line 7 of the code below I am getting a compiler error saying that the constructor of the Line class cannot take two arguments. Why is that? And then at around line 30 and 31 I cannot get the GetLength method to recognize the bottomRight point and the origin point. Any advice would be appreciated, thanks!
class Program
{
static void doWork()
{
Point origin = new Point(0,0);
Point bottomRight = new Point(1366, 768);
Line myLine = new Line(bottomRight, origin);
//double distance = origin.DistanceTo(bottomRight);
double distance = GetLength(myLine);
Console.WriteLine("Distance is: {0}", distance);
Console.WriteLine("Number of Point objects: {0}", Point.ObjectCount());
Console.ReadLine();
}
static void Main(string[] args)
{
try
{
doWork();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static double GetLength(Line line)
{
Point pointA = line.bottomRight;
Point pointB = line.origin;
return pointA.DistanceTo(pointB);
}
}
class Line
{
static void Line(Point pointA, Point pointB)
{
pointA = new Point();
pointB = new Point();
}
}
And here is the code for the Point class:
class Point
{
private int x, y;
private static int objectCount = 0;
public Point()
{
this.x = -1;
this.y = -1;
objectCount++;
}
public Point(int x, int y)
{
this.x = x;
this.y = y;
objectCount++;
}
public double DistanceTo(Point other)
{
int xDiff = this.x - other.x;
int yDiff = this.y - other.y;
double distance = Math.Sqrt((xDiff * xDiff) + (yDiff * yDiff));
return distance;
}
public static int ObjectCount()
{
return objectCount;
}
}
Don't use void
public Line(Point pointA, Point pointB)
{
pointA = new Point();
pointB = new Point();
}
Your Line class looks incomplete. It doesn't store any data. It should looks more like this:
class Line
{
public Point BottomRight { get; set; }
public Point Origin { get; set; }
public Line(Point pointA, Point pointB)
{
Origin = pointA;
BottomRight = pointB;
}
}
Then you need to only change line.BottomRight and line.Origin in GetLength method:
static double GetLength(Line line)
{
Point pointA = line.BottomRight;
Point pointB = line.Origin;
return pointA.DistanceTo(pointB);
}
This should work now

Categories