public class Car
{
// the car's year model
private int yearModel;
// the make of the car.
private String make;
// the car's current speed.
private int speed;
// no-arg constructor
public Car()
{
yearModel = 2004;
make = "Honda";
speed = 0;
}
// constructor
public Car(int yearModel, String make, int speed)
{
super();
this.yearModel = yearModel;
this.make = make;
this.speed = speed;
}
public int getYearModel()
{
return yearModel;
}
public void setYearModel(int yearModel)
{
this.yearModel = yearModel;
}
public String getMake()
{
return make;
}
public void setMake(String make)
{
this.make = make;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void accelerate()
{
speed += 5;
}
public void brake()
{
if (5 < speed)
{
speed -= 5;
}
else
{
speed = 0;
}
}
}
package com.test;
public class CarDemo
{
/**
* @param args
*/
public static void main(String[] args)
{
Car fwdCar = new Car();
Car sportsCar = new Car(2004, "Porsche", 100);
// Display the current status of four wheel drive Car
System.out.println("\nCurrent status of the sports car:");
System.out.println("Year model: " + sportsCar.getYearModel());
System.out.println("Make: " + sportsCar.getMake());
System.out.println("Speed: " + sportsCar.getSpeed());
// Accelrate the four wheel drive car five times.
System.out.println("\nAccelerating four wheel drive drive car...");
fwdCar.accelerate();
fwdCar.accelerate();
fwdCar.accelerate();
fwdCar.accelerate();
fwdCar.accelerate();
// Decelerate the four wheel drive car two times.
System.out.println("\nDecelerating four wheel drive car...");
fwdCar.brake();
fwdCar.brake();
// Decelerate the sports car three times
System.out.println("\nDecelerating sports car...");
sportsCar.brake();
sportsCar.brake();
sportsCar.brake();
// Accelerate the sports car two times
System.out.println("\nAccelerating sports car...");
sportsCar.accelerate();
sportsCar.accelerate();
// Display the speed of the sports car
System.out.println("\nNow the speed of the sports car is "
+ sportsCar.getSpeed());
}
}
运行结果:
Current status of the sports car:
Year model: 2004
Make: Porsche
Speed: 100
Accelerating four wheel drive drive car...
Decelerating four wheel drive car...
Decelerating sports car...
Accelerating sports car...
Now the speed of the sports car is 95
分享到:
相关推荐
"CarDemo.zip"是一个专注于树莓派小车控制的程序,通过Python编程语言实现了对树莓派小车的全方位控制。下面我们将深入探讨这个项目的核心知识点。 1. **树莓派介绍** 树莓派(Raspberry Pi)是一款基于Linux操作...
"cardemo4s店后台管理系统"是一个专为4S汽车销售店设计的后台管理系统,它主要功能是实现对店内业务的高效管理,包括基础的数据增删改查操作以及人员管理系统。这个系统基于Java编程语言开发,因此具备了Java语言的...
CarDemo.java
"carDemo"可能是一个基于Java开发的项目示例,它可能是为了演示如何使用Java进行汽车相关的业务逻辑处理或者模拟汽车系统功能。在这个项目中,"carDemo-main"可能代表了项目的主目录,包含了整个应用的源代码、配置...
IA与野生(Cardemo) 为CEA Paris-Saclay开发的交互式3D游戏。 使用ThreeJS在javascript中开发。 安装说明 确保在目标系统上安装了NPM 将存储库克隆到正在运行的HTTP服务器上 git clone ...
本文将深入探讨一个基于C#语言的汽车管理系统——CarDemo,它以其高效、稳定和易用性在市场上获得了“非常好”和“提高”的高度评价。北大青鸟作为知名的教育机构,其在IT技术培训方面有着深厚的底蕴,为CarDemo的...
遗传算法实验¶ ↑ 简单介绍遗传算法的工作原理。 在线演示 3D(新的、快速的、带有 pthread/...http://guillaumebouchetepitech.github.io/geneticAlgorithm_experiment/carDemo/web/index.html 如果你喜欢它:¶ ↑
计算机图形学,MATLAB。...│ cardemo.m │ color.jpg │ eye.jpg │ └─我做的matlab5个车牌_顶帽_边缘_腐蚀 car1.m car1.png car2.m car2.png car3.m car3.png car4.m car4.png car5.m car5.png
这些模型文件(如 cardemo.umr)可在 MyEclipse 内部共享,方便团队协作。 #### 5. 创建和编辑 UML 图表 MyUML 提供了直观的图表编辑工具,支持多种 UML 图表类型的创建和编辑。开发者可以直接在图表上修改细节,...
class CarDemo { public static void main(String[] args) { Car demoCar = new Car(); // 构造一辆车 demoCar.set_number(3388); // 设置车号为3388 demoCar.show_number(); // 显示车号 } } ``` **3. 文件...
在提供的`carDemo`文件中,很可能包含了实现这些步骤的MATLAB代码示例。代码应有详细注释,指导新手逐步了解每个部分的功能和实现方式。通过运行和修改这段代码,初学者能够亲自动手实践,加深对图像处理和车牌提取...
"CarDemo"则演示了如何构建一个动态平衡的车辆系统;而"RagdollDemo"则呈现了逼真的角色动画和物理交互。 此外,"SoftBodyWorld"演示了如何模拟布料、绳索等软体物体的运动,这对于游戏中的环境交互或电影特效来说...
public class CarDemo { public static void main(String args[]){ Car demoCar1= new Car(); demoCar1.setNumber(168168); demoCar1.showNumber(); } } 六、设计题 1、 编写一个...