To do: October 19
Declare a reference variable named numList of type ArrayList of Integers and assign to it an empty ArrayList that will store Integers.
To do: October 19
The method numVowels returns the number of vowels (upper or lower case) in parameter str.
numVowels("car") -> 1
numVowels("BANANARAMA") -> 5
numVowels("") -> 0
public int numVowels(String str) {
Homework
What is a Concrete class?
A concrete class is one that can be directly created or instantiated, and then used.
In other words, you can create an object from a concrete class definition.
Example of abstract class: Monster class in Repl.it
What is an Abstract class?
An abstract class is one where parts of the class are declared abstract, making the whole class abstract.
Those parts of the class that are abstract are incomplete which means that you cannot directly instantiate or create an object using an abstract class.
Interfaces
Modified from O(N)CS Lessons
Abstract classes
Abstract classes are declared abstract and may include:
Interface
Real-world Interfaces
What do all of these vehicles have in common?
These actions require controls that you manipulate
Vehicle interface
It is up to each type of vehicle to define how each of these methods are implemented based on the design of that vehicle.
Interface ⇒ Implementations are unique to class
In interfaces, processes are defined, but implementation is left to each class.
Each class has its own definition of the common methods because each class works in a different way.
This vehicle interface requires that these three methods be implemented because they are essential to any vehicle’s operation.
What is an interface?
An interface is a list of methods that must be implemented.� �An interface may not contain any implemented methods.��Interfaces cannot have constructors!!!�
Type Parameters:�T - the type of objects that this object may be compared to
Compares this object with the specified object for order.
Interface Comparable <T>
int compareTo(T o)
currentObject.compareTo(parameterObject)
Student class
public class Student implements Comparable�{
public int compareTo(Student other)� {� return 0;� }�}
Revisit Student class
public class Student implements Comparable�{� public int compareTo(Student other)� {� return 0;� }�}
//Why doesn’t this work?
public class Student implements Comparable�{� public int compareTo(Object obj)� {� Student other = (Student)obj;� //code here� }�}�// if Type (i.e. class) is not declared, parameter variable must be Object.�Then must cast as Student type within implementation.
Type Declaration (generics)
public class Student implements Comparable <Student>�{
public int compareTo(Student other)� {
//code here� }�}�
Recall that array lists use generics too!
List<String> myList = new ArrayList<String>();
List is a child interface of the Collection interface.
Generics restrict a list to a particular data type. If you do not declare a type, then any type of object can be stored in the list (or array list).
List myList = new ArrayList();�//You can add an Integer, then String, etc.
Define compareTo by age
public int compareTo(Student other)�{� if ( age > other.age )� return 1;� else if (age < other.age)� return -1;� return 0;�}
Define compareTo by age
public int compareTo(Student other)�{� if ( age > other.age )� return 1;� else if (age < other.age)� return -1;� return 0;�}�Is there another way to implement this method?
Define compareTo by age
public int compareTo(Student other)�{� return age - other.age;�}
Define compareTo by name (alphabetical order)
public int compareTo(Student other)�{�
}
Define compareTo by name (alphabetical order)
public int compareTo(Student other)�{� return name.compareTo(other.name);�}
Define compareTo by name (alphabetical order)
public int compareTo(Student other)�{� return name.compareTo(other.name);�}
Wait. What?
Why can we call on compareTo within compareTo?
Define compareTo by grade
public int compareTo(Student other)
{� // implementation
�}
What is different about this implementation?
Define compareTo by grade
public int compareTo(Student other)
{
double diff = grade-other.grade;
if (diff<0)� return -1;
if (diff>0)
return 1;
return 0;
�}
Interfaces - summary
Homework: