Advertisement
Dakpluto

Car.java

Oct 12th, 2014
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Car {
  4.     private String name;
  5.     private int speed;
  6.     private int location;
  7.  
  8.     private static int minimumSpeed = 0;
  9.     private static int maximumSpeed = 120;
  10.  
  11.     public static int getMinimumSpeed() {
  12.         return minimumSpeed;
  13.     }
  14.  
  15.     public static void setMinimumSpeed(int minimumSpeed) {
  16.         Car.minimumSpeed = minimumSpeed;
  17.     }
  18.  
  19.     public static int getMaximumSpeed() {
  20.         return maximumSpeed;
  21.     }
  22.  
  23.     public static void setMaximumSpeed(int maximumSpeed) {
  24.         Car.maximumSpeed = maximumSpeed;
  25.     }
  26.  
  27.     public Car(String name, int speed) {
  28.         this.name = name;
  29.         this.setLocation(0);
  30.         this.speed = speed;
  31.     }
  32.  
  33.     public String getName() {
  34.         return name;
  35.     }
  36.  
  37.     public void setName(String name) {
  38.         this.name = name;
  39.     }
  40.  
  41.     public int getSpeed() {
  42.         return speed;
  43.     }
  44.  
  45.     public void setSpeed(int speed) {
  46.         if (speed > Car.maximumSpeed) {
  47.             this.speed = Car.maximumSpeed;
  48.         } else if (speed < Car.minimumSpeed) {
  49.             this.speed = Car.minimumSpeed;
  50.         } else {
  51.             this.speed = speed;
  52.         }
  53.     }
  54.  
  55.     public int getLocation() {
  56.         return location;
  57.     }
  58.  
  59.     public void setLocation(int location) {
  60.         this.location = location;
  61.     }
  62.  
  63.     public String toString() {
  64.         return "Car [name=" + name + ", speed=" + speed + ", location="
  65.                 + location + "]";
  66.     }
  67.  
  68.     public void accelerate() {
  69.         this.speed += 2;
  70.         if (this.speed > Car.maximumSpeed) {
  71.             this.speed = Car.maximumSpeed;
  72.         }
  73.     }
  74.  
  75.     public void decelerate() {
  76.         this.speed -= 2;
  77.         if (this.speed < Car.minimumSpeed) {
  78.             this.speed = Car.minimumSpeed;
  79.         }
  80.     }
  81.  
  82.     public int randomSpeedChange() {
  83.         int result;
  84.         Random randomSpeed = new Random();
  85.         result = (randomSpeed.nextInt(21) - 10);
  86.         return result;
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement