Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////// Main Class //////////////////////////////////
- import java.util.Scanner;
- public class CarView {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Car car = new Car();
- int speed;
- car.setCarID(53940901);
- System.out.println("Please enter the initial speed of the car: ");
- speed = sc.nextInt();
- acc(car, speed);
- car.show();
- car.deceleration();
- car.deceleration();
- car.show();
- car.stop();
- car.show();
- }
- private static void acc(Car c, int sp) {
- for(int i = 0; i < sp; i+=1) {
- c.accalerate();
- }
- }
- }
- //////////////////////////////// Car Class ////////////////////////////////////
- public class Car {
- // Declaring class properties
- private int carID, currentSpeed;
- public int getCarID() {
- return carID;
- }
- public void setCarID(int carID) {
- this.carID = carID;
- }
- public int getCurrentSpeed() {
- return currentSpeed;
- }
- public void accalerate() {
- currentSpeed += 1;
- }
- public void deceleration() {
- currentSpeed -= 1;
- }
- public void stop() {
- currentSpeed = 0;
- }
- public void show() {
- System.out.printf("The car number is %d and the speed is %d.\n", carID, currentSpeed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement