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

📄 j2pp(4-7).txt

📁 java2 primer plus一书源程序
💻 TXT
📖 第 1 页 / 共 3 页
字号:

  public void setBodyType( int bodyType )
  {
      if( bodyType >= COUPE && bodyType <= T_TOP )
      {
          this.bodyType = bodyType;
      }
  }

  public int getTopSpeed()
  {
      return this.topSpeed;
  }

  public void setTopSpeed( int topSpeed )
  {
      if( topSpeed > 0 )
      {
          this.topSpeed = topSpeed;
      }
  }

  public boolean isRunning()
  {
      return this.running;
  }

  public int getCurrentSpeed()
  {
      return this.currentSpeed;
  }

  public void turnOn() {
    running = true;
  }

  public void turnOff() {
    running = false;
  }

  public void accelerate() {
    switch( engineType ) {
    case V4: 
      speedUp( 2 );
      break;
    case V6: 
      speedUp( 3 );
      break;
    case V8: 
      speedUp( 4 );
      break;
    case V10: 
      speedUp( 5 );
      break;
    }
  }

  private void speedUp( int amount ) {
    if( running == false ) {
      // Do nothing - car is not running!
      return;
    }

    if( ( currentSpeed + amount ) >= topSpeed ) {
      currentSpeed = topSpeed;
    }
    else {
      currentSpeed += amount;
    }
  }

  public void decelerate() {
    if( running == false ) { 
      // Do nothing - car is not running!
      return;
    }

    if( ( currentSpeed - 5 ) <= 0 ) {
      currentSpeed = 0;
    }
    else {
      currentSpeed -= 5;
    }
  }
}

6.5
public class CarTest3
{
    public static void main( String[] args )
    {
        // Define the attributes of the car
        CarObject car = new CarObject( CarObject.V10,
                                       CarObject.CONVERTIBLE,
                                       185 );

        // Do some things with the car
        car.turnOn();
        for( int i=0; i<10; i++ )
        {
            car.accelerate();
            System.out.println( "Current Speed: " + car.getCurrentSpeed() );
        }    

        for( int i=0; i<5; i++ )
        {
            car.decelerate();
            System.out.println( "Current Speed: " + car.getCurrentSpeed() );
        }
        car.turnOff();
    }

}

6.6
public class Fraction
{
    private int numerator;
    private int denominator;

    public Fraction( int numerator,
                     int denominator )
    {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    public int getNumerator()
    {
        return this.numerator;
    }

    public void setNumerator( int numerator )
    {
        this.numerator = numerator;
    }

    public int getDenominator()
    {
        return this.denominator;
    }

    public void setDenominator( int denominator )
    {
        if( denominator != 0 )
        {
            this.denominator = denominator;
        }
    }

    public float getFraction()
    {
        return ( float )numerator / ( float )denominator; 
    }

    public static void main( String[] args )
    {
        Fraction f = new Fraction( 3, 4 );
        System.out.println( "3/4 = " + f.getFraction() );
    }
}

6.7
public class Outer
{
    private int a = 5;
    public class Inner
    {
        private int i=1;
        public void myMethod()
        {
            System.out.println( "a=" + a + ", i=" + i ); 
        }
    }

    public static void main( String[] args )
    {
        Outer.Inner innerClass = new Outer().new Inner();
        innerClass.myMethod();
    }
}

6.8
public class OuterTest
{
    public static int outerInt = 5;

    public static class StaticInner
    {
        public static int doubleVal( int n )
        {
            System.out.println( "outerInt=" + outerInt );
            return 2*n;
        }
    }

    public void testInner()
    {
        int a = 5;
        System.out.println( "a=" + a + ", doubleVal=" + StaticInner.doubleVal( a ) );
    }

    public static void main( String[] args )
    {
        int n = 7;
        System.out.println( "n=" + n + ", doubleVal=" + OuterTest.StaticInner.doubleVal( 7 ) );

        OuterTest out = new OuterTest();
        out.testInner();
    }
}

*
public class Number {
   private int number;

   public Number( int number ) {
      this.number = number;
   }

   public int getNumber() {
      return this.number;
   }

   public void setNumber( int number ) {
      this.number = number;
   }

   public static void main( String[] args ) {
      Number one = new Number( 1 );
      Number two = new Number( 2 );
      System.out.println( "Beginning: " );
      System.out.println( "One = " + one.getNumber() );
      System.out.println( "Two = " + two.getNumber() );

      // Assign two to one
      two = one;
      System.out.println( "\nAfter assigning two to one: " );
      System.out.println( "One = " + one.getNumber() );
      System.out.println( "Two = " + two.getNumber() );

      // Change the value of two
      two.setNumber( 3 );
      System.out.println( "\nAfter modifying two: " );
      System.out.println( "One = " + one.getNumber() );
      System.out.println( "Two = " + two.getNumber() );
   }
}

7.1
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();
    }
}

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

⌨️ 快捷键说明

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