Created
September 29, 2014 22:30
-
-
Save OlgaKulikova/0c4a2be46172735c2462 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.Module2.Lesson1.task3; | |
/*Написать класс «автомобиль», который должен уметь заводится, | |
глушить мотор, ехать и держать необходимую скорость. | |
*/ | |
public class Car { | |
private String name; | |
private int speed; | |
public void setName(String s) { | |
name = s; | |
} | |
public String getName() { | |
return name; | |
} | |
public void switchOn() { | |
System.out.println(name + " завелся."); | |
} | |
public void switchOff() { | |
System.out.println(name + " заглушил мотор."); | |
} | |
public void go() { | |
System.out.println(name + " поехал."); | |
} | |
public void keepSpeed(int x) { | |
speed += x; | |
System.out.println(name + " едет со скоростью " + speed + " км в час."); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyCar { | |
public static void main(String[] args) { | |
Car ford = new Car(); | |
ford.setName("Ford"); | |
System.out.println("Мой новый автомобиль - " + ford.getName() + "!"); | |
ford.switchOn(); | |
ford.go(); | |
ford.keepSpeed(60); | |
ford.switchOff(); | |
System.out.println(""); | |
Car toyota = new Car(); | |
toyota.setName("Toyota"); | |
System.out.println("Мой новый автомобиль - " + toyota.getName() + "!"); | |
toyota.switchOn(); | |
toyota.go(); | |
toyota.keepSpeed(120); | |
toyota.switchOff(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment