Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rextester.Program.Main is the entry point for your code. Don't change it.
- //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
- using System;
- namespace filler
- {
- class GasProgram
- {
- class Trip {
- //Declaration of the instance variables of the
- //class private
- string destination;
- private double distanceTravelled;
- private double totalGasolineCost;
- private double gallonsConsumed;
- //Default constructor of the class
- public Trip() { }
- // 2 parameter constructor of the class with the
- // parameters as destination and the distance
- // travelled
- public Trip(string place, double distance) {
- destination = place;
- distanceTravelled = distance;
- }
- //end of constructor
- //4 parameter constructor of the class with
- //the parameters as destination, distance
- //travelled, cost of gasoline and the
- //number of gallons consumed
- public Trip(string place, double distance, double cost, double gallons) {
- destination = place;
- distanceTravelled = distance;
- totalGasolineCost = cost;
- gallonsConsumed = gallons;
- }
- //end of constructor
- //Property acting as mutator and accessor
- //for destination
- public string Destination {
- get {
- return destination;
- }
- //end of get
- set {
- destination = value;
- }
- //end of set
- }
- //end of property
- //Property acting as mutator and accessor
- //for distance travelled
- public double DistanceTravelled {
- get {
- return distanceTravelled;
- }
- //end of get
- set {
- distanceTravelled = value;
- }
- //end of set
- }
- //end of property
- //Property acting as mutator and accessor
- //for total gasoline cost
- public double TotalGasolineCost {
- get {
- return totalGasolineCost;
- }
- //end of get
- set {
- totalGasolineCost = value;
- }
- //end of set
- }
- //end of property
- //Property acting as mutator and accessor
- //for number of gallons consumed
- public double GallonsConsumed {
- get {
- return gallonsConsumed;
- }
- //end of get
- set {
- gallonsConsumed = value;
- }
- //end of set
- }
- //end of property
- //Method to return number of
- //miles travelled per gallon
- public double milesPerGallon() {
- return (distanceTravelled / gallonsConsumed);
- }
- //end of method
- //Method to calculate the cost of
- //gasoline consumed per mile
- public double costPerMile() {
- return (distanceTravelled / totalGasolineCost);
- }
- //end of method
- //Overriding the ToString method to give the
- //details of the Trip class
- public override string ToString() {
- return "Destination: " + destination + "\nDistance Travelled: " + distanceTravelled + " miles" + "\nTotal Gasoline Cost: $ " + totalGasolineCost.ToString("F2") + "\nGallons Consumed: " + gallonsConsumed + "\nMiles(Per Gallon): " + milesPerGallon().ToString("F3") + "\nCost(Per Mile): $ " + costPerMile().ToString("F2");
- }
- //end of method
- }
- //end of class
- //Class for testing the Trip class
- class Program {
- static void Main(String[] args) {
- //Declaration and initialization of an
- //object of class Trip with the default
- //constructor
- Trip t1 = new Trip();
- //Assigning the values to instance
- //variables through the properties
- //of the class
- t1.Destination = "Shimla";
- t1.DistanceTravelled = 1290;
- t1.GallonsConsumed = 37;
- t1.TotalGasolineCost = 17000;
- //Printing the required details about the class
- //by the overridden ToString method
- Console.WriteLine("Details about the first" + " trip: "); Console.WriteLine(t1);
- //Declaration and initialization of an //parameter constructor
- Trip t2 = new Trip("Jaipur", 1498);
- //Assigning the values to left out
- //instance variables through the
- //properties of the class
- t2.GallonsConsumed = 41;
- t2.TotalGasolineCost = 21890;
- //Printing the required details about the class
- //by the overridden ToString method
- Console.WriteLine("\nDetails about the second"+ " trip: ");
- Console.WriteLine(t2);
- //Declaration and initialization of an
- //object of class Employee with the 4
- //parameter constructor
- Trip t3 = new Trip("Murthal", 1050, 14508, 28);
- //Printing the required details about the class
- //by the overridden ToString method
- Console.WriteLine("\nDetails about the third" + " trip: ");
- Console.WriteLine(t3); Console.ReadLine();
- }
- //end of main
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement