University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Bil.java 827B

123456789101112131415161718192021222324252627282930313233343536
  1. class Bil {
  2. private int kilometerstand;
  3. private int bensin;
  4. private final int tankStorrelse;
  5. private final int kmPerLiter;
  6. public void kjorTur(int km) throws Exception {
  7. if (km > hentMaksDistanse()) {
  8. throw new Exception("Du har ikke nok bensin..");
  9. } else {
  10. kilometerstand += km;
  11. bensin -= km / kmPerLiter;
  12. }
  13. }
  14. public void refuel(double liter) throws Exception {
  15. if (bensin + liter > tankStorrelse)
  16. throw new Exception("Tanken din har ikke plass til saa mye benin.");
  17. else
  18. bensin += liter;
  19. }
  20. public double hentMaksDistanse() {
  21. return (double)bensin * (double)kmPerLiter;
  22. }
  23. public int hentKilometerstand() {
  24. return kilometerstand;
  25. }
  26. Bil(int tankStorrelse, int kmPerLiter) {
  27. this.tankStorrelse = tankStorrelse;
  28. this.kmPerLiter = kmPerLiter;
  29. this.bensin = tankStorrelse;
  30. }
  31. }