Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. //Rextester.Program.Main is the entry point for your code. Don't change it.
  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
  3.  
  4. using System;
  5. namespace filler
  6. {
  7. class GasProgram
  8. {
  9. class Trip {
  10. //Declaration of the instance variables of the
  11. //class private
  12. string destination;
  13. private double distanceTravelled;
  14. private double totalGasolineCost;
  15. private double gallonsConsumed;
  16. //Default constructor of the class
  17. public Trip() { }
  18. // 2 parameter constructor of the class with the
  19. // parameters as destination and the distance
  20. // travelled
  21. public Trip(string place, double distance) {
  22. destination = place;
  23. distanceTravelled = distance;
  24. }
  25. //end of constructor
  26. //4 parameter constructor of the class with
  27. //the parameters as destination, distance
  28. //travelled, cost of gasoline and the
  29. //number of gallons consumed
  30. public Trip(string place, double distance, double cost, double gallons) {
  31. destination = place;
  32. distanceTravelled = distance;
  33. totalGasolineCost = cost;
  34. gallonsConsumed = gallons;
  35. }
  36. //end of constructor
  37. //Property acting as mutator and accessor
  38. //for destination
  39. public string Destination {
  40. get {
  41. return destination;
  42. }
  43. //end of get
  44. set {
  45. destination = value;
  46. }
  47. //end of set
  48. }
  49. //end of property
  50. //Property acting as mutator and accessor
  51. //for distance travelled
  52. public double DistanceTravelled {
  53. get {
  54. return distanceTravelled;
  55. }
  56. //end of get
  57. set {
  58. distanceTravelled = value;
  59. }
  60. //end of set
  61. }
  62. //end of property
  63. //Property acting as mutator and accessor
  64. //for total gasoline cost
  65. public double TotalGasolineCost {
  66. get {
  67. return totalGasolineCost;
  68. }
  69. //end of get
  70. set {
  71. totalGasolineCost = value;
  72. }
  73. //end of set
  74. }
  75. //end of property
  76. //Property acting as mutator and accessor
  77. //for number of gallons consumed
  78. public double GallonsConsumed {
  79. get {
  80. return gallonsConsumed;
  81. }
  82. //end of get
  83. set {
  84. gallonsConsumed = value;
  85. }
  86. //end of set
  87. }
  88. //end of property
  89. //Method to return number of
  90. //miles travelled per gallon
  91. public double milesPerGallon() {
  92. return (distanceTravelled / gallonsConsumed);
  93. }
  94. //end of method
  95. //Method to calculate the cost of
  96. //gasoline consumed per mile
  97. public double costPerMile() {
  98. return (distanceTravelled / totalGasolineCost);
  99. }
  100. //end of method
  101. //Overriding the ToString method to give the
  102. //details of the Trip class
  103. public override string ToString() {
  104. 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");
  105. }
  106. //end of method
  107. }
  108. //end of class
  109. //Class for testing the Trip class
  110. class Program {
  111. static void Main(String[] args) {
  112. //Declaration and initialization of an
  113. //object of class Trip with the default
  114. //constructor
  115. Trip t1 = new Trip();
  116. //Assigning the values to instance
  117. //variables through the properties
  118. //of the class
  119. t1.Destination = "Shimla";
  120. t1.DistanceTravelled = 1290;
  121. t1.GallonsConsumed = 37;
  122. t1.TotalGasolineCost = 17000;
  123. //Printing the required details about the class
  124. //by the overridden ToString method
  125. Console.WriteLine("Details about the first" + " trip: "); Console.WriteLine(t1);
  126. //Declaration and initialization of an //parameter constructor
  127. Trip t2 = new Trip("Jaipur", 1498);
  128. //Assigning the values to left out
  129. //instance variables through the
  130. //properties of the class
  131. t2.GallonsConsumed = 41;
  132. t2.TotalGasolineCost = 21890;
  133. //Printing the required details about the class
  134. //by the overridden ToString method
  135. Console.WriteLine("\nDetails about the second"+ " trip: ");
  136. Console.WriteLine(t2);
  137. //Declaration and initialization of an
  138. //object of class Employee with the 4
  139. //parameter constructor
  140. Trip t3 = new Trip("Murthal", 1050, 14508, 28);
  141. //Printing the required details about the class
  142. //by the overridden ToString method
  143. Console.WriteLine("\nDetails about the third" + " trip: ");
  144. Console.WriteLine(t3); Console.ReadLine();
  145. }
  146. //end of main
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement