Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package SpeedRacing_03;
- public class Car {
- //Model, fuel amount, fuel cost for 1 kilometer and distance traveled.
- private String model;
- private double fuel;
- private double consumption;
- private int distance;
- public Car(String model, double fuel, double consumption) {
- this.model = model;
- this.fuel = fuel;
- this.consumption = consumption;
- this.distance = 0;
- }
- public boolean drive(int distanceToDrive) {
- //разстояние -> колко гориво ще ни отнеме
- double needFuel = distanceToDrive * this.consumption;
- if(needFuel <= this.fuel) {
- //изминаваме разстоянието
- this.distance += distanceToDrive;
- this.fuel -= needFuel;
- return true;
- }
- return false;
- }
- @Override
- public String toString() {
- //AudiA4 1.00 50
- return String.format("%s %.2f %d", this.model, this.fuel, this.distance);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement