📄 porsche.java
字号:
public class Porsche
{
public static final int TIPTRONIC = 1;
// Attributes
private int horsePower = 450;
private int maximumSpeed = 220;
private int numberOfDoors = 2;
private String paint = "Yellow";
private int turbos = 2;
private int gasCapacity = 15;
private int oilCapacity = 5;
private int transmission = TIPTRONIC;
private boolean nos;
// State attributes
private boolean turbo1Engaged = false;
private boolean turbo2Engaged = false;
private boolean nosEnabled = false;
private boolean running = false;
private int currentSpeed;
private int currentGas;
private int currentOil;
public Porsche()
{
}
public void start()
{
running = true;
}
public void stop()
{
running = false;
}
public boolean isRunning()
{
return running;
}
public void accelerate()
{
// Check to see if we are running or not
if( running == false )
{
return;
}
// Create a variable representing how much we are going to
// accelerate this second
int increment = 15;
// Check the turbos; they add 5mph per second acceleration
if( turbo1Engaged )
{
increment += 5;
}
if( turbo2Engaged )
{
increment += 5;
}
// Check the NOS; it represents 15mph per second
if( nos )
{
increment += 15;
}
// Increment the current speed
currentSpeed += increment;
if( currentSpeed > maximumSpeed ) currentSpeed = maximumSpeed;
}
public void decelerate()
{
currentSpeed -= 20;
if( currentSpeed < 0 ) currentSpeed = 0;
}
public void engageTurbos()
{
turbo1Engaged = true;
turbo2Engaged = true;
}
public void disengageTurbos()
{
turbo1Engaged = false;
turbo2Engaged = false;
}
public void engageNOS()
{
nos = true;
maximumSpeed += 50;
}
public void disengageNOS()
{
nos = false;
maximumSpeed -= 50;
}
public int getCurrentSpeed()
{
return currentSpeed;
}
public String toString()
{
return "A shiny new " + paint + " Porsche!";
}
public static void main( String[] args )
{
Porsche p = new Porsche();
System.out.println( "My new car: " + p );
p.start();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
for( int i=0; i<20; i++ )
{
if( i == 5 )
{
p.engageTurbos();
}
else if( i == 14 )
{
p.engageNOS();
}
p.accelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
p.disengageNOS();
p.disengageTurbos();
while( p.getCurrentSpeed() > 0 )
{
p.decelerate();
System.out.println( "Current speed: " + p.getCurrentSpeed() );
}
p.stop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -