以下是我必须完成的Java程序的指令和代码。我卡住了,不知道如何继续。我试图弄清楚这一点。我觉得我不知道我在做什么。所有的帮助,方向和解释将非常感激。构造函数和无参数构造函数?
写了一个名为
Car
类具有以下字段:
yearModel
:该yearModel
场是保持汽车的年模型的int。
make
:该make
字段引用一个字符串对象,其中包含汽车的品牌。
speed
:speed
字段是保存汽车当前速度的int值。此外,类应该有下面的构造和其他 方法:
构造函数:一个构造函数应该接受这款车的年份型号, 化妆和速度作为参数。应将这些值分配给 对象的
yearModel
,make
和speed
字段。另一个构造函数 没有参数,并将0指定为汽车的年车型,速度为 ,并将空字符串(“”)指定为make。访问者:相应的访问者方法应将 中的值存储在对象的
yearModel
,make
和speed
字段中。变体:适当的变体方法应将值存储在 对象的
yearModel
,make
和speed
字段中。
accelerate
:加速方法应在每次调用speed
字段 时加5。
brake
:制动方法应该从speed
字段中减去5,每个字段调用 时间。演示程序中的课程,要求用户输入数据 ,然后创建一个
Car
对象。然后它调用accelerate
方法 五次。在每次调用accelerate
方法后,获取汽车的当前speed
并显示它。然后拨打brake
方法五 次。在每次调用brake
方法后,获取当前speed
的 汽车并显示它。运行该程序的输出会出现类似:
Enter the car's year model: 1965 Enter the car's make: Mustang Enter the car's speed: 30 Current status of the car: Year model: 1965 Make: Mustang Speed: 30 Accelerating... Now the speed is 35 Accelerating... Now the speed is 40 Accelerating... Now the speed is 45 Accelerating... Now the speed is 50 Accelerating... Now the speed is 55 Braking... Now the speed is 50 Braking... Now the speed is 45 Braking... Now the speed is 40 Braking... Now the speed is 35 Braking... Now the speed is 30
这是我到目前为止有:
public class Car {
// Declaration of variables.
private int yearModel;
private String make;
private int speed;
// Constructor that accepts arguements.
public static void acceptor(int yearModelIn, String makeIn, int speedIn){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the car's year model: ");
yearModelIn = keyboard.nextInt();
System.out.println("Enter the car's make: ");
makeIn = keyboard.next();
System.out.println("Enter the car's speed: ");
speedIn = keyboard.nextInt();
}
// Constructor that zeroes fields.
public void zeroer()
{
yearModel = 0;
speed = 0;
make = ("");
}
// Accessor Methods
public int getYearModel()
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
// Accelerate method for adding 5 to speed.
public void Accelerate()
{
speed += 5;
}
// Brake method for reducing speed.
public void Brake()
{
speed-=5;
}
'Accessor'对于'getter'方法来说只是一个奇特的词,而对'setter'来说''Mutator''。 – 2014-10-07 04:24:53
“_I卡住了”什么?哪部分不工作? – csmckelvey 2014-10-07 04:24:57
你的构造函数应该看起来像'public Car(String whatever){}'not“acceptor”或者“zeroer” – tom 2014-10-07 04:26:43