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

📄 j2pp(4-7).txt

📁 java2 primer plus一书源程序
💻 TXT
📖 第 1 页 / 共 3 页
字号:
4.1
public class WhileExample {
   public static void main( String[] args ) {
      int i=1;
      while( i <= 10 ) {
         System.out.println( "i=" + i );
         i++;
      }
   }
}

4.2
public class DoWhileExample {
   public static void main( String[] args ) {
      int a = 0;
      do {
	 System.out.println( "a=" + a );
         a += 2;
      } while( a <= 10 );
   }
}

4.3
public class ForExample {
   public static void main( String[] args ) {
      for( int i=1; i<= 10; i++ ) {
         System.out.println( "i=" + i );
      }
   }
}

4.4
public class ForMultipleExample {
   public static void main( String[] args ) {
      for( int i=1, j=10; i<=10; i++, j-- ) {
         System.out.println( "i=" + i );
         System.out.println( "j=" + j );
         System.out.println( "i*j=" + i*j );
      }
   }
}

4.5
public class BreakExample {
   public static void main( String[] args ) {
      for( int i=1; i<= 10; i++ ) {
         if( i == 5 ) {
	   break;
	 }
         System.out.println( "i=" + i );
      }
      System.out.println( "Done" );
   }
}

4.6
public class ContinueExample {
   public static void main( String[] args ) {
      for( int i=1; i<= 10; i++ ) {
         if( i == 5 || i == 7 ) {
	   continue;
	 }
         System.out.println( "i=" + i );
      }
      System.out.println( "Done" );
   }
}

4.7
public class TimesTableExample {
   public static void main( String[] args ) {
      for( int i=1; i<=10; i++ ) {
         for( int j=1; j<=10; j++ ) {
            System.out.print( (i*j) + "\t" );
         }
         System.out.println();
      }
   }
}

4.8
public class TimesTableExample2 {
   public static void main( String[] args ) {
      outer:
      for( int i=1; i<=10; i++ ) {
         for( int j=1; j<=10; j++ ) {
            if( ( i*j ) == 25 ) {
               break outer;
            }
            System.out.print( (i*j) + "\t" );
         }
         System.out.println();
      }
      System.out.println( "Done" );
   }
}

5.1
public class Math {
  public static int square( int i ) {
    return i*i;
  }

  public static void main( String[] args ) {
    for( int i=1; i<=10; i++ ) {
       System.out.println( "i=" + i + ", i*i=" + square( i ) );
    }
  }
}

5.2
public class Math2 {
  public static int square( int n ) {
    return n*n;
  }

  public static int cube( int n ) {
    return n*n*n;
  }

  public static int toThePower( int n, int power ) {
    int result=n;
    for( int i=0; i<power; i++ ) {
       result *= n;
    }
    return result;
  }

  public static int factorial( int n ) {
    int result = n;
    for( int i=n-1; i>0; i-- ) {
       result *= i;
    }
    return result;
  }

  public static boolean isPrime( int n ) {
    // Your turn..
    return false;
  }

  public static void showNumber( String operation, int n ) {
    System.out.println( "The result of the " + operation + " is " + n );
  }

  public static void main( String[] args ) {
    int a = 5;
    showNumber( "Square", square( a ) );
    showNumber( "Cube", cube( a ) );
    showNumber( "To The Power", toThePower( a, 4 ) );
    showNumber( "Factorial", factorial( a ) );
    
  }
}

5.3
public class Scope {

  static int x = 5;

  public static int timesX( int n ) {
    int result = n*x;
    return result;
  }

  public static void main( String[] args ) {
    int m = 10;
    System.out.println( "m times x = " + timesX( m ) );
  }

}

5.4
public class FactorialRecursive {
  public static int factorial( int n ) {
    if( n == 1 ) return 1;
    return n * factorial( n-1 );
  }


  public static void main( String[] args ) {
    System.out.println( "5 factorial = " + factorial( 5 ) );
    
  }
}

*
public class RecursiveIterativeTest {
  public long factorialIterative( long n ) {
    long result = n;
    for( long i=n-1; i>0; i-- ) {
       result *= i;
    }
    return result;
  }

  public long factorialRecursive( long n ) {
    if( n == 1 ) return 1;
    return n * factorialRecursive( n-1 );
  }

