WC - Constructors (Lesson)
Constructors
Introduction
An object’s state refers to its attributes and their values at a given time and is defined by instance variables belonging to the object. The constructors are used to initialize the instance variables. When thinking about objects and how to write the code for their class, think about what an object has. There is a “has-a” relationship between the object and its instance variables. For example, in our Triangle class, a triangle has a base and height. Brainstorm some objects and their “has-a” relationships.
Constructors
Constructor methods always have the same names as their class. Their purpose is to initialize the instance variables. Default constructors do not have any parameters. Constructors also do not have a return type. The following statements create two Robot objects by calling their constructors.
Notice that there is nothing inside the parenthesis on the bill Robot. That Robot is created with the default constructor.
The betty Robot uses the initializing constructor. Notice it has parameters. These parameters would set the instance variables for this Robot. Constructor parameters are local variables to the constructor and provide data to initialize variables.
When no constructor is written, Java provides a no-argument (default) constructor, and the instance variables are set to default values. The default values for primitive types are 0 for int, 0.0 for double, and false for boolean. The default value for an object is null.
Parameters
Parameters are used to send information back and forth. In the Triangle class (from our previous lesson) the initializing constructor passes two parameters, double b and double h.
The parameters are local variables. That means that b and h only exist within the braces of the constructor. If you were to use those variables anywhere else in the class, you would get an error message.
The code shown above instantiates a new Triangle with a base value of 1 and a height value of 4. The numbers 1 and 4 are now assigned to variables base and height, respectively. This code would be in the main method in a tester or runner class.
Documentation with Comments
Previously, we learned about comments in code. Comments are ignored by the compiler and are not executed when the program is run.
There are three types of comments in Java:
/* */ | generates a block of comments |
/ / | generates a comment on one line |
/** */ | Javadoc comments, used to create API documentation |
When reading code, you may notice pre and postconditions.
A precondition is a condition that must be true just prior to the execution of a section of program code for the method to behave as expected. There is no expectation that the method will check to ensure preconditions are satisfied. When you write methods, you should not code the precondition.
The below image shows comments from one of the past AP CS free response questions. Notice the precondition. When writing the code for this method the programmer does not need to make sure that year2 is the highest value or that both year1 and year2 are greater than or equal to zero. That can be assumed.
A postcondition is a condition that must always be true after the execution of a section of program code. Postconditions describe the outcome of the execution in terms of what is being returned or the state of an object. Programmers write the method code to satisfy the postconditions when preconditions are met.
The following is an example of comments for a method that contain both a pre and post condition. The postcondition describes an outcome once the method has been written correctly.
Circle Class
Practice-It! Practice
1.Go to the PracticeIt website Links to an external site..
2.Log into account.
3.Click on Start Practicing!
4.Go to the most recent edition.
5.Click on Chapter 8: Classes.
6.Complete Exercises: 8.14 – 8.22
IMAGES CREATED BY GAVS