Lecture 15 : Classes II ICS102 College of Computer Science & Engineering

advertisement
King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering
Information & Computer Science Department
ICS102
Lecture 15 : Classes II
July 17, 2016
Constructors

A constructor is a special kind of method that is designed to
initialize the instance variables for an object:
public ClassName(ParametersList){…}

A constructor must have the same name as the class

A constructor has no type returned, not even void

Constructors are typically overloaded
Constructor Example
Constructor
How Constructors are called

A constructor is called when an object of the class is created using
new
ClassName objectName = new ClassName(anyArgs);

The name of the constructor and its parenthesized list of
arguments (if any) must follow the new operator

A constructor cannot be invoked like an ordinary method

Example:
Employee e1 = new Employee(“Mohamed”, 20, 5000);
Constructors
Constructor
Calling the constructor
Constructors

In the previous lecture we saw this example:
We did not define any
constructor,
but
we created objects using
new !!
Explanation .. Next slide ..
Include a No-Argument Constructor



If you do not include any constructors in your class, Java
will automatically create a default or no-argument
constructor that takes no arguments, performs no
initializations, but allows the object to be created
If you include even one constructor in your class, Java
will not provide this default constructor
If you include any constructors in your class, be sure to
provide your own no-argument constructor as well
No-argument constructor
No-argument
constructor
Copy Constructor

A copy constructor is a constructor with a single argument of the
same type as the class.
It creates an object which is an exact copy of the argument object

Example:

How to invoke a copy constructor:

- The methods equals and toString


Java expects certain methods, such as equals and toString, to
be in all, or almost all, classes
The purpose of equals, a boolean valued method, is to compare
two objects of the class to see if they satisfy the notion of "being
equal“

Note: You cannot use == to compare objects
public boolean equals(ClassName objectName)

The purpose of the toString method is to return a String
value that represents the data in the object
public String toString()
equals example
equals example

Invoking equals method:
equals invocation
toString example
toString invocation
The end
Important to do at home :
- read sections 4.3 and 4.4 (pages
244-273)
Exercise: Temperature Class (1/2)


Write a Temperature class that has two instance variables: a
temperature value (a floating-point number) and a character
for the scale, either 'C' for Celsius or 'F' for Fahrenheit.
The class should have four constructor methods:




one for each instance variable (assume zero degrees if no value is
specified and Celsius if no scale is specified),
one with two parameters for the two instance variables, and
a no-argument constructor (set to zero degrees Celsius).
Include two accessor methods to return the temperature:
 getTempCelsius: to return the degrees Celsius,
 getTempFahrenheit: to return the degrees Fahrenheit
IMPORTANT: use the following formulas :


degreesC = 5(degreesF - 32)/9
degreesF = (9(degreesC)/5) + 32
Exercise: Temperature Class (2/2)

Include three mutator methods,






setValue to set the value,
setScale to set the scale ('F' or 'C'), and
setValueAndScale to set both;
Include a suitable toString method.
Include a equals method
Then write a test class called TestTemperature that tests all the
methods. Be sure to use each of the constructors.
Download