  public static void main( String[] args ) {
    RecursiveIterativeTest test = new RecursiveIterativeTest();
    long memBefore = Runtime.getRuntime().freeMemory();
    long start = System.currentTimeMillis();
    System.out.println( "Iterative factorial 1000 = " + test.factorialIterative( 20 ) );
    long end = System.currentTimeMillis();
    long memAfter = Runtime.getRuntime().freeMemory();
    System.out.println( "Runtime: " + ( end - start ) + "ms, memory used=" + ( memAfter - memBefore ) );


    memBefore = Runtime.getRuntime().freeMemory();
    start = System.currentTimeMillis();
    System.out.println( "Recursive factorial 1000 = " + test.factorialRecursive( 20 ) );
    end = System.currentTimeMillis();
    memAfter = Runtime.getRuntime().freeMemory();
    System.out.println( "Runtime: " + ( end - start ) + "ms, memory used=" + ( memAfter - memBefore ) );
  }
}

*
public class SquareOverload {
  public static int square( int n ) {
    System.out.println( "Integer Square" );
    return n*n;
  }

  public static long square( long l ) {
    System.out.println( "Long Square" );
    return l*l;
  }

  public static double square( double d ) {
    System.out.println( "Double Square" );
    return d*d;
  }


  public static void main( String[] args ) {
    int n = 5;
    long l = 100;
    double d = 1000.0;
    System.out.println( "n squared=" + square( n ) );
    System.out.println( "l squared=" + square( l ) );
    System.out.println( "d squared=" + square( d ) );
    
  }
}

6.1
public class CarTest {
  static final int COUPE = 1;
  static final int CONVERTIBLE = 2;
  static final int T_TOP = 3;

  static final int V4 = 1;
  static final int V6 = 2;
  static final int V8 = 3;
  static final int V10 = 4;

  static int engineType;
  static int bodyType;
  static int topSpeed;
  static int gas;
  static int oil;
  static boolean isRunning;
  static int currentSpeed;

  public static void turnOn() {
    isRunning = true;
  }

  public static void turnOff() {
    isRunning = false;
  }

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

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

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

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

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

  public static void main( String[] args ) {
    // Define the attributes of the car
    engineType = V10;
    bodyType = CONVERTIBLE; 
    topSpeed = 185;
    isRunning = false;
    currentSpeed = 0;

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

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

}

6.2
public class Car {
  public static final int COUPE = 1;
  public static final int CONVERTIBLE = 2;
  public static final int T_TOP = 3;

  public static final int V4 = 1;
  public static final int V6 = 2;
  public static final int V8 = 3;
  public static final int V10 = 4;

  public int engineType;
  public int bodyType;
  public int topSpeed;
  public int gas;
  public int oil;
  public boolean isRunning;
  public int currentSpeed;
}

6.3
public class CarTest2 {

    public static void turnOn( Car c ) {
      c.isRunning = true;
    }

    public static void turnOff( Car c ) {
      c.isRunning = false;
    }

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

    public static void speedUp( Car c, int amount ) {
      if( c.isRunning == false ) {
        // Do nothing - car is not running!
        return;
      }

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

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

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

  public static void main( String[] args ) {
    // Define the attributes of the car
    Car c1 = new Car();
    c1.engineType = Car.V10;
    c1.bodyType = Car.CONVERTIBLE; 
    c1.topSpeed = 185;
    c1.isRunning = false;
    c1.currentSpeed = 0;

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

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

}

6.4
public class CarObject {
  public static final int COUPE = 1;
  public static final int CONVERTIBLE = 2;
  public static final int T_TOP = 3;

  public static final int V4 = 1;
  public static final int V6 = 2;
  public static final int V8 = 3;
  public static final int V10 = 4;

  private int engineType;
  private int bodyType;
  private int topSpeed;
  private int gas;
  private int oil;
  private boolean running;
  private int currentSpeed = 0;

  public CarObject()
  {
  }

  public CarObject( int engineType,
                    int bodyType,
                    int topSpeed )
  {
      this.engineType = engineType;
      this.bodyType = bodyType;
      this.topSpeed = topSpeed;
  }

  public int getEngineType()
  {
      return this.engineType;
  }

  public void setEngineType( int engineType )
  {
      if( engineType >= V4 && engineType <= V10 )
      {
          this.engineType = engineType;
      }
  }

  public int getBodyType()
  {
      return this.bodyType;
  }

⌨️ 快捷键说明

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