Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. public class MyCars {
  2. public static void main() {
  3. Fuel fuel = new Fuel(5);
  4. Car carnr1 = new Car(fuel, "Mercedes", "C65", 3.0, 320, 13);
  5. Car carnr2 = new Car(fuel, "Opel", "Insignia", 2.0, 160, 11);
  6.  
  7. System.out.println(carnr1.getInfo());
  8. System.out.println(carnr1.fuelCost(200));
  9.  
  10. System.out.println(carnr2.getInfo());
  11. System.out.println(carnr2.fuelCost(250));
  12. }
  13.  
  14. public class Fuel {
  15. private double _cost;
  16.  
  17. public Fuel(cost) {
  18. _cost = cost;
  19. }
  20.  
  21. double getCost() {
  22. return _cost;
  23. }
  24.  
  25. void changeCost(double cost) {
  26. _cost = cost;
  27. }
  28. }
  29.  
  30. public class Car {
  31. private String carBrand;
  32. private String carModel;
  33. private double carEngine;
  34. private int horsePower;
  35. private double fuelConsumption;
  36. private Fuel fuel;
  37.  
  38. public Car(Fuel fuel, String carBrand, String carModel, double carEngine, int horsePower, double fuelConsumption) {
  39. this.fuel = fuel;
  40. this.carBrand = carBrand;
  41. this.carModel = carModel;
  42. this.carEngine = carEngine;
  43. this.horsePower = horsePower;
  44. this.fuelConsumption = fuelConsumption;
  45. }
  46.  
  47. String getInfo() {
  48. return carBrand + " " + carModel + "; " + carEngine + "; " + horsePower + "KM" + "; " + fuelConsumption + " l/100km;";
  49. }
  50.  
  51. String fuelCost(double kilometers) {
  52. double totalCost = fuelConsumption * (kilometers/100) * fuel.getCost();
  53. return "Koszt przejechania " + kilometers + " kilometrow autem " + carBrand + " " + carModel + " to " + totalCost + "zl.";
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement