Advertisement
Guest User

Solution to Challenge "Throwing Exceptions"

a guest
Jan 2nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. class GoKart {
  2.   public static final int MAX_BARS = 8;
  3.   private String color;
  4.   private int barCount;
  5.   private int lapsDriven;
  6.  
  7.   public GoKart(String color) {
  8.     this.color = color;
  9.   }
  10.  
  11.   public String getColor() {
  12.     return color;
  13.   }
  14.   public void charge() {
  15.     barCount = MAX_BARS;
  16.   }
  17.  
  18.   public boolean isBatteryEmpty() {
  19.     return barCount == 0;
  20.   }
  21.  
  22.   public boolean isFullyCharged() {
  23.     return MAX_BARS == barCount;
  24.   }
  25.  
  26.   public void drive() {
  27.     drive(1);
  28.   }
  29.  
  30.   public void drive(int laps) {
  31.     if (laps > barCount) {
  32.       throw new IllegalArgumentException();
  33.     }
  34.     lapsDriven += laps;
  35.     barCount -= laps;
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement