javaProgLec05-2014
Download
Report
Transcript javaProgLec05-2014
Agenda Lec06
Notepad
LatinTranslator
Debugger
Exceptions
Go over QT exam
OCP JP 7-I & 7-2
MortalKombat
Exceptions:
Checked versus Unchecked
Exceptions
http://www.ibm.com/developerwo
rks/java/library/j-jtp05254/
Probable Source
Within your Probable
control
Source
Checked
User should
Recover
NO
NO
NO
RuntimeExc YES
eption
(unchecked)
Programmer NO
NO
All other
exceptions
(checked)
User/Syste
m
YES
Error
NO
JVM
YES
Example of Error: System-out-of-memory Error
Examples of RuntimeException: ClassCastException, NumberFormatException.
Example of checked Exceptions: EOFExcception,
Situations which cause Exceptions
(or Errors) to be thrown
• An internal Error occurs in the VM
(OutOfMemoryError)
• An unchecked Exception is generated and
thrown (ArrayIndexOutOfBounds)
• A checked Exception is thrown (EOFException)
• You purposefully throw an Exception.
Exception sequence
• Once an exception is generated, the execution
sequence immediately terminates until the
exception is caught (or propagates up the call
stack to the VM). In practice that means that
any code following your statement which
threatens to throw an exception will NOT be
executed if an exception occurs. No return
value!
• An exception is thrown up the call stack
Some Java naming wierdness in the
Exception Handling API
• Despite having the 'able' suffix, Throwable is a
class, not an interface.
• Errors are considered part of the "Exception
Handling" API
• All Exceptions and Errors occur at Runtime, so
RuntimeException is a useless name.
Some things to remember about
Exceptions
• Anything that inherets from RuntimeException
is unchecked.
The debate over checked and
unchecked
See:
http://www.ibm.com/developerworks/java/libra
ry/j-jtp05254/
1/ are you dealing with user or remote data?
2/ should your user recover?
If yes, use a checked exception
Debugger
Breakpoints, Bookmarks, TODO
•
•
•
•
•
•
•
•
Alt-2 (see breakpoints and bookmarks)
Cntl-F11 (add bookmark)
//TODO your comment here
Left-click left-margin to add breakpoint
Right-click red-circle to edit
View breakpoints
Conditional breakpoints
All exceptions !(this instanceof
java.lang.ClassNotFoundException)
Stepping through code
•
•
•
•
F8 step-over (used most frequently)
F7 step-into
Shift-F8 step-out
If you want to continue execution until the
next breakpoint is reached or end of execution
is reached. Press PLAY
Watching and evaluating
• Variables: usually provides enough data
• Notice that changing data is blue when you
step
• You can right-click any primitive or object and
“add to watches”
• Right-click to “evaluate expression”
JUNIT
Need to add junit-4.xx.jar and hamcrest-core1.1.jar and add as library to project
Get the following plugin: generateTestCases
Ctrl-Shift-A “download” Downloads...
Browse repositories
Get GenerateTestCases plugin
Reflection
• If your program is written well and adheres to
the principals of polymorphism, then you
don't really need reflection.
• However, it's nice to know you have it when
testing/debugging though.
• Very useful when first learning an OO
language.
Reflection
• Reflection allows you to inspect the type
(class) of the implicit parameter at runtime.
• We will use reflection to gain a deeper
understanding of polymorphism and the java
event model.
Every class has a class object which you can
access like so: java.util.Date.class, or like so:
Class.forName(strFullyQualifiedClass);
See reflection example
Reflection
Name of Driver
TimeTestOuter
TimeTestInner
TimeTestLocal
TimeTestAnon
implemnts
ActionListener
ActionListener
ActionListener
ActionListener
See inner example
Implicit
param
EventListener type
Defined
yes
EventListenerOuter
In separate java
file
yes
EventListenerInner
In same java file
yes
anonymous
Same method
no
anonymous
inline
Source Code Online
Source Code Online
• Please avail yourselves of the many excellent
sources for source code, both full applications
and snippets.
• http://stackoverflow.com/ (best for snippets)
• GitHub.com
• BitBucket.org
• http://www.planet-source-code.com/ (best
for full apps)
• http://www.freewarejava.com/
• http://www.javagalaxy.com/
Guidelines for using found source code
• If you know the URL where you got it, cite like
so:
– //http://stackoverflow.com/questions/26305/how
-can-i-play-sound-in-java
• Don't copy an entire application; that is
plagiarism.
• Use found source code within reason.
Cross-Cutting Concerns
BlackJack
• Exceptions
• Assertions
• Logging
Shoe
Dealer
Inner and Anonymous Classes
• Though you are captive in the OO paradigm,
you can use inner classes and anonymous
classes to get around this constraint and write
procedural-like code.
• Often times, no one but the enclosing class
cares about an object. In this case, you may
consider using anonymous inner classes.
• Often times, these anon inner classes are both
reference- and type-anonymous.
Model a system
• Object-oriented programming is about
modeling a system.
• Write the problem out as requirements -- this
will essentially be a math world problem (your
favorite kind to solve!)
• Your computer objects map directly to realworld objects (nouns).
• Your methods map directly to real-world
actions (verbs).
Write a very simple application for a contact manager. The the sake of
simplicity, each contact will have a name and a phone number only. The
user should be able to create new contacts and diplay all contacts.
Build a GUI
Write a very simple application for Latin dictionary. The the sake of
simplicity, each entry has a Latin word and an English equivalent, which
could be several words. The user should be able to add a new definition,
delete a definition.
Build a GUI
Describe the system:
The game of BlackJack; a single player plays against the house for money.
His bet is consistently $100.00 and he starts with 1,000.00. There is a shoe
of six 52-card decks which is reshuffled when the shoe is half used. If the
player wins the hand, his gets his bet back plus the amount of the bet. If he
loses, he loses the money, and if he gets blackjack, he get's his bet * 1.5.
The player plays against a dealer who must follow the following strict rules;
aces are worth 11points only, and the dealer must hit on 16 or below. The
player however, is allowed to hit or hold on any hand-value. Furthermore,
aces are worth either 1 or 11, whichever is more advantageous.
An initial two hands are dealt on seperate sides of a table consisting of two
cards apiece. The dealer's hand displays only one card up. The player has
the option to hit, hold, split, double-down, buy insurance, etc. For this
initial version of the game, we'll consider only hit, hold, and deal.
blue are nouns (objects or fields)
red are verbs (methods)
Interfaces
• A class implements an interface rather than
extends it. Any class that implements the
interface must override all the interface
methods with it's own methods.
• Interface names often end with "able" to
imply that they add to the capabilty of the
class.
• An interface is a contract; it defines the
methods