初学者请求java源代码

2024-12-21 21:53:01
推荐回答(5个)
回答1:

class Car
{
// 车辆属性
private String brand; // 品牌
private double engineDisplacement;// 排气量
private double speed;// 当前速度
private boolean status;// 启动状态
private double maxSpeed;// 最大速度

public double getSpeed () {
return this.speed;
}

public Car(String brand, double engineDisplacement, double maxSpeed) {
this.brand = brand;
this.engineDisplacement = engineDisplacement;
this.maxSpeed = maxSpeed;
// 其他属性也要有初始值,不然执行出错。
this.speed = 0;
this.status = false;
}

/** 启动 */
public void start() {
this.status = true;
printNowStatus ();
}
/** 关闭(熄火) */
public void stop() {
// 只在速度为0时关闭(貌似楼上两位没仔细看题…)
if (this.speed == 0)
{
this.status = false;
}
printNowStatus ();
}

/** 加速 */
public void speedUp() {
// 只在启动时可以加速
if (this.status)
{
this.speed = this.speed + 20;
if (this.speed > this.maxSpeed)
{
this.speed = this.maxSpeed;
}
}
printNowStatus ();
}

/** 减速 */
public void slowDown() {
// 只在启动时可以减速
if (this.status)
{
this.speed = this.speed - 10;
if (this.speed < 0)
{
this.speed = 0;
}
}
printNowStatus ();
}

/** 状态打印,在每次启动,加减速,关闭时调用 */
private void printNowStatus () {
System.out.println("轿车【" + this.brand + "】现在的启动状态是:" + this.status + "速度是:" + this.speed +"。");
}

}

public class TestCar
{
public static void main(String[] args)
{
Car myCar = new Car ("红旗", 2, 120);
//启动
myCar.start();
// 循环加速到120
while (myCar.getSpeed() < 120)
{
myCar.speedUp();
}
//循环减速
while (myCar.getSpeed() > 0)
{
myCar.slowDown();
}
//关闭
myCar.stop();
}
}
/* 直接拿文本写的,我用的电脑没装jdk,楼主自己到Java开发环境下调试,应该没什么问题… */

回答2:

//调试过的,肯定好使

class car {
// 品牌
private String brand;
// 发动机排量
private double engineDisplacement;
// 速度
private double speed=0;
// 状态
private boolean status=false;
// 最高时速
private double maxSpeed;
// 构造函数
car(String brand, double engineDisplacement, double maxSpeed) {
this.brand = brand;
this.engineDisplacement = engineDisplacement;
this.maxSpeed = maxSpeed;
}
// 启动
void start(){
this.status=true;
System.out.println(this.status);
System.out.println(this.speed);
}
// 加速
double speedUp(){
this.speed+=20;
this.speed=this.speed>=this.maxSpeed?this.maxSpeed:this.speed;
this.status=this.speed>0?true:false;
System.out.println(this.status);
System.out.println(this.speed);
return this.speed;
}
// 减速
double slowDown(){
this.speed-=10;
this.speed=this.speed>=0?this.speed:0;
this.status=this.speed==0?false:true;
System.out.println(this.status);
System.out.println(this.speed);
return speed;
}
// 熄火
void stop(){
this.status=false;
this.speed=0;
System.out.println(this.status);
System.out.println(this.speed);
}
}
//TestCar类,输出应该是一堆打印信息
public class TestCar{

public static void main(String args[]){
car iCar=new car("红旗",2.0,120.00);
iCar.start();
//加速7次,实际上最后一次应该是加速不了的
for(int i=0;i<7;i++){
iCar.speedUp();
}
//减速13次,实际上最后一次应该是减不了的
for(int j=0;j<13;j++){
iCar.slowDown();
}
iCar.stop();
}
}

回答3:

Car.java
public class Car {
String brand;//品牌
double engineDisplacement;//发动机排量
double speed;//速度
boolean status;//状态
double maxSpeed;//最高时速
//构造函数
public Car(String brand, double engineDisplacement, double maxSpeed){
this.brand=brand;
this.engineDisplacement=engineDisplacement;
this.maxSpeed = maxSpeed;
}

public void start(){
this.status=true;
System.out.println("汽车状态:启动");
}

public void speedUp(){
if(this.status == true){
double s = this.speed+20;
if(s<=this.maxSpeed){
this.speed = s;
}else{
this.speed=this.maxSpeed;
}
}
showMsg(this);
}

public void slowDown(){
if(this.status == true){
double s = this.speed-10;
if(s>=0){
this.speed = s;
}else{
this.speed=0;
this.status=false;
}
}
showMsg(this);
}
//当前状态信息
public void showMsg(Car car){
if(car.status == false){
System.out.println("汽车的当前状态:未启动");
}else{
System.out.println("汽车的当前状态:已启动,车速为:"+car.speed);
}

}
}
===================================================================
TestCar.java
public class TestCar {
public static void main(String[] args) {
Car car = new Car("红旗",2.0,120.00);
car.start();
car.speedUp();
car.speedUp();
car.speedUp();
car.speedUp();
car.speedUp();
car.speedUp();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();
car.slowDown();

}
}

回答4:

public class Car {

private String brand;
private double engineDisplacement;
private boolean status;
private double speed;
private double maxSpeed;

public Car(String brand, double engineDisplacement, double maxSpeed) {
this.brand=brand;
this.engineDisplacement=engineDisplacement;
this.maxSpeed=maxSpeed;
}

public void start(){
status =true;
}
public void speedUp(){
if(status){
if((maxSpeed-20)>=speed){
speed +=20;
};
System.out.println(speed);
}
}
public void slowDown(){
if(status){
speed=speed-10;
if(speed<=0){
speed=0;
stop();
}
System.out.println(speed);
}
}
public void stop(){
speed=0;
status=false;
System.out.println(" Car Stop.");
}

/**
* @param args
*/
public static void main(String[] args) {

Car hongQiCar = new Car("红旗",2.0d,120.00d);

hongQiCar.start();

int speedUpTimes = 120/20;

for(int i=0;i hongQiCar.speedUp();
}

int slowDownTimes= 120/10;

for(int i=0;i hongQiCar.slowDown();
}
}
}

回答5:

你就懒吧!
这应该是在学校老师出的题吧
我学java oop 时老师就给我出个这提
自己不好好研究跑到 百度搜答案!
可简单自己写吧