### CS201 - Spring 2014 - Class 9

• exercises

• sorting
- what is the sorting problem?
- given a list of numbers, sort them in some order
- what are some of the sorting algorithms you may have heard of before?
- insertion sort
- selection sort
- merge sort
- bubble sort
- quick sort

• sorting arrays
- as we saw before, java has a class called Arrays that has some static methods to perform things on arrays
- we used Arrays.toString before
- Arrays has a sort method: Arrays.sort
- it takes an array as a parameter and sorts the array *in place*
- in place means it does not generate a new array, but mutates the array passed in
- like our reverse method from the last assignment
- the return type is void!
- for example, look at the sortNumsDemo in the SortingArrays class in the Sorting demo

• sorting things that aren't numbers
- What about sorting strings? How should we sort them?
- ideally, alphabetically
- look at the sortStringsDemo method in the SortingArrays class in the Sorting demo
- generate a bunch of strings with numbers at the beginning
- call the Arrays.sort
- notice that it does the right thing!

• sorting Cards
- look at the sortCards method in the SortingArrays class in the Sorting demo
- What do you think will happen?
- We get an Exception (error)! Why?
Exception in thread "main" java.lang.ClassCastException: Card cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at SortingArrays.sortCards(SortingArrays.java:53)
at SortingArrays.main(SortingArrays.java:58)

- How do we know how to sort cards?!?

• an aside, casting
- take the following code
Card c = new Card(...);
Object o = c;

- is this legal?
- yes! Object is a superclass of all classes
- What if we added this line:
Card c2 = o;

- is this legal?
- no! Even though we know that what is in o, Java doesn't, so it's not going to allow it
- In situations where you know the type of the object you can tell Java this and "cast" the reference into a reference of a new type
- For example, we can write the code above as:

Card c = new Card(...);
Object o = c;
Card c2 = (Card)o;

- To cast a object reference, we put the type we'd like to cast it to in parentheses
- casting tells the Java compiler to change the type of the thing referenced
- What will happen if we do the following:
Shape s = new Shape(...);
Rectangle r = (Rectangle)s;

- Will Java complain?
- not when we compile it. We told Java that we knew what we were doing?
- What will happen when we run the code?
- ClassCastException!
- We told java that we knew what we were doing, but at runtime, Java does know the object type and checks it
- if we try and cast something to a type that we can't, it will complain

• Back to our Exception above, what do you think is going on?
- Look at the Arrays class documentation:
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

- there are lots of different variants of sort
- which one do you think we're using?
- Object[]
- The Arrays class allows us to sort anything! However...
- We have to specify *how* these things are sorted

• Comparable
- What information do we need to know to sort an array of objects?
- have to be able to compare two objects and ask which is larger!

• Interfaces
- Java only allows for you to have one superclass (one parent)
- this is called "single inheritance"
- Why do you think it does this?
- What if you had two parents with the same method? Which do you inherit?
- Are there reasons we might want to have multiple parents?
- Besides inheriting functionality, it also allows for us to broadcast the types of things that our object can do.
- For example, because the Rectangle class extends the Shape class, we know that all rectangles have a getArea method
- This allows Java to check to make sure object have certain functionality
- To solve this problem, Java has what are called "interfaces"
- An interface defines the methods a class MUST define/have
- An interface is NOT a class
- An interface DOES define a new type

• Defining an interface
- to declare a new interface, you define the method headers, but not the bodies of the methods:

public interface A{
public void printYeah();
public int returnANumber();
public String returnTheString(String s);
}

• look at AInterface and BClass in the InterfaceBasics code
-