Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. package com.company;
  2.  
  3. //Написать класс "Автомобиль", который должен уметь заводиться, глушить мотор, ехать и держать необходимую скорость.
  4.  
  5. class Automobile {
  6.  
  7. private boolean starter;
  8. private double speed;
  9.  
  10. void turnOn() {
  11.  
  12. starter = true;
  13. System.out.println("Engine is On");
  14.  
  15. }
  16.  
  17. void turnOff() {
  18.  
  19. starter = false;
  20. System.out.println("Engine is Off");
  21. }
  22.  
  23. void setSpeed(double speedset, double acceleration) {
  24.  
  25. if (starter) {
  26.  
  27. if (this.speed < speedset) {
  28.  
  29. while (this.speed != speedset) {
  30. if (this.speed + acceleration > speedset) {
  31.  
  32. acceleration = speedset - this.speed;
  33. }
  34.  
  35. this.speed += acceleration;
  36. System.out.println("Speed now is " + this.speed);
  37. }
  38.  
  39. System.out.println("Final Speed now is " + this.speed);
  40. }
  41. else {
  42.  
  43. while (this.speed != speedset) {
  44. if (this.speed - acceleration < speedset) {
  45.  
  46. acceleration = this.speed - speedset;
  47. }
  48.  
  49. this.speed -= acceleration;
  50. System.out.println("Speed now is " + this.speed);
  51. }
  52.  
  53. System.out.println("Final Speed now is " + this.speed);
  54. }
  55. } else
  56. System.out.println("Start the engine!");
  57.  
  58.  
  59. }
  60. }
  61. public class Main {
  62.  
  63. public static void main(String[] args) {
  64.  
  65. Automobile tesla = new Automobile();
  66. tesla.turnOn();
  67. tesla.setSpeed(100, 14);
  68. tesla.setSpeed(60, 3);
  69. tesla.setSpeed(0,25);
  70. tesla.turnOff();
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement