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#.
Shraddha Bhat10 pointsPosted Apr 25, 2013, 11:34 AM EDT
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(); } } }