Lecture 7 - Interfaces, Generics & ArrayLists Flashcards
What are interfaces? Why are they useful?
- Interfaces are a collection of abstract methods (“bare bones”), but have no implementations, constructors and don’t return anything.
- A class that implements an interface must include the methods from the interface, but the interface doesn’t care what’s actually inside them.
- Interfaces are useful when you want to implement certain functionality, but there are no shared attributes.
- Any objects from a class that implements an interface can be stored in a reference of the type of the interface
What happens if you store an object as an interface reference? Eg. Interface a = new OtherClass();
You can only use the methods from the interface class. Greeter is the interface, person & dog implement the interface
Person p = new Person("Sam", 45); Greeter a = new Person("Mary", 34); Greeter b = new Dog();
Greeter[] aa = new Greeter[3]; aa[0] = new Dog(); aa[1] = new Dog(); aa[2] = new Person("Jill",17);
// call sayHello() from Greeter for each position in the array for(int i=0;i<3;i++) { aa[i].sayHello(); }
Now the only methods they can access are:
- The Greeter interfaces methods
- toString method
- getClass
None of their own methods
How does a class implement an interface?
public class Dog implements Greeter{..
How do you create an interface class?
public class interface Greeter{
What methods do a class that implements an interface need to have?
The method from the interface class (interface doesn’t care what’s in it as long as it returns the correct type eg., boolean result)
- constructor
- toString()
- getter
Give an example of using the Random import to find the next number and put it into each position in the array.
Number[] a = new Number[10]; Random r = new Random(); System.out.println("Unsorted");
for(int i=0;i<10;i++) { a[i] = new Number(r.nextInt(100)); System.out.print(a[i] + " ");
When is System.out.println(); useful?
Are a loop to add it and then below when we do another print statement it will be on the next line.
How can we call a sort method from another class and use it for an array we made in the current class?
ClassName.methondname(array name)
Eg., Sorting.sort(a);
from the lecture example (8)
What does r.nextDouble() do?
Returns the next random double number between 0.0 and 1.0
What happens when a user clicks something on a Swing GUI?
an ActionEvent is created automatically and is processed by a method called actionPerformed
How do you use an ActionListener?
- Implements ActionListener in the class name
- Register the component with the Listener:
component. addActionListener(instanceOfListenerclass) - Override the actionPerformed(Action Event e) method
instanceOf is a clumsy way of doing things because it can take any object, we need to check it is the right object.
What is a better way of doing it?
Generics
Comparable is a good example of generics. What does it look like?
Compares this Object with the specified Object for order. Returns a -1 if the current object should be earlier, 0 if it is in the same place, or 1 if the current should object should be later than the earlier object in the list.
public class Student2 implements Comparable{ private int grade; private String name;
public Student2(String n,int g) { name = n; grade = g;}
public int getGrade() {
return grade;}
public int compareTo(Student2 o) { if(this.getGrade() > o.getGrade()) { return 1;} else if(this.getGrade() < o.getGrade()) { return -1; }else {return 0; }}}
How do you write methods that can take arguments of any type?
Use eg.,
public static void printArray(G[] Array)
What is an ArrayList?
An object we can use to store references to store objects - (not primitive types, but you can get around that using the wrapper class!). That can grow & shrink based on the number of elements. It is generic so we need to tell it what type we will store in it.
How do you insert an element at a specified position?
// insert element at specified pos public void add(int index, E element)
languages.add(1, “C++”);
How do you add an element to the end of an array list?
// add element to the end of specified type 'E' public boolean add(E e)
//Creating a list of numbers List list2=new ArrayList(); list2.add(21); list2.add(11); list2.add(51); list2.add(1);
//Sorting the list Collections.sort(list2);
//Traversing list through the for-each loop for(Integer number:list2) System.out.println(number); }
What method removes all of the elements from an ArrayList?
arrayListName.clear();
eg.,
ArrayList languages = new ArrayList<>();
languages.add(“Java”);
languages.add(“JavaScript”);
languages.add(“Python”);
System.out.println(“Programming Languages: “ + languages);
// remove all elements languages.clear(); System.out.println("ArrayList after clear(): " + languages); } }
Programming Languages: [Java, JavaScript, Python]
ArrayList after clear(): []
What method returns true if the list contains a specific element?
public boolean contains(Object o)
arrayListName.contains());
eg., import java.util.ArrayList;
class Main { public static void main(String[] args) { // create an ArrayList ArrayList numbers = new ArrayList<>();
// insert element to the arraylist numbers. add(2); numbers. add(3); numbers. add(5);
// returns true number.contains(3)
// returns false number.contains(1)
What method returns the element at a specified position in the list?
public E get(int index)
- This is the get() for an Array list.
- Throws an IndexOutOfBounds Exception
Eg, import java.util.LinkedList; import java.util.List; public class JavaListGetExample1 { public static void main(String[] args) { List list= new LinkedList<>(); for (int i=0;i<6;i++){ list.add(i); // returns the element at the specified position in this list int index =list.get(i); System.out.println("Element "+i+" stored at Index : "+index); } } }