1. 程式人生 > >5.18下午

5.18下午

have eth 下午 for loop overload pri state rim either

Overview of the basic template of a Java class:

package declaration

import statements

access specifier class ClassName
{
}

access specifier is either "public", "private", "protected" or it can be unspecified.  If it is
unspecified then it has package visibility.

We extended the Fraction class to have a equals() method and a toString() method.
This was an example of method overriding.
We talked about how every class is a subclass of java.lang.Object.  We saw how
the subclass (which automatically extends Object) will ineritet the toString() and equals()
method from the parent class.  So we saw an example of inheritence, and also overloading
the inherited methods.

Implemented a main method in a TestFraction class, for testing the Fraction test using
assertions.  Throw Exception if assertion fails.  Since we throw new Exception() from
inside the main method, main must specifiy "throws Exception"

public static void main(String[] args) throws Exception 

{
   Fraction A = new Fraction(BigInteger.valueOf(1), BigInteger.valueOf(2);
   Fraction B = new Fraction(BigInteger.valueOf(1), BigInteger.valueOf(4);

   if (A.equals(B)) throw new Exception();  // assert false A.equals(B);
}

Also we discussed a few primitive types : int, long, float, double, boolean
and mentioned the auto-boxing and auto-unboxing between boolean and Boolean.  There are
corresponding boxed types for int (Integer), long (Long), float (Float), double (Double) also.

We covered 

3 types of loops:

for(;;){}   (very convenient syntax for conditional interation)
while(){}  (can be used to implement same logic as a for loop)
do{}while()   (body of loop is always executed the first time)

break (terminates the loop)
continue (skips to next iteration)

5.18下午