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

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
1 Working with References. 2 References Every object variable is a reference to an object Also true when an object is passed as an argument When the object.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS503: Fourth Lecture, Fall 2008 Advanced OOP and Lab. Michael Barnathan.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Abstract Classes and Interfaces
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Abstract classes and Interfaces. Abstract classes.
9/4/2015Abstract classes & Interface1 Object Oriented Design and Programming II Chapter 10 Abstract classes and Interfaces.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Polymorphism & Interfaces
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Inheritance Only not the good kind of inheritance where you discover your Great Aunt left you a yacht. The bad kind that involves obscure Java syntax.
CPS 100, Fall GridGame APT How would you solve this problem using recursion?
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Introduction to Generics
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Written by: Dr. JJ Shepherd
Wel come To Seminar On C#.
Today’s lecture Review of chapter 8 Go over examples.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Lecture 5:Interfaces and Abstract Classes
Object-Oriented Concepts
Sections Inheritance and Abstract Classes
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Interfaces and Inheritance
Writing Methods AP Computer Science A.
2009 Test Key.
Agenda Inheritance Polymorphism Type compatibility Homework.
CMPE212 – Reminders Assignment 2 due next Friday.
Review for Midterm 3.
Presentation transcript:

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

Today is All Review 1.Inheritance 2.General Exam Advice 3.HashCode/Equals 4.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.Inheritance 2.General Exam Advice 3.HashCode/Equals 4.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.Inheritance 2.General Exam Advice 3.HashCode/Equals 4.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 (); 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.Inheritance 2.General Exam Advice 3.HashCode/Equals 4.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 : class CheezBurger implements Comparable { int compareTo(CheezBurger other) { return mySauce.compareTo(other.mySauce); } //elsewhere ArrayList 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 { 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; }