When one class contains an instance variable whose type is another class this is called composition Instead of inheritance which is based on an ISA relationship composition is based on a ID: 445492
Download Presentation The PPT/PDF document "Composition" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Composition
When one class contains
an instance variable whose type is another class
, this is called
composition
.
Instead of inheritance, which is based on an
IS-A
relationship, composition is based on a
HAS-A
relationship.
For example, a car
is a
vehicle, and
has an
engine:
public class Car extends Vehicle
{
private Engine x = new Engine();
} Slide2
Search algorithms
We have learned the 4 most common algorithms for
sorting
an array.
There are 2 main algorithms for searching an array – looking for an item in an array, and if it is found, determining the index at which it was found.
Binary Search
Sequential SearchSlide3
Binary Search
Note: you must begin with a sorted array.
The
Binary Search
algorithm uses these steps:
Find the middle index
Compare the element located there to the thing you are searching for (the “target”)
If the target is larger, then ignore the lower half of the array, and find the middle index of the larger half
If smaller, ignore the larger half, find middle index of smaller half.
Repeat until you find (or do not find) the target
Demo:
BinarySearchDemoSlide4
Sequential Search
The
Sequential Search
algorithm is much simpler, but
less efficient
than Binary Search.
Simply look at each element in the array, starting at the first element, until you find the target.Slide5
null
When you create an Object, such as a String, you must initialize it. If you do not, then its value is null.
Then, if you attempt to display it, or call a method on that Object, such as finding the length of a String, you will get an error – either a compiler error, or a runtime error that is called
NullPointerException
.
Sometimes, when you do not yet know the value of an Object, you deliberately set it to null, in order to avoid compiler errors. This is called a
null reference.
You can check for null references using if statements.
d
emo:
NullDemoSlide6
Wrapper Classes
i
nts
, doubles, and chars are known as
primitive types
,
or
built-in types
.
There are no methods associated with these types of variables.
Strings, on the other hand, are
not
primitive: there is a class called String that has methods, such as .length( ), .
charAt
( ), etc.
So, when you create a String variable, you are actually creating an
object
of the String class.
Using primitive types is more efficient, in terms of code and computer memory. But what if you want to use certain methods on an
int
, a double, or a char? Slide7
Wrapper Classes
A wrapper class allows you to “wrap” or “box” a primitive type into an object, so that you can use methods associated with that object.
Example:
Integer age = new Integer(34);
The value of age is 34, but you can do more with this than you could with a normal, primitive int. Slide8
What is the point of a Wrapper Class?
The 3 most common uses of a wrapper class are:
To use in an
ArrayList
(remember,
ArrayLists
hold Objects, not primitive types!) **See
ArrayListDemo
**
When you want to use a null reference
(to deliberately set a variable value to null. When would you do this? One example: When you can’t be 100% sure that a method will return a valid integer.)
When you want to use an Integer, Double, or Char
polymorphically
example:
Object
num
= new Integer(23);