C#: I need assitance on my code.. I\'m getting an error message \"Use of unassigned local
variable for \"rectangle1 ==2, Square2 == 2, Triangle3 == 3\" on the ShapeDemo Application.
Can you please help me? Here my code:
namespace Lab7ShapeDemo
{
class Triangle : GeometricFigure
{
public Triangle() { }
public Triangle (double height, double width)
{
}
public override double ComputeArea()
{
return ((1 / 2.0) * (width * height));
}
}
}// end on Triangle class
class Square : Triangle
{
public Square() { }
public Square(double height, double width) : base (height, height) { }
public Square (double heightWidth) : base (heightWidth, heightWidth) { }
public override double ComputeArea()
{
return base.ComputeArea();
}
}
} // end of Square class
class Rectangle : GeometricFigure
{
public Rectangle() { }
public Rectangle(double height, double width)
{
}
public override double ComputeArea()
{
return (height * width);
}
}
} //End of REctangle class
public GeometricFigure()
{
height = 0;
width = 0;
area = 0;
}
public GeometricFigure(double height, double width)
{
this.height = height;
this.width = width;
this.area = ComputeArea();
}
public abstract double ComputeArea();
}
} //End of GeometircFigure class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab7ShapeDemo
{
class ShapesDemo
{
static void Main(string[] args)
{
string Rectangle_HeightString;
string Rectangle_WidthString;
string Square_HeightString;
string Triangle_HeightString;
string Triangle_WidthString;
string Enter_NumberString;
double Rectangle_height;
double Rectangle_Width;
double Square_height;
double Triangle_Height;
double Triangle_Width;
double Enter_Number;
Console.Write(\"Choose a shape from the following options:\");
Console.WriteLine(\"[1] Rectangle\");
Console.WriteLine(\"[2] Square\");
Console.WriteLine(\"[3} Triangle\");
Console.Write(\"Enter the option number (or zero to terminate this program): \");
Enter_NumberString = Console.ReadLine();
Enter_Number = Convert.ToDouble(Enter_NumberString);
Console.WriteLine();
int rectangle1, Square2, Triangle3;
while (Enter_Number != 0)
{
if (rectangle1 == 1)
{
Console.Write(\"Enter the hight of the rectangle: \");
Rectangle_HeightString = Console.ReadLine();
Rectangle_height = Convert.ToDouble(Rectangle_HeightString);
Console.Write(\"Enter the width of the rectanlge: \");
Rectangle_WidthString = Console.ReadLine();
Rectangle_Width = Convert.ToDouble(Rectangle_WidthString);
Rectangle r1 = new Rectangle(Rectangle_height, Rectangle_Width);
displayGeometricFigure(r1);
}
else if (Square2 == 2)
{
Console.Write(\"Enter the height of the Square\");
Square_HeightString = Console.ReadLine();
Square_height = Convert.ToDouble(Square_HeightString);
Square sq1 = new Square(Square_height);
displayGeometricFigure(sq1);
}
else if (Triangle3 == 3)
{
Console.Write(\"Enter the hight of the Triangle\");
Triangle_HeightString = Console.ReadLine();
Triangle_Height = Convert.ToDouble(Tria.
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
C# I need assitance on my code.. Im getting an error message Us.pdf
1. C#: I need assitance on my code.. I'm getting an error message "Use of unassigned local
variable for "rectangle1 ==2, Square2 == 2, Triangle3 == 3" on the ShapeDemo Application.
Can you please help me? Here my code:
namespace Lab7ShapeDemo
{
class Triangle : GeometricFigure
{
public Triangle() { }
public Triangle (double height, double width)
{
}
public override double ComputeArea()
{
return ((1 / 2.0) * (width * height));
}
}
}// end on Triangle class
class Square : Triangle
{
public Square() { }
public Square(double height, double width) : base (height, height) { }
public Square (double heightWidth) : base (heightWidth, heightWidth) { }
public override double ComputeArea()
{
return base.ComputeArea();
}
}
} // end of Square class
class Rectangle : GeometricFigure
{
public Rectangle() { }
public Rectangle(double height, double width)
{
}
public override double ComputeArea()
2. {
return (height * width);
}
}
} //End of REctangle class
public GeometricFigure()
{
height = 0;
width = 0;
area = 0;
}
public GeometricFigure(double height, double width)
{
this.height = height;
this.width = width;
this.area = ComputeArea();
}
public abstract double ComputeArea();
}
} //End of GeometircFigure class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab7ShapeDemo
{
class ShapesDemo
{
static void Main(string[] args)
{
string Rectangle_HeightString;
string Rectangle_WidthString;
string Square_HeightString;
string Triangle_HeightString;
string Triangle_WidthString;
3. string Enter_NumberString;
double Rectangle_height;
double Rectangle_Width;
double Square_height;
double Triangle_Height;
double Triangle_Width;
double Enter_Number;
Console.Write("Choose a shape from the following options:");
Console.WriteLine("[1] Rectangle");
Console.WriteLine("[2] Square");
Console.WriteLine("[3} Triangle");
Console.Write("Enter the option number (or zero to terminate this program): ");
Enter_NumberString = Console.ReadLine();
Enter_Number = Convert.ToDouble(Enter_NumberString);
Console.WriteLine();
int rectangle1, Square2, Triangle3;
while (Enter_Number != 0)
{
if (rectangle1 == 1)
{
Console.Write("Enter the hight of the rectangle: ");
Rectangle_HeightString = Console.ReadLine();
Rectangle_height = Convert.ToDouble(Rectangle_HeightString);
Console.Write("Enter the width of the rectanlge: ");
Rectangle_WidthString = Console.ReadLine();
Rectangle_Width = Convert.ToDouble(Rectangle_WidthString);
Rectangle r1 = new Rectangle(Rectangle_height, Rectangle_Width);
displayGeometricFigure(r1);
}
else if (Square2 == 2)
{
Console.Write("Enter the height of the Square");
Square_HeightString = Console.ReadLine();
Square_height = Convert.ToDouble(Square_HeightString);
Square sq1 = new Square(Square_height);
4. displayGeometricFigure(sq1);
}
else if (Triangle3 == 3)
{
Console.Write("Enter the hight of the Triangle");
Triangle_HeightString = Console.ReadLine();
Triangle_Height = Convert.ToDouble(Triangle_HeightString);
Console.Write("Enter the width of the Triangle: ");
Triangle_WidthString = Console.ReadLine();
Triangle_Width = Convert.ToInt16(Triangle_WidthString);
Triangle t1 = new Triangle(Triangle_Height, Triangle_Width);
displayGeometricFigure(t1);
Console.WriteLine();
Console.ReadKey();
}
else
{
Console.Write("Enter the option number (or zero to terminate this program): ");
}
}
}
public static void displayGeometricFigure(GeometricFigure gf)
{
Console.WriteLine("The shape is: ", gf);
Console.WriteLine("The shape height is: ", gf);
Console.WriteLine("The shape width is: ", gf);
Console.WriteLine("The shape area is: ", gf);
Console.WriteLine();
}
}
}
Here the Demo Below:
Solution
Your code is incomplete it seems. You missed the starting lines of the GeometricFigure class.
5. Anyways, going through your code, and the error you specified, the problem is with the Main()
class. There, you read the option into a variable called Enter_Number, and that is what you
should compare in the If-else block, and not with variables recatangle1, square2, and triangle3.
Here is the modified version for you:
namespace Lab7ShapeDemo
{
class Triangle : GeometricFigure
{
public Triangle() { }
public Triangle (double height, double width)
{
}
public override double ComputeArea()
{
return ((1 / 2.0) * (width * height));
}
}
}// end on Triangle class
class Square : Triangle
{
public Square() { }
public Square(double height, double width) : base (height, height) { }
public Square (double heightWidth) : base (heightWidth, heightWidth) { }
public override double ComputeArea()
{
return base.ComputeArea();
}
}
} // end of Square class
class Rectangle : GeometricFigure
{
public Rectangle() { }
public Rectangle(double height, double width)
{
}
public override double ComputeArea()
6. {
return (height * width);
}
}
} //End of REctangle class
public GeometricFigure()
{
height = 0;
width = 0;
area = 0;
}
public GeometricFigure(double height, double width)
{
this.height = height;
this.width = width;
this.area = ComputeArea();
}
public abstract double ComputeArea();
}
} //End of GeometircFigure class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lab7ShapeDemo
{
class ShapesDemo
{
static void Main(string[] args)
{
string Rectangle_HeightString;
string Rectangle_WidthString;
string Square_HeightString;
string Triangle_HeightString;
string Triangle_WidthString;
7. string Enter_NumberString;
double Rectangle_height;
double Rectangle_Width;
double Square_height;
double Triangle_Height;
double Triangle_Width;
double Enter_Number;
Console.Write("Choose a shape from the following options:");
Console.WriteLine("[1] Rectangle");
Console.WriteLine("[2] Square");
Console.WriteLine("[3} Triangle");
Console.Write("Enter the option number (or zero to terminate this program): ");
Enter_NumberString = Console.ReadLine();
Enter_Number = Convert.ToDouble(Enter_NumberString);
Console.WriteLine();
int rectangle1, Square2, Triangle3;
while (Enter_Number != 0)
{
if (Enter_Number == 1)
{
Console.Write("Enter the hight of the rectangle: ");
Rectangle_HeightString = Console.ReadLine();
Rectangle_height = Convert.ToDouble(Rectangle_HeightString);
Console.Write("Enter the width of the rectanlge: ");
Rectangle_WidthString = Console.ReadLine();
Rectangle_Width = Convert.ToDouble(Rectangle_WidthString);
Rectangle r1 = new Rectangle(Rectangle_height, Rectangle_Width);
displayGeometricFigure(r1);
}
else if (Enter_Number == 2)
{
Console.Write("Enter the height of the Square");
Square_HeightString = Console.ReadLine();
Square_height = Convert.ToDouble(Square_HeightString);
Square sq1 = new Square(Square_height);
displayGeometricFigure(sq1);
8. }
else if (Enter_Number == 3)
{
Console.Write("Enter the hight of the Triangle");
Triangle_HeightString = Console.ReadLine();
Triangle_Height = Convert.ToDouble(Triangle_HeightString);
Console.Write("Enter the width of the Triangle: ");
Triangle_WidthString = Console.ReadLine();
Triangle_Width = Convert.ToInt16(Triangle_WidthString);
Triangle t1 = new Triangle(Triangle_Height, Triangle_Width);
displayGeometricFigure(t1);
Console.WriteLine();
Console.ReadKey();
}
else
{
Console.Write("Enter the option number (or zero to terminate this program): ");
}
}
}
public static void displayGeometricFigure(GeometricFigure gf)
{
Console.WriteLine("The shape is: ", gf);
Console.WriteLine("The shape height is: ", gf);
Console.WriteLine("The shape width is: ", gf);
Console.WriteLine("The shape area is: ", gf);
Console.WriteLine();
}
}
}
I just highlighted the modifications I did. If you still face any problem, just get back to me.