slides 4/22 cop 3330. topics final exam review final exam the final exam is friday, april 29 th at...

27
Slides 4/22 COP 3330

Upload: camron-townsend

Post on 18-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Slides 4/22

COP 3330

Topics

• Final Exam Review

Final Exam

• The final exam is Friday, April 29th at 10:00 AM in the usual room

• No notes, books, calculators, etc.

• Don't forget!

• Duration is officially 2 hrs and 50 minutes – not likely to take more than 90 minutes though.

Topics

• Focus is largely on the fundamentals covered during the first two exams.

• The newer material will still be covered, but not in great detail. I'll give some examples of what you can expect further along.

Enums

• An enum defines a finite number objects of the same type- Begins by explicitly enumerating those

objects- After the semicolon, it's just like any other

class (except that the enumerated objects are the only ones that can exist)

• The default toString() method returns the name of the object – you can override this, though.

• The static values() method returns an array of the enumerated values

Generics

• Generic polymorphism allows classes to be defined in terms of other classes- E.g. Vector<String> is a Vector of Strings

• You can make your own generic class by putting <T> after the name of your class (where T can be any valid identifier)

• Then, inside your class, T refers to whatever is passed in as the generic formal parameter- E.g. If you have public MyClass<T> and

someone constructs a MyClass<Integer>, then T refers to Integer

Threads

• To create a Thread:- Pass a Runnable object to the

constructor of Thread - Call the start() method on the Thread

object to start the run() method of the Runnable object in a new thread

UML

UML

• Access modifiers: The visibility of fields and methods are represented by the first character before their names- Private: -- Public: +- Protected: #

• Other modifiers:- If a method or field is underlined that

indicates that it is static- If a method, field, or class name is italicized,

that indicates that it is abstract

UML

• Inter-class relationships- Generalization (extends)

Arrow points at the superclass- Realization (implements)

Arrow points at the interface Lollipop doesn’t allow for details

- Composition (has-a: built out of) Closed diamond is next to the class that holds

things- Aggregation (has-a: container)

Open diamond is next the class that holds things- Association

Class Basics

• Instance Variables (Fields)

• Instance Methods

• Static Variables (Fields)

• Static Methods

• Constructors

Inheritance

• The keyword extends defines an is-a relationship between classes- If Foo extends Bar, then Foo is-a Bar- That means that variables of type Bar can

also hold objects of type Foo • The keyword implements also defines an is-a

relationship, but between classes and interfaces- If Foo implements Comparable<Foo>, then

Foo is-a Comparable<Foo>

• Dynamic binding refers to waiting until runtime to determine what code a method call will go to

Inheritance

• Interfaces provide a set of methods that must be written in anything that implements the interface

• Abstract classes provide partial implementations of a class, but it’s left to anything that inherits from it to fill in the gaps

Polymorphism

• This is the ability to have objects of different types that possess similar structure or behavior, though they may implement it in their own way.

• For example, a Cat object and a Dog object can both respond to a makeNoise() method, even though the results of that method call will vary.

Polymorphism

• These different objects need to 'look the same' to the methods that make use of them.

- If the object is passed as a parameter, the type of the object needs to be fixed. If it's a Cat, then we can't pass a Dog object.

• The is-a relationships imposed by inheritance or implementing an interface is Java's way to make them look the same.

• For example, Cat and Dog could both inherit from Animal, so a method that uses either Cat or Dog objects can just take a reference of type Animal.

• Since both Cat and Dog are also Animals, they 'look the same' in that respect.

• Similarly, if Cat and Dog both implemented the same interface (Noisy, for example) then the method could just take a reference of type Noisy.

Example

public void sing(Animal a){ for(int i = 0; i < 10; i++) a.makeNoise();}

....Cat c = new Cat();sing(c);Dog d = new Dog();sing(d);

Dynamic Binding

• This is really what makes polymorphism useful.• With our Cat and Dog example, remember that if we

use an Animal reference to hold either of these objects, we can only 'see' (and therefore call) the methods that they inherit from Animal.

• However, if Cat and Dog override Animal's makeNoise() method, then when we call it on our Animal reference – the correct overridden version will be called.

• If the reference points to a Cat, it'll call Cat's version of the method. If it's a Dog, it'll call Dog's version. If it's an Animal, it'll call Animal's version.

Dynamic Binding

• In our sing() example, when the compiler processes the sing() method, it doesn't know whether it will be passed a Cat or a Dog object.

• So it doesn't link up the call to makeNoise() with an actual method at compile-time.

• When the program is run, it examines the actual object it was passed, figures out the type, and then binds the call to the correct code.

• This is why we call the binding 'dynamic'. It's also sometimes called late binding.

• The normal approach is usually called static binding (not to be confused with static methods!).

instanceof

• foo instanceof Bar evaluates to- true: only if foo holds an object that

can be treated as a Bar- Compile error: if it is illegal for foo to

hold something that could be treated as a Bar

- false: if foo could hold something that can be treated as a Bar, but doesn’t

super

• super refers to the parent class- In a constructor, the very first thing

that happens is a call to the super constructor

- In any instance method, super is the part of this object that comes from the parent class

Method Modifiers

• public – Anything can access it

• protected – Can only be accessed by things that inherit from the class (or that are in the same package)

• private – Can only be accessed from within the class

• abstract – Must be overridden (only allowed in abstract classes)

• final – Cannot be overridden

Errors

• Compilation Errors

• Runtime Errors

• Logic Errors

Exceptions

• Catching exceptions- Use try/catch/finally

• Throwing exceptions

Exceptions

• Unchecked exceptions- Anything that inherits from

RuntimeException is an unchecked Exception

- You don’t need to declare that your method throws unchecked exceptions

• Checked exceptions- Checked exceptions are exceptions that

don’t inherit from RuntimeException

GUI

• JOptionPane is used to pop up canned dialogs:- JOptionPane.showMessageDialog() – Pop up

a message and an OK button- JOptionPane.showConfirmDialog() – Pop up

a message and Yes, No, and Cancel buttons- JOptionPane.showInputDialog() – Pop up a

message and collect a string from the user

GUI

• Useful Swing Components- JFrame – A window- JTextArea – A multi-line text entry area- JTextField – A single line text entry area- JLabel – A text or image display area- JButton – A push button- Canvas – (From java.awt) A drawing area

To draw on a Canvas, you need to get access to its Graphics object

Examples for newer material

• Given a simple UML class diagram, write skeleton code for the class (method stubs instead of full-fledged methods).

• Write an enum representing coin denominations

• Write a generic method to reverse an array of any type.