Philip Harris

Philip Harris

  • NA
  • 45
  • 7.4k

Project help (fields and overloading methods)

Nov 14 2014 5:52 AM
I am kind of stuck and i am taking my class online because i am deployed please give me some advice on where to go from where I am at. I have been reading and trying to think about it but I believe I just need some advice for someone to push me the right way.
Thanks -Phil

HERE IS THE TASK
Code a class shapes2D that has fields for 3 sides, and PI, and overloaded methods (with the same name) to calculate hypotenuse, area, and perimeter for rectangle, right triangle, and circle. Let the compiler choose between the overloaded methods based on the number of side-parameters that you pass to the method. Circle will have one side (radius), triangle will call hypotenuse with 2 sides and area/perimeter with 3.

attached is where i am stuck
I also pasted it below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Shape2D
{
    class shape2d
    {
         public static double pi
        {
            get
            {
                return Math.PI;
            }
        }
         public double side1
         {
             set;
             get;
         }
         public double side2
         {
             set;
             get;
         }
         public double side3
         {
             set;
             get;
         }
        static void areasolve(double side1, double side2, double side3)
        {
            double area = ((side1 * side2) / 2);
            Console.WriteLine("The area of the triangle is {0}", area);
        }
        static void areasolve(double side1, double side2)
        {
            double area = side1 * side2;
            Console.WriteLine("The area of the rectangle is {0}", area);
        }
        static void areasolve(double side1)
        {
            double area = ( pi * (side1 * side1));
            Console.WriteLine("The area of the circle is {0}", area);
        }
        static void hypot(double side1, double side2)
        {
            double hypot = Math.Sqrt((side1 * side1) + (side2 * side2));
            Console.WriteLine("The hypotenuse of the triangle is {0}", hypot);
        }
        static void Prm(double side1, double side2, double side3)
        {
            double permim = side1 + side2 + side3;
            Console.WriteLine("The perimeter of the triangle is {0}", permim);
        }
        static void Prm(double side1, double side2)
        {
            double prerim = 2 * side1 + 2 * side2;
            Console.WriteLine("The perimeter of rectangle is {0}", prerim);
        }
        static void Prm(double side1)
        {
            double prerim = 2 * pi * side1;
            Console.WriteLine("The perimeter of the circle is {0}", prerim);
        }
    }
    class program
    {
        static void Main(string[] args)
        {
            shape2d s2d = new shape2d();
            string answer;
            Console.WriteLine("would you like to solve for a rectangle triangle or circle");
            answer = Console.ReadLine();
            if (answer == "rectangle")
            {
                Console.WriteLine("please enter side1");
                s2d.side1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("please enter side2");
                s2d.side2 = Convert.ToDouble(Console.ReadLine());
            }
            else if (answer == "circle")
            {
                Console.WriteLine("please enter the radious");
                s2d.side1 = Convert.ToDouble(Console.ReadLine());
            }
            else
            {
                string quest;
                Console.WriteLine("do you know the hypotenuse yes or no");
                quest = Console.ReadLine();
                if (quest == "yes")
                {
                    Console.WriteLine("please enter the hypotenuse");
                    s2d.side1 = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("please enter side1");
                    s2d.side2 = Convert.ToDouble(Console.ReadLine());
                }
                else
                {
                    Console.WriteLine("please enter side1");
                s2d.side1 = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("please enter side2");
                s2d.side2 = Convert.ToDouble(Console.ReadLine());
                }
            }
            Console.ReadKey();

        }
    }
}

Answers (3)