Skip to content

Can you help me with a few programs.

Shraddha Bhat

Asked Apr 25, 2013, 12:37 AM EDT1 answer
Share
LinkedInX
1)Write a program to define a class vehicle and a class engine. Let the vehicle class contain engine. The
engine should be started at the source and stopped after reaching the destination .Let the speed and the
distance be read. The program should throw an exception when the vehicle tries to travel beyond
destination.Develop the application in C#.


1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    i wud like to kno if what i ve tried on the above mentnd question is right.

    C#
    namespace T1
    {
        public class BeyondDestinationException :Exception
        {
            public BeyondDestinationException(string Message) :base(Message) { }
            public BeyondDestinationException() { }
            public BeyondDestinationException(string Message ,Exception inner):base(Message,inner) {}
        }
        public class Engine
        {
            public static void turnOn()
            {
                Console.WriteLine("Engine Started");
            }
            public static void Running()
            {
                Console.WriteLine("\n Vehicle Engine Running Smoothly\n");
            }
            public static void stopped()
            {
                Console.WriteLine("\nEngine Stopped\n");
            }
        }
        public class Vehicle
        {
            Engine E= new Engine( );
            public  double Speed;
            public  double distance=0 , maxdistance;
            public  void Run( double s)
            {
                try
                {
                    if (distance ==0)
                    {
                        Engine.turnOn ();
                        distance+=1;
                    }
                    else if (distance>0 && distance <=maxdistance)
                    {
                        Engine.Running();
                        distance = distance + s;
                    }
                    else
                    {
                        Speed =0.0;
                        Engine.stopped();
                        throw new BeyondDestinationException("The Vehicle is beyond the destination and hence ");
    
                    }
                }
                catch (BeyondDestinationException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }
       public class MainApp
       {
            public static void Main()
            {
                Vehicle v = new Vehicle ();
                Console .WriteLine("Enter the distance of destination from the starting station(source)" );
                v.maxdistance = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter the speed:") ;
                v.Speed= double.Parse(Console.ReadLine());
                for(int d=0;d<=v.maxdistance+1;d++)
                {
                    v.Run (v.Speed);
                    if (v.Speed == 0)
                        break;
                }
                Console.ReadKey();
            }
        }
    }
    0