⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 porsche.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
public class Porsche extends NewCar
{
    // Attributes
    private int turbos = 2;
    private boolean nos;

    // State attributes
    private boolean turbo1Engaged = false;
    private boolean turbo2Engaged = false;
    private boolean nosEnabled = false;

    public Porsche()
    {
        super( "Porsche",
               450,
               220,
               2,
               "Yellow",
               15,
               5,
               NewCar.TIPTRONIC );
    }

    public void accelerate() throws CarException
    {
        // Check to see if we are running or not
        if( running == false )
        {
	    CarException e = new CarException( "Car is not running, cannot accelerate!" );
            e.setEngineRunning( false );
            throw e;
        }

        // 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() throws CarException
    {
        // Check to see if we are running or not
        if( running == false )
        {
	    CarException e = new CarException( "Car is not running, cannot decelerate!" );
            e.setEngineRunning( false );
            throw e;
        }
        currentSpeed -= 20;
        if( currentSpeed < 0 ) currentSpeed = 0;
    }

    public void tuneUp()
    {
        System.out.println( "Tuning up a porsche..." );
    }

    public void changeOil()
    {
        System.out.println( "Changing a porsche's oil..." );
    }

    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 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 );
        try
        {
            //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();
        }
        catch( CarException ce )
        {
            ce.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -