求救!!按要求编写Java应用程序

vivi薇薇 2019-03-14 11:11:12
按要求编写Java应用程序。 (1)创建一个叫做机动车的类Car: 属性:车牌号(String),车速(int),载重量(double) 功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。 编写两个构造方法:一个没有形参,在方法中将车牌号设置“XX1234”,速 度设置为100,载重量设置为100;另一个能为对象的所有属性赋值; (2)创建测试类: 在测试类中创建两个机动车对象。 创建第一个时调用无参数的构造方法,调用成员方法使其车牌为“辽 A9752”,并让其加速。 创建第二个时调用有参数的构造方法,使其车牌为“辽B5086”,车速为150, 载重为200,并让其减速。 输出两辆车的所有信息
...全文
122 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
bcsflilong 2019-03-14
  • 打赏
  • 举报
回复 1


public class Car {
@Override
public String toString() {
return "Car [车牌号=" + licensePlateNumber + ", 实时车速=" + speed + ", 测体重量=" + weight + "]";
}
private String licensePlateNumber;
private int speed;
private double weight;

public Car(String licensePlateNumber, int speed, double weight) {
super();
this.licensePlateNumber = licensePlateNumber;
this.speed = speed;
this.weight = weight;
}

public Car() {
this.licensePlateNumber="XX1234";
this.speed=100;
this.weight=100;
}

public String getLicensePlateNumber() {
return licensePlateNumber;
}

public void setLicensePlateNumber(String licensePlateNumber) {
this.licensePlateNumber = licensePlateNumber;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public double getWeight() {
return weight;
}

public void setWeight(double weight) {
this.weight = weight;
}

public void speedUp() {
this.speed++;
}

public void speedDown() {
this.speed--;
}
public static void main(String[] args) {
Car car=new Car();
car.setLicensePlateNumber("辽A9752");
//加速
car.speedUp();

Car car2=new Car("辽B5086", 150, 200);

car2.speedDown();

System.out.println(car);
System.out.println(car2);
}
}

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