review for exam 1 as you arrive…please get a handout

26
Review for Exam 1 As you arrive…please get a handout

Upload: kevin-wood

Post on 17-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review for Exam 1 As you arrive…please get a handout

Review for Exam 1

As you arrive…please get a handout

Page 2: Review for Exam 1 As you arrive…please get a handout

Today is All Review

1. Inheritance2. General Exam Advice3. HashCode/Equals4. Comparable

Page 3: Review for Exam 1 As you arrive…please get a handout

Inheritance1. When one class “extends” another, it gets all of the functions (and variables) that

it’s superclass hasclass 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();

Page 4: Review for Exam 1 As you arrive…please get a handout

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 variabledoHonk(new ToyotaCamry());

Page 5: Review for Exam 1 As you arrive…please get a handout

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 carpublic String honk() { return “beep”; }

}

public void doHonk(Car carVar} {System.out.println(carVar.honk());

}Car mikesCar = new ToyotaCamry();Car regularCar = new Car();

//prints beepdoHonk(mikesCar);//prints honkdoHonk(regularCar);

Page 6: Review for Exam 1 As you arrive…please get a handout

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

Page 7: Review for Exam 1 As you arrive…please get a handout

The point of abstract classes is to have subclasses that override their abstract

methodsabstract 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 subclassesIntCollection collection = new IntArrayList();Intcollection.add(7);

Page 8: Review for Exam 1 As you arrive…please get a handout

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

Page 9: Review for Exam 1 As you arrive…please get a handout

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();

Page 10: Review for Exam 1 As you arrive…please get a handout

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

Page 11: Review for Exam 1 As you arrive…please get a handout

Today is All Review

1. Inheritance2. General Exam Advice3. HashCode/Equals4. Comparable

Page 12: Review for Exam 1 As you arrive…please get a handout

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 O4. 2 Questions about recursion

Page 13: Review for Exam 1 As you arrive…please get a handout

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.

Page 14: Review for Exam 1 As you arrive…please get a handout

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

Page 15: Review for Exam 1 As you arrive…please get a handout

Today is All Review

1. Inheritance2. General Exam Advice3. HashCode/Equals4. Comparable

Page 16: Review for Exam 1 As you arrive…please get a handout

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// functionmyMap.get(new ComplexNum(0,0));

Page 17: Review for Exam 1 As you arrive…please get a handout

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

Page 18: Review for Exam 1 As you arrive…please get a handout

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

Page 19: Review for Exam 1 As you arrive…please get a handout

Today is All Review

1. Inheritance2. General Exam Advice3. HashCode/Equals4. Comparable

Page 20: Review for Exam 1 As you arrive…please get a handout

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”

Page 21: Review for Exam 1 As you arrive…please get a handout

compareTo

• Returns an int• If the int is possitive, bigger• If the int is negative, smaller• If the int is 0, equal sizeCheezBurger 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

Page 22: Review for Exam 1 As you arrive…please get a handout

Remember class function’s always have a current instance

class CheezBurger {//more functionsprivate String mySauce;public String getSauce() {

return mySauce;}

}//elsewhereCheezBurger b1 = new CheezBurger(“MAYO”);CheezBurger b2 = new CheezBurger(“Mustard”);

System.out.println(b1.getSauce());

Page 23: Review for Exam 1 As you arrive…please get a handout

CheezBurger a = getBurgerA();CheezBurger b = getBurgerB();if(a.compareTo(b) > 0) {}

//far away…in class CheezBurgerint compareTo(CheezBurger other) {

//we have two CheezBurgers here//the CheezBurger we’re IN and//The CheezBurger we’re passedreturn mySauce.compareTo(other.mySauce);

}

Page 24: Review for Exam 1 As you arrive…please get a handout

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

}

}//elsewhereArrayList<CheezBurger> burgers = getAllBurgers();Collections.sort(burgers); //calls our compareTo

Page 25: Review for Exam 1 As you arrive…please get a handout

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

Page 26: Review for Exam 1 As you arrive…please get a handout

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;}

}