Review for Exam 1 As you arrive…please get a handout Today is All Review 1. 2. 3. 4. Inheritance General Exam Advice HashCode/Equals Comparable Inheritance 1. When one class “extends” another, it gets all of the functions (and variables) that it’s superclass has class Superclass { public int getNum() { return 45; } } class Subclass extends Superclass { //empty! } //in some function… Subclass var = new Subclass(); //var has a getNum method, even though it’s //got no code! It inherits the method from //Superclass var.getNum(); Because a subclass can do everything a superclass can do, you can use a subclass in any variable that expects a superclass class Car { public String honk() { return “honk”; } } class ToyotaCamry extends Car { } public void doHonk(Car carVar} { System.out.println(carVar.honk()); } Car mikesCar = new ToyotaCamry(); //a method parameter is just another kind of variable doHonk(new ToyotaCamry()); But subclasses can add new methods or change the way existing methods work class Car { public String honk() { return “honk”; } } class ToyotaCamry extends Car { // we say that ToyotaCamry “overrides” the method honk in car public String honk() { return “beep”; } } public void doHonk(Car carVar} { System.out.println(carVar.honk()); } Car mikesCar = new ToyotaCamry(); Car regularCar = new Car(); //prints beep doHonk(mikesCar); //prints honk doHonk(regularCar); An abstract class is a class that is missing one or more methods abstract class IntCollection { public abstract add(int new value); } //abstract classes can never be created //this will not compile! IntCollection collection = new IntCollection(); Intcollection.add(7); The point of abstract classes is to have subclasses that override their abstract methods abstract class IntCollection { public abstract add(int new value); } class IntArrayList extends IntCollection { public add(int newValue) { myList.add(newValue); } } //variables of abstract classes can still store //non-abstract subclasses IntCollection collection = new IntArrayList(); Intcollection.add(7); Interfaces are like abstract classes, except they can’t have any non abstract methods (and you use implements not extends) interface IntCollection { public abstract add(int new value); } class IntArrayList implements IntCollection { public add(int newValue) { myList.add(newValue); } } IntCollection collection = new IntArrayList(); Intcollection.add(7); Write the class definitions to match this code Ninja[] ninjas = new Ninja[2]; ninjas[0] = new FireNinja(); ninjas[1] = new Ninja(); for(Ninja ninja : ninjas) { ninja.attack(); } //code prints // “You are attacked by a fire ninja” // “You are attacked by a ninja” //Question: // Could Ninja have an abstract method? // Could Ninja be an interface? // Could this code compile – FireNinja foo = new Ninja(); class Ninja { public void attack() { System.out.println(“You are attacked by a ninja”) } } class FireNinja extends Ninja { public void attack() { System.out.println(“You are attacked by a fire ninja”) } } // Could Ninja have an abstract method? NO! // Could Ninja be an interface? NO! Abstract class and interfaces can not be instantiated (i.e. you couldn’t say “new Ninja()”) // Could this code compile – FireNinja foo = new Ninja(); NO! – Ninja is the superclass, not the other way around Today is All Review 1. 2. 3. 4. Inheritance General Exam Advice HashCode/Equals Comparable What will be on the exam 1. 3 Questions about just solving ordinary APish problems, often with structures like maps, sets, and lists 2. 3 Questions about classes hashcode equals and comparable 3. 1 Question about Big O 4. 2 Questions about recursion Advice • Don’t forget you get 1 (2-sided) page of notes • Arrive on time, keep track of your time through the exam • When solving coding problems, try to stay as close to genuine Java as you can • Never leave a question blank – if you can solve 50% of a problem, that is worth partial credit • A correct non-recursive solution to a problem that requires recursion is 1/5 points • Don’t forget your base cases for recursion problems! It’s considered a big miss if it’s not there • Stop immediately when time is called. If you don’t it’s 10% off your exam grade. Approximately how I grade coding questions • 5 points, fully functional algorithm maybe with the occasional missed semicolon or misnamed library call • 4 points, mostly functional algorithm with an edge case missed • 3 points, basically the right idea for the algorithm but several missed cases or errors • 2 points, part of the algorithm done correctly, some other part wrong • 1 point, some functional code that shows me you understood part of what needed be done, even if you couldn’t do the whole problem Today is All Review 1. 2. 3. 4. Inheritance General Exam Advice HashCode/Equals Comparable HashCode and Equals • All classes inherit from object • All classes have the methods equals and hashCode • But sometimes you want to change them: myMap = new HashMap<ComplexNum, String>(); myMap.put(new ComplexNum(0,0), “ZERO”); // somewhere far away in a different // function myMap.get(new ComplexNum(0,0)); What we want myNum = new ComplexNum(0,0); myNum.equals(new ComplexNum(0,0)) { //this should be true } How can we make that happen? (Hint rhymes with “Boveriding”) The catch • Equals needs some boilerplate (just look it up if you need it) • BUT you also must be consistent with hashCode • THE RULES: – If myVar1.equals(myVar2), myVar1.hashCode() must equal myVar2.hashCode() – To the greatest extent possible, it’s good if myVar1 does not equal myVar2 myVar1.hashCode() does not equal myVar2.hashCode() Today is All Review 1. 2. 3. 4. Inheritance General Exam Advice HashCode/Equals Comparable Comparable • Oftentimes we want to sort our objects • But how do we compare objects to sort them? • Here’s what we want: CheezBurger a = getBurgerA(); CheezBurger b = getBurgerB(); if(a.isBiggerThan(b)) { //a is bigger than b } • But in Java, we don’t use “isBiggerThan” we use “compareTo” compareTo • Returns an int • If the int is possitive, bigger • If the int is negative, smaller • If the int is 0, equal size CheezBurger a = getBurgerA(); CheezBurger b = getBurgerB(); if(a.compareTo(b) > 0) { //a is bigger than b } Note: there are two objects in this comparison, but only 1 parameter Remember class function’s always have a current instance class CheezBurger { //more functions private String mySauce; public String getSauce() { return mySauce; } } //elsewhere CheezBurger b1 = new CheezBurger(“MAYO”); CheezBurger b2 = new CheezBurger(“Mustard”); System.out.println(b1.getSauce()); CheezBurger a = getBurgerA(); CheezBurger b = getBurgerB(); if(a.compareTo(b) > 0) { } //far away…in class CheezBurger int compareTo(CheezBurger other) { //we have two CheezBurgers here //the CheezBurger we’re IN and //The CheezBurger we’re passed return mySauce.compareTo(other.mySauce); } One final detail • Classes that have the compare to function should always implement the interface Comparable<ClassName>: class CheezBurger implements Comparable<CheezeBurger> { int compareTo(CheezBurger other) { return mySauce.compareTo(other.mySauce); } } //elsewhere ArrayList<CheezBurger> burgers = getAllBurgers(); Collections.sort(burgers); //calls our compareTo Ok…your turn Write a new class UtimateCheezBurger. It has two instance variables, a sauce (like the CheezBurger) and a numberOfBurgers. We want to sort UtimateCheezBurger first by sauce, then by numberOfBurgers (increasing). So Mayo,3 comes before Mayo,6 which comes before Mustard,2 Oh and make sure your class has a constructor so I can initialize the UltimateCheezBurger like this: UltimateCheezBurger b = new UltimateCheezBurger(“Mayo”,4); class UltimateCheezBurger implements Comparable<UltimateCheezBurger> { String mySauce; int myBurgers; public UltimateCheezBurger(String sauce, int burgers) { mySauce = sauce; myBurgers = burgers; } public int compareTo(UltimateCheezBurger other) { int sauceCompare = mySauce.compareTo(other.mySauce); if(sauceCompare != 0) return sauceCompare; return myBurgers – other.myBurgers; } }