/
Review for Exam 1 Review for Exam 1

Review for Exam 1 - PowerPoint Presentation

pamella-moone
pamella-moone . @pamella-moone
Follow
402 views
Uploaded On 2015-12-01

Review for Exam 1 - PPT Presentation

As you arriveplease get a handout Today is All Review Inheritance General Exam Advice HashCode Equals Comparable Inheritance When one class extends another it gets all of the functions and variables that its ID: 211426

cheezburger class public ninja class cheezburger ninja public abstract int car hashcode intcollection add compareto equals mysauce return comparable

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Review for Exam 1" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Review for Exam 1

As you arrive…please get a handoutSlide2

Today is All Review

Inheritance

General Exam Advice

HashCode

/Equals

ComparableSlide3

Inheritance

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

();Slide4

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

());Slide5

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

regular

Car

= new Car();

//prints beep

doHonk

(

mikesCar

);

//prints honk

doHonk

(

regularCar

);Slide6

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

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

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

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

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 aroundSlide11

Today is All Review

Inheritance

General Exam Advice

HashCode

/Equals

ComparableSlide12

What will be on the exam

3 Questions about just solving ordinary

APish

problems, often with structures like maps, sets, and lists

3 Questions about classes

hashcode

equals and comparable

1 Question about Big O

2 Questions about recursionSlide13

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.Slide14

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 problemSlide15

Today is All Review

Inheritance

General Exam Advice

HashCode

/Equals

ComparableSlide16

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

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

”)Slide18

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

Today is All Review

Inheritance

General Exam Advice

HashCode

/Equals

ComparableSlide20

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

”Slide21

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 parameterSlide22

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

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

);

}Slide24

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

compareToSlide25

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

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

;

}

}