Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Car {
- private String name;
- private int speed;
- private int location;
- private static int minimumSpeed = 0;
- private static int maximumSpeed = 120;
- public static int getMinimumSpeed() {
- return minimumSpeed;
- }
- public static void setMinimumSpeed(int minimumSpeed) {
- Car.minimumSpeed = minimumSpeed;
- }
- public static int getMaximumSpeed() {
- return maximumSpeed;
- }
- public static void setMaximumSpeed(int maximumSpeed) {
- Car.maximumSpeed = maximumSpeed;
- }
- public Car(String name, int speed) {
- this.name = name;
- this.setLocation(0);
- this.speed = speed;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getSpeed() {
- return speed;
- }
- public void setSpeed(int speed) {
- if (speed > Car.maximumSpeed) {
- this.speed = Car.maximumSpeed;
- } else if (speed < Car.minimumSpeed) {
- this.speed = Car.minimumSpeed;
- } else {
- this.speed = speed;
- }
- }
- public int getLocation() {
- return location;
- }
- public void setLocation(int location) {
- this.location = location;
- }
- public String toString() {
- return "Car [name=" + name + ", speed=" + speed + ", location="
- + location + "]";
- }
- public void accelerate() {
- this.speed += 2;
- if (this.speed > Car.maximumSpeed) {
- this.speed = Car.maximumSpeed;
- }
- }
- public void decelerate() {
- this.speed -= 2;
- if (this.speed < Car.minimumSpeed) {
- this.speed = Car.minimumSpeed;
- }
- }
- public int randomSpeedChange() {
- int result;
- Random randomSpeed = new Random();
- result = (randomSpeed.nextInt(21) - 10);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement