Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- //Написать класс "Автомобиль", который должен уметь заводиться, глушить мотор, ехать и держать необходимую скорость.
- class Automobile {
- private boolean starter;
- private double speed;
- void turnOn() {
- starter = true;
- System.out.println("Engine is On");
- }
- void turnOff() {
- starter = false;
- System.out.println("Engine is Off");
- }
- void setSpeed(double speedset, double acceleration) {
- if (starter) {
- if (this.speed < speedset) {
- while (this.speed != speedset) {
- if (this.speed + acceleration > speedset) {
- acceleration = speedset - this.speed;
- }
- this.speed += acceleration;
- System.out.println("Speed now is " + this.speed);
- }
- System.out.println("Final Speed now is " + this.speed);
- }
- else {
- while (this.speed != speedset) {
- if (this.speed - acceleration < speedset) {
- acceleration = this.speed - speedset;
- }
- this.speed -= acceleration;
- System.out.println("Speed now is " + this.speed);
- }
- System.out.println("Final Speed now is " + this.speed);
- }
- } else
- System.out.println("Start the engine!");
- }
- }
- public class Main {
- public static void main(String[] args) {
- Automobile tesla = new Automobile();
- tesla.turnOn();
- tesla.setSpeed(100, 14);
- tesla.setSpeed(60, 3);
- tesla.setSpeed(0,25);
- tesla.turnOff();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement