1 of 35

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.

2 of 35

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) {

3 of 35

Homework

  • Replit 04-02 and 04-03
  • goFormative - Abstract classes and Replit 04-01, 04-02, and 04-03

4 of 35

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.

5 of 35

Example of abstract class: Monster class in Repl.it

6 of 35

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.

7 of 35

Interfaces

Modified from O(N)CS Lessons

8 of 35

Abstract classes

Abstract classes are declared abstract and may include:

  • Instance variables (concrete)
  • Constructors (concrete)
  • Methods - modifiers/accessors (concrete)
    • Defined and implemented within the abstract class
  • Abstract methods
    • Header and purpose defined
    • Not implemented

9 of 35

Interface

  • Even more abstract
  • All elements are abstract, none are concrete
  • No instance variables
  • No implementations
  • Only names of methods and their basic purpose

10 of 35

Real-world Interfaces

11 of 35

What do all of these vehicles have in common?

  • Speed up
  • Slow down
  • Turn

12 of 35

These actions require controls that you manipulate

13 of 35

Vehicle interface

  • accelerate() - make vehicles speed up
  • brake() - make vehicles slow down
  • steer(direction) - turn vehicle in direction indicated

It is up to each type of vehicle to define how each of these methods are implemented based on the design of that vehicle.

14 of 35

15 of 35

16 of 35

17 of 35

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.

18 of 35

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!!!�

19 of 35

Type Parameters:�T - the type of objects that this object may be compared to

Compares this object with the specified object for order.

20 of 35

Interface Comparable <T>

int compareTo(T o)

currentObject.compareTo(parameterObject)

  • Returns 0 → objects are equal
  • Returns int<0 (negative integer) → current object precedes� parameter object
  • Returns int>0 (positive integer) → current object comes after� parameter object

21 of 35

Student class

public class Student implements Comparable�{

public int compareTo(Student other)� {� return 0;� }�}

22 of 35

Revisit Student class

public class Student implements Comparable�{� public int compareTo(Student other)� {� return 0;� }�}

//Why doesn’t this work?

23 of 35

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.

24 of 35

Type Declaration (generics)

public class Student implements Comparable <Student>{

public int compareTo(Student other)� {

//code here� }�}�

25 of 35

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.

26 of 35

Define compareTo by age

public int compareTo(Student other)�{� if ( age > other.age )� return 1;� else if (age < other.age)� return -1;� return 0;�}

27 of 35

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?

28 of 35

Define compareTo by age

public int compareTo(Student other)�{� return age - other.age;�}

29 of 35

Define compareTo by name (alphabetical order)

public int compareTo(Student other)�{�

}

30 of 35

Define compareTo by name (alphabetical order)

public int compareTo(Student other)�{� return name.compareTo(other.name);�}

31 of 35

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?

32 of 35

Define compareTo by grade

public int compareTo(Student other)

{� // implementation

�}

What is different about this implementation?

33 of 35

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;

�}

34 of 35

Interfaces - summary

  • The interface is the ultimate abstract class
  • Method headers and purposes defined
  • No implementations
  • No instance variables → variables in interfaces are static and final
  • Concrete classes that implement an interface must implement all of the methods of the interface

35 of 35

Homework:

  • goFormative - Interfaces
  • Repl.it 04-04 vowels and 04-05 Student class - Comparable