Home > Net >  Creating and comparing objects based on user input
Creating and comparing objects based on user input

Time:06-06

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());
            }
        }
    }
}

CodePudding user response:

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.

CodePudding user response:

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();
            }

CodePudding user response:

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;
    }
  •  Tags:  
  • c#
  • Related