Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MyCars {
- public static void main() {
- Fuel fuel = new Fuel(5);
- Car carnr1 = new Car(fuel, "Mercedes", "C65", 3.0, 320, 13);
- Car carnr2 = new Car(fuel, "Opel", "Insignia", 2.0, 160, 11);
- System.out.println(carnr1.getInfo());
- System.out.println(carnr1.fuelCost(200));
- System.out.println(carnr2.getInfo());
- System.out.println(carnr2.fuelCost(250));
- }
- public class Fuel {
- private double _cost;
- public Fuel(cost) {
- _cost = cost;
- }
- double getCost() {
- return _cost;
- }
- void changeCost(double cost) {
- _cost = cost;
- }
- }
- public class Car {
- private String carBrand;
- private String carModel;
- private double carEngine;
- private int horsePower;
- private double fuelConsumption;
- private Fuel fuel;
- public Car(Fuel fuel, String carBrand, String carModel, double carEngine, int horsePower, double fuelConsumption) {
- this.fuel = fuel;
- this.carBrand = carBrand;
- this.carModel = carModel;
- this.carEngine = carEngine;
- this.horsePower = horsePower;
- this.fuelConsumption = fuelConsumption;
- }
- String getInfo() {
- return carBrand + " " + carModel + "; " + carEngine + "; " + horsePower + "KM" + "; " + fuelConsumption + " l/100km;";
- }
- String fuelCost(double kilometers) {
- double totalCost = fuelConsumption * (kilometers/100) * fuel.getCost();
- return "Koszt przejechania " + kilometers + " kilometrow autem " + carBrand + " " + carModel + " to " + totalCost + "zl.";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement