Advertisement
eranseg

Car

Aug 21st, 2019
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. //////////////////////////// Main Class //////////////////////////////////
  2. import java.util.Scanner;
  3.  
  4. public class CarView {
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.         Car car = new Car();
  8.         int speed;
  9.         car.setCarID(53940901);
  10.         System.out.println("Please enter the initial speed of the car: ");
  11.         speed = sc.nextInt();
  12.         acc(car, speed);
  13.         car.show();
  14.         car.deceleration();
  15.         car.deceleration();
  16.         car.show();
  17.         car.stop();
  18.         car.show();
  19.     }
  20.  
  21.     private static void acc(Car c, int sp) {
  22.         for(int i = 0; i < sp; i+=1) {
  23.             c.accalerate();
  24.         }
  25.     }
  26. }
  27.  
  28. //////////////////////////////// Car Class ////////////////////////////////////
  29. public class Car {
  30.  
  31.     // Declaring class properties
  32.     private int carID, currentSpeed;
  33.  
  34.     public int getCarID() {
  35.         return carID;
  36.     }
  37.  
  38.     public void setCarID(int carID) {
  39.         this.carID = carID;
  40.     }
  41.  
  42.     public int getCurrentSpeed() {
  43.         return currentSpeed;
  44.     }
  45.  
  46.     public void accalerate() {
  47.         currentSpeed += 1;
  48.     }
  49.  
  50.     public void deceleration() {
  51.         currentSpeed -= 1;
  52.     }
  53.  
  54.     public void stop() {
  55.         currentSpeed = 0;
  56.     }
  57.  
  58.     public void show() {
  59.         System.out.printf("The car number is %d and the speed is %d.\n", carID, currentSpeed);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement