Constructors

Document Sample
Constructors Powered By Docstoc
					Programming 2

  Constructor
                Instantiation
• When talking about variables, we use two
  key words, declare and initialize
  – Declaring a variable is to create the variable
    somewhere in the computers memory
  – Initializing the variable means to give it a value
• When we create an object of a class, we do
  not called it declaration anymore, we call it
  instantiating the class object. We will see
  this once we have a class to use for an
  example.
                          Class Definition
   • In the last presentation, we saw our
     first class definition. Here is another:
public class Car                               •Notice that there are three class variables
{                                              speed, mpg and fuel that are private. Most of
             private int speed;                the time, we create class data members to be
             private int mpg;                  private.
             private int fuel;                 •Remember, that means that only objects of
             public void setCar( )             that class can use the variables. Outside of
             {                                 the class, you cannot access them.
                DEFINED SET OF INSTRUCTIONS    •Notice that there are two methods
             }                                 associated with the class Car. Both setCar();
             public void printCar();           and printCar(); are void functions which
             {                                 means they will pass data to one another or
                 DEFINED SET OF INSTRUCTIONS   other classes.
             }
}
• We have a class definition for car, we then create the method definitions.
• They will look like this:
public void setCar()
{
   String s = JOptionPane.showInputDialog( “How fast can your car go?”
                                         + ”\nEnter the speed: ”);
   String m = JOptionPane.showInputDialog( “How many miles per gallon can
                          your car go?\nEnter the miles per gallon: ”);
   String f = JOptionPane.showInputDialog( “How much fuel does your car
                          have?\nEnter the number of gallons: ”);

    speed = Integer.parseInt(s);   //sets the local variables that are entered
    mpg = Integer.parseInt(m);     //equal to the private data members of
    fuel = Integer.parseInt(f);    //the class Car. This way the entire class
}                                  //has access to the data
public void printCar()
{
   String output = “\nThe speed of your car is “ + speed + “.”
         + “\nThe miles per gallon that your car gets is “ + mpg + “.”
         + “\nThe amount of fuel in your tank is “ + fuel +“ gallons.”
         + “\nYou should be able to drive “ + fuel*mpg + “ miles!”;
   JOptionPane.showMessageDialog( null, output, “Print the Car”,
                          JOptionPane.INFORMATION_MESSAGE );
}


• A thing to remember:
    – Since speed, mpg and fuel are private members of the
      class, only the methods of the class can access them.
              Constructors
• Of special note is the fact that the data
  members of Car (speed, fuel and mpg) are
  not initialized. In fact, you should not
  initialize them in the class definition.
• A constructor is a special method of a class.
  The in the constructor is where any
  initialization takes place.
• A constructor MUST have the same name
  as the class. Therefore, our constructor must
  be named Car().
• If you go back to the class definition, we
  did not create a constructor.
• If you do not create a constructor of your
  own, Java creates one for you. We call this
  the default constructor.
• We used the default constructor in the last
  presentation, so this time lets create our
  own.
 • We first go back to the class definition and
   modify it so that it reads one of these ways:
public class Car                             public class Car
{                                            {
             private int speed;                           private int speed;
             private int mpg;                             private int mpg;
             private int fuel;                            private int fuel;
             public Car()                                 public Car()
             {                                            {
                           speed = 0;                         speed = mpg = fuel = 0;
                           mpg = 0;                       }
                           fuel = 0;                      public void setCar( )
             }                                            {
             public void setCar( )                          DEFINED SET OF INSTRUCTIONS
             {                                            }
               DEFINED SET OF INSTRUCTIONS                public void printCar();
             }                                            {
             public void printCar();                        DEFINED SET OF INSTRUCTIONS
             {                                            }
               DEFINED SET OF INSTRUCTIONS   }
             }
}
• Notice that we did not specify the
  constructors return type. In fact a
  constructor cannot return a value.
• You can send data to a constructor, but we
  will see that later.
• We also have to define the constructor’s
  implementation ( that is what we call the
  method definition)

public Car( )
{
    speed = 0;
    mpg = 0;
    fuel = 0;
}
• When a class object is instantiated (created), the
  constructor is automatically called with the line
      Car newcar = new Car( );
• In this case, our constructor initializes all class
  data members to zero.
• The constructor can be overloaded, which means
  there can be multiple constructors that are sent
  different things.
• Arguments can be passed to the constructor

• Lets take a look at main
public static void main(String args[]) {
     Car newCar = new Car( );

     newCar.setCar();
     newCar.printCar();
     System.exit(0);
}
• We can put this all together to see the
  program.
  – See Source Code
  – See Application
                     Assignment
• In main try a line like:     Car newcar = new Car();
                               newcar.fuel = 2;
  See what the computer says.
• Create a program the will use a class called Human.
  Objects of class Human should have data fields to store
  the height, weight and name of the person. There should
  be a constructor that initializes the data members to be
  zero. There are to be four methods, one that asks for
  each piece of data and one that prints it out.

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:10
posted:1/15/2012
language:English
pages:12