Advertisement
mmayoub

classes, Car tester, Exercise 02 slide 47

Jul 7th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package class170706;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CarTester {
  6.  
  7.     public static void main(String[] args) {
  8.         // create a scanner
  9.         Scanner s = new Scanner(System.in);
  10.  
  11.         // create a new car
  12.         Car myCar = new Car();
  13.         System.out.printf("default car was created: %s\n\n", myCar);
  14.  
  15.         do {
  16.             System.out.print("Enter car Number: ");
  17.         } while (!myCar.setCarNumber(s.nextInt()));
  18.         System.out.printf("car number was updated successfuly to: %d\n",
  19.                 myCar.getCarNumber());
  20.  
  21.         int targetSpeed;
  22.         System.out
  23.                 .print("Enter target speed (positive=forward , negative=backword: ");
  24.         targetSpeed = s.nextInt();
  25.  
  26.         System.out.println("\ncar start moving . . .");
  27.         if (targetSpeed > 0) {
  28.             // start moving forward
  29.             myCar.accelerate();
  30.         } else {
  31.             // start moving backward
  32.             myCar.decelerate();
  33.         }
  34.         System.out.printf("car is accelerating: %s\n", myCar);
  35.  
  36.         // continue moving
  37.         for (int i = 1; i < Math.abs(targetSpeed); i += 1) {
  38.             // speed up in the same direction
  39.             myCar.accelerate();
  40.             System.out.printf("car is accelerating: %s\n", myCar);
  41.         }
  42.  
  43.         // decelerating 2km/h
  44.         System.out.println("\nstart decelerating 2 km/h . . .");
  45.         myCar.decelerate();
  46.         System.out.printf("car is decelerating: %s\n", myCar);
  47.         myCar.decelerate();
  48.         System.out.printf("car is decelerating: %s\n", myCar);
  49.  
  50.         // stop the car
  51.         myCar.stop();
  52.         System.out.printf("\ncar has stopped %s\n", myCar);
  53.     }
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement