PowerPoint Presentation - CSE 110 01/24/2005

Document Sample
PowerPoint Presentation - CSE 110 01/24/2005 Powered By Docstoc
					Introduction to Programming
                  with Java, for Beginners




           Intro to Math Class
           Concept of static
           API Tour
           Intro Random Class
                   Java Library
   Java provides a huge library or collection of
    useful programs

   A gold mine of well-tested code that can save
    you countless hours of development time

   This huge library information is provided in API -
    Application Programming Interface



                                                         1
                  The Math Class
   Description provided in Java API
   Collection of common math functions (sin, cos, sqrt, etc.).
   And two constants: PI and E

             > Math.PI
             3.141592653589793
             > Math.E
             2.718281828459045
             > Math.sqrt(25)
             5.0
             > Math.pow(2,10)
             1024
             > Math.cos(0)
             1.0
             > Math.cos(2 * Math.PI)
             1.0
                                                              2
      How the Math Class Works
public class Math{
  public static final double PI =
3.141592653589793;

    public static double sin(double d){ .. }
    public static double sqrt(double d) { .. }

    private Math(){}
    ..
}
> Math.PI
3.141592653589793
> Math.sqrt(25)
5.0
                                                 3
What's different about Math Class
   It’s different from a typical Java class
      It is a “stateless” class
     We only need one Math class (not multiple
      instances)
     No need to instantiate it (hence, no public
      constructor)
     All of its variables and methods are static

     static means “applies to the class as a
      whole” vs. “applies to an individual instance”


                                                       4
            Static Data or Methods
   A class can have data and methods of its own (not part
    of the objects)

   For example, Dog class can keep a count of the number
    of objects it has created
     class Dog{
      static int dogCount; //not an instance variable
      …
      Dog(String dogName, int dogAge){
          name = dogName;
         age = dogAge;
         dogCount++; //short form for dogCount = dogCount +1
       }
    }



                                                               5
                    Static (contd..)
   In Dr Java
    >Dog d1 = new Dog(“Fluffy”, 5);
    >Dog d2 = new Dog(“Fido”, 6);
    >Dog.dogCount
    2

   Dog.dogCount is asking Dog class for the count of
    the dogs created
       Since dogCount belongs to class and not the object
       Hence use the classname and not Object name to
        access variable dogCount

   More on static topic next time

                                                             6
          Using Reading Library (API)
   We will use API documentation for Java Version 5

   Find the documentation for the Math class
       If you scroll down the lower left panel and click on
        the link labeled Math, the large "main" panel on the
        right will display the documentation for the Math
        class




                                                               7
             General Class Interface
   A class' public interface
       Notice the four headings in the main panel.
       Field Summary
          Has information about public variables
       Constructor and Method summaries
          Describe the public constructors and methods
       Method Detail
          Provides detail on method inputs (parameter(s))and output
           (return type) and some extra details
       The collection of all of a class' public fields,
        constructors, and methods constitute its "interface",
        that is, the public face that it shows the world.
       Note that private variables and methods are not a
        part of its interface.

                                                                       8
             Math Class Description
   Notice the phrase java.lang at the top of the
    main panel above the word Math
       This means that the Math class is part of the core
        Java language and hence can be used directly


   Math Class Interface
       Field Summary: Has two constants PI and E
       Constructor Summary: has no public constructor
       Methods Summary: many methods all which are
        static
       Method Details: e.g. sqrt() takes a double and
        returns a double
                                                             9
     Packages and import Statements
   If a class is not part of java language i.e. java.lang, you'll see
    package name
   What is a package?
        Basically it's a directory that has a collection of related classes
        E.g. Random Class description contains: java.util.Random
        Indicating that the Random code is stored in a file called
         java/util/Random.class somewhere on your machine.
        The java/util directory/folder is known as the "util", or utility
         package.
   Since Random is not part of Java Language we need to tell
    Java where to find it by saying
        import java.util.Random;
        Another way is to use the asterisk "wildcard character": import
         java.util.*;

                                                                               10
                    Random Class
   A class to create Random numbers
   Constructor Summary shows the objects of this
    type can be created
       E.g. Random r = new Random();

   Method Summary shows that it can generate
    random values of types:
      integers, doubles etc.

      E.g. r.nextInt(6) – Generate a integer numbers
       between 0 (inclusive) and 6 (exclusive)
       How do I generate a number between 1 and 6 ?
                                                        11

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:12
posted:8/31/2012
language:Unknown
pages:12