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

📄 pinto.java

📁 java2 primer plus一书源程序
💻 JAVA
字号:
public class Pinto
{
    public static final int MANUAL = 2;

    // Attributes
    private int horsePower = 50;
    private int maximumSpeed = 60;
    private int numberOfDoors = 5;
    private String paint = "Two-tone";
    private int gasCapacity = 15;
    private int oilCapacity = 5;
    private int transmission = MANUAL;

    // State attributes
    private boolean running = false;
    private int currentSpeed;
    private int currentGas;
    private int currentOil;
    public boolean hatchBackDoorOpen = false;

    public Pinto()
    {
    }

    public void start()
    {
        // It isn't going to start ;)
        running = false;
    }

    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;
        }

        currentSpeed += 4;
        if( currentSpeed > maximumSpeed ) 
        {
            currentSpeed = maximumSpeed;

            // The high speed knocked the door open!
            openHatchBack();
        }
    }

    public void decelerate()
    {
        currentSpeed -= 5;
        if( currentSpeed < 0 ) currentSpeed = 0;
    }

    public int getCurrentSpeed()
    {
        return currentSpeed;
    }

    public void rollStart()
    {
        running = true;
    }

    public void pushUpHill()
    {
        System.out.println( "Ouch, this thing is heavy!" );
    }
    
    public void openHatchBack()
    {
        hatchBackDoorOpen = true;
    }
    
    public void closeHatchBack()
    {
        hatchBackDoorOpen = false;
    }

    public boolean isHatchBackDoorOpen()
    {
        return hatchBackDoorOpen;
    }

    public String toString()
    {
        return "A rusty old " + paint + " Pinto";
    }

    public static void main( String[] args )
    {
        Pinto p = new Pinto();
        System.out.println( "My car: " + p );
        p.start();
        if( p.isRunning() == false )
        {
            System.out.println( "Starter failed, let's roll start it!" );
            p.rollStart();
        }
        System.out.println( "Current speed: " + p.getCurrentSpeed() + 
                            ", Hatchback open = " + p.isHatchBackDoorOpen() );
        for( int i=0; i<20; i++ )
        {
            p.accelerate();
            System.out.println( "Current speed: " + p.getCurrentSpeed() + 
                                ", Hatchback open = " + p.isHatchBackDoorOpen() );
        }
        while( p.getCurrentSpeed() > 0 ) 
        {
            p.decelerate();
            System.out.println( "Current speed: " + p.getCurrentSpeed() + 
                                ", Hatchback open = " + p.isHatchBackDoorOpen() );
        }
        p.stop();
        if( p.isHatchBackDoorOpen() )
        {
            System.out.println( "Have to close the hatchback!" );
            p.closeHatchBack();
        }
    }
}

⌨️ 快捷键说明

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