编写一个Java应用程序,该程序中有两个类,即Vehicle(用于刻画机动车)和 User(主类)具体要求如下: (1)Vehicle类有一个double类型的变量speed,用于刻画机动车的速度,有一个 int型变量power,用于刻画机动车的功率。在类中定义了speedUp(int s)方法,体现机动车有加速功能:定义了speedDown()方法,体现机动车有减速功能;定义了setPower(int p)方法,用于设置机动车的功率;定义了getPower()法,用于获取机动车的功率。
时间: 2023-05-19 11:01:15 浏览: 84
好的,以下是Java应用程序的代码:
Vehicle类:
public class Vehicle {
private double speed;
private int power;
public void speedUp(int s) {
speed += s;
}
public void speedDown() {
speed--;
}
public void setPower(int p) {
power = p;
}
public int getPower() {
return power;
}
}
User类:
public class User {
public static void main(String[] args) {
Vehicle car = new Vehicle();
car.setPower(100);
System.out.println("The power of the car is: " + car.getPower());
car.speedUp(50);
System.out.println("The speed of the car is: " + car.getSpeed());
car.speedDown();
System.out.println("The speed of the car after slowing down is: " + car.getSpeed());
}
}
希望这个代码可以满足你的需求。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)