Brand: brand, type String; Engine: engineDisplacement, type is double; Speed: speed, type of double; Status: status, type of Boolean; Top speed: maxSpeed, a type of double,
The Car has the following methods:
Method: the Car (String brand, double engineDisplacement, double maxSpeed), the method USES the value of the variable parameter is set members,
Start: start (), the value of this method makes the status becomes true;
Acceleration: speedUp (), when the car is in a state of start, each time the method invocation, speed increase 5, but shall not be higher than the highest speed per hour,
Slow: slowDown (), when the car is in a state of start, each time the method invocation, speed reduction of 5, but shall not be less than zero speed,
Stall: stop (), when the speed is zero, the status of values into a false,
Each method in addition to change member variables, but also print out after method execution status and speed,
Write the main method to instantiate a Car object, its brand is "red flag", displacement of 2.0, a top speed of 160.00, start the Car, speed up to 120, then slowed to zero, the last stall,
CodePudding user response:
Public class Car {
Private String brand;
Private double engineDisplacement;
Private double maxSpeed;
Private double speed=0;
Private Boolean status=false;
Public Car (String brand, double engineDisplacement, double maxSpeed) {
Super ();
This. Brand=brand;
Enclosing engineDisplacement=engineDisplacement;
Enclosing maxSpeed=maxSpeed;
}
Public String getBrand () {
Return brand;
}
Public void setBrand (String brand) {
This. Brand=brand;
}
Public double getEngineDisplacement () {
Return engineDisplacement;
}
Public void setEngineDisplacement (double engineDisplacement) {
Enclosing engineDisplacement=engineDisplacement;
}
Public double getMaxSpeed () {
Return maxSpeed;
}
Public void setMaxSpeed (double maxSpeed) {
Enclosing maxSpeed=maxSpeed;
}
Public double getSpeed () {
The return speed;
}
Public void setSpeed (double speed) {
This. Speed=speed;
}
Public Boolean isStatus () {
Return the status;
}
Public void setStatus (Boolean status) {
Enclosing the status=status;
}
Public void the start () {
The status=true;
Print ();
}
Public void speedUp () {
If (status) {
Speed=speed + 5 & gt; MaxSpeed? MaxSpeed: speed + 5;
}
Print ();
}
Public void slowDown () {
If (status) {
Speed=speed - 5 & lt; 0? Zero: speed - 5;
}
Print ();
}
Public void the stop () {
If (speed==0) {
The status=false;
}
Print ();
}
Private void print () {
System. The out. Println (" status: "+ status);
System. The out. Println (" speed: "+ speed);
}
Public static void main (String [] args) {
The Car the Car=new Car (" red flag ", 2.0, 160.00);
The car. The start ();
While (car. GetSpeed () & lt; 120) {
Car. SpeedUp ();
}
While (car. GetSpeed () & gt; 0 {
The car. The slowDown ();
}
The car. Stop ();
}
}