Docstoc

capter 4 of java

Document Sample
capter 4 of java Powered By Docstoc
					                                         "
      !                              #       # #
                                     #


                               $                               %                  &
                                                                   %                  &
int n = 1000000;             double f = 4.35;
System.out.println(n * n);
                             System.out.println(100 * f);
// prints -727379968           // prints 434.99999999999994




                               '                           (                      #


                                             0

                                                   1


                                             2

                                                       1


                                )* '+,                                     +. !
                                                                       )* - /




                                                                                          1
+                                    3       (<type>) expression

                                             Example
     5
    4/ 4#
                                               double balance = 12.45;
     5, #
    /, /                                       int dollar = (int) balance; //stores 12


                     % 55                              #
          &                                       6                          %        &




                                                               !
                                              0       #=
7                                        8
                                              6
'
    9 3            5
                  /:
                     ; <    5   %&
long rounded = Math.round(balance);
                                                       %                         &#




                                                                   #
;                                             6
    >

                                              < 3
;                      "
    final double PI = 3.14159
                                              +5C          3   BankAccount
              $       6                           7
            6   7
    ; +? @@? A ? 0 9?;$2+ 6B +




                                                                                          2
 public void withdraw(double amount)                 public void withdraw(double amount)
 {                                                   {
   double newBalance = balance – amount - 5;           final int BANK_FEE = 5;
   balance = newBalance;                               double newBalance = balance – amount – BANK_FEE;
                                                       balance = newBalance;
 }
                                                     }

 7             :       8
 7                 8




     B     #
                                                            < 3                   1

                                                                              3                   #
     < 3                   %            &                                 %               &

                                                         public class Math{
                                                           …
                                                           public static final double E = 2.71828
                                                           public static final double PI = 3.14159
                                                           …
                                                         }




                                                                                              "
public static final int BANK_FEE = 5;                   0             %                       &
                                                         final <type> <name> = <value>;
public void withdraw(double amount)
                                                         final int HOURS_IN_DAY = 24;
{
  double newBalance = balance – amount – BANK_FEE;
  balance = newBalance;
}
                                                        0         %                   &
                                                         <accessSpecifier> static final <type>
                                                           <name> = <value>;
                                                         public static final int HOURS_IN_DAY =
                                                           24;




                                                                                                          3
                                                                      5
                                                                     4.                   #0          #
                $
                                                                                     2
                    0                   8                    items = items + 1;
                                                                 +
                                                                         items + 1;
    0                                            #
                    %       3                        &
+5
time = duration * 60;


0                                           89
            8




                    %                                                        &           '
    $                               items   -
    0                           #

    0                                                        items   =   items   +   5;   items += 5;
                                                             items   =   items   *   2;   items *= 2;
        items++;
                                                             items   =   items   –   2*5;   items -= 2*5;
    2           %           &                                items   =   items   /   x;   items /= x;
        items--;




                        $
                                                             (
         .
        ' D :E .                                                                         %&
        4D 'E 8                                                                           %       &
                                                                                            ##
                                                                                           F* D
    <                           %                        #                               )#
                    &
                                                             ) *




                                                                                                            4
               %                                                         $
  7                        #                             A                         %   3   java.lang&

      9
                                                         A
  int     totalTime = 503;                              Math.pow(x,n)
  int     MINUTES_PER_HOUR = 60;
  int     hours = totalTime / MINUTES_PER_HOUR;
                                                          =
  int     minutes = totalTime % MINUTES_PER_HOUR;
                                                        Math.sqrt(x)       5
                                                                          /:




                                                                                       +
                                                         7                      G, /8      G,
                                                                               - ' *- / B - ' D
                                                          /8
                                                         -/

                                                         6                     0             8
  0
(-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a)
                                                        double average = s1 + s2 + s3 / 3;
or
(-b + Math.sqrt(Math.pow(b,2) - 4 * a * c)) / (2 * a)
                                                         7                                   s1, s2, s3
                                                                     8




          ,                                                    -.              /              -0
  0                                            (        x1 =   (-
                                                               (-b + Math.sqrt(b * b – 4 * a * c)) /
                                                          (2   * a);
                                                        x2 =   (-
                                                               (-b - Math.sqrt(b * b – 4 * a * c)) /
                                                          (2   * a);
      7    1                #
      0        (       3               3
      +            (               #
                                                        double root = Math.sqrt(b * b – 4 * a * c);
                                                             (-
                                                        x1 = (-b + root) / (2 * a);
                                                             (-
                                                        x1 = (-b - root) / (2 * a);




                                                                                                          5
            1                 '           $                             '           $
                                                           9                    1                              8
                                                               System
                                                               Math


    BankAccount                                            H                            #
                                                                                    %System.out&
        +                 balance                          %Math.sqrt()&
                                                               7                                Math
            harrysChecking.getBalance()                        System
            myChecking.getBalance()




                                                                        '           $
                                                                             <                         =
                                                                                (                          #
&            $

$                                                          0                #

                                                               Math.sqrt()          @7 I                           #
                                                                                                       8




                      '           $                                             '           .
                                                      3   ClassName.methodName( parameters)
                                                          ClassName.methodName(parameters)


                                          #       1       -.       2
                                                            Math.sqrt(4)

                                      *       J       K            2
                                                                    3                       %
    $             %           &                                                         &
              *           J           K




                                                                                                                       6
               3'               4         5                                            '
                            #                     6
                                                  int number = 15;
     ; 3            #                             String strnum = "" + number;


                            length()
                                                  9                            8
                                                  int result = Integer.parseInt(someString);
     +          %               /&   ""
                                                  double result = Double.parseDouble(someString);




 B         3                                          String subString()


"19"    // Works for parseInt() & parseDouble()   String greeting = "Hello, World!";
"15.23" // Works for parseDouble() only           String sub = greeting.subString(0,5);
"123a" // Error for both                                     "Hello“
                                                    //sub is "Hello“


                                                  $
                                                              %                    &
                                                                                           2B$1
                                                                                           2B$1




               6                     %
                                                      B               System.out
     2          #
                                                      I                   30
                                                      System.in
 <
           %            &                                 +       #
                                                              3                                   #
                                                                                   3          '   L




                                                                                                      7
                       '                                        '
 6                             #
               3                               nextInt(), nextDouble()


     Scanner                       #            7           #
         System.in
                                                                              3 5

                                                    ;   #
Scanner in     = new Scanner(System.in);
                                                            (                       5




                           '                                    -.
                   #                           Scanner stdin = new Scanner(System.in);
nextLine()
                                               System.out.print("Enter name: ");
                                           5                 stdin.nextLine();
                                               String name = stdin.nextLine();
 7                     #
                                               System.out.print("Enter age:   ");
                                                         stdin.nextInt();
                                               int age = stdin.nextInt();

                           %           &
next()




                                                                                         8

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:5
posted:2/13/2010
language:English
pages:8