Download 01.Overview - Calvin College

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
An Overview of Objects
and the Java API
Joel Adams and Jeremy Frens
Department of Computer Science
Calvin College
(1/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
Where did Java come from?
Object-oriented programming:
Late 1970s: Xerox PARC (GUIs, Ethernet, Laserprinters, ...)
 Smalltalk (Adele Goldberg)



Classes of objects
Objects communicate via messages
Everything allocated dynamically



Byte code compiler
Virtual machine (interpreter)
Garbage collector
Mid-1980s: Bell Labs
 C++ (Bjarne Stroustrup): “C with Classes”, a hybrid language
Early 1990s: Sun Microsystems
 Java (James Gosling): “Just another virtual architecture”
 Syntax like C++, features like Smalltalk
(2/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
What is Java?
When people say “Java” they may mean:
An object-oriented programming language
A developers kit (compiler, debugger, …)
A run-time environment small enough to be built-in to a
web browser, cell-phone, cable-box, …
A platform-independent system that strives for “write once,
run anywhere” portability anywhere on the Internet
MS’s .Net gives much of this, except platform-independence.
Much of what we will cover also applies to VB.Net.
(3/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
Classes and Objects
What is a class?
Think “typhoon-class submarine” or “galaxy-class starship”:
A mechanism for creating new types
A means of declaring a container to store both
 variables to store an object’s attributes (internal state); and
 messages to which an object will respond (behavior)
A factory for constructing related objects
What is an object?
 An object is an instance of a class.
(4/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
A Simple Java Class
Suppose we needed to represent fractions (1/2, 3/4) exactly…
class Fraction {
private int myNumerator;
private int myDenominator;
public int getNumerator() {
return myNumerator;
}
public int getDenominator()
return myDenominator;
}
...
}
(5/10)
To store a fraction’s internal
state, we need variables to
hold its numerator and
denominator
We then define a method for
{
each message to which we
want a Fraction to respond
A fair amount of design time goes into deciding
what methods a class should provide for its users
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
Creating and Using Objects
Once we have an operational Fraction class…
class FractionTester {
public static void main(String [] args) {
Fraction frac1 = new Fraction(); we can use it to create
Fraction frac2 = new Fraction(3,4); Fraction instances
if ( frac1.getDenominator() != 0) {
// do something useful with frac1
}
}
}
(6/10)
and compute using
those instances by
sending messages
The real power of OOP comes from organizing objects
into hierarchies, which we’ll see in a future session.
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
Object-Oriented Design
Rumbaugh’s Object Modeling Technique (OMT):
Write a natural-language description of your system.
Our application should display a window containing a textbox in
which a user enters their name. When they have entered it, the
application should display a personalized greeting for that user.
The nouns (except external ones) are your objects.
 If any object cannot be directly represented using predefined
types, build a class to create a type for it.
The verbs (except user actions) are your operations.
 If any operation cannot be directly performed using predefined
operations, build a method (message) to perform it.
(7/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
OOD: Storyboarding
Many designers find it useful to sketch storyboards that show
the appearances of their GUIs:
HelloGUI
HelloGUI
Please enter your name:
Please enter your name:
Jane
Enter
Jane
Welcome to Java, Jane!
If one storyboard can be reached from another, they are
connected by an arrow labeled with the event that triggers
the transition…
(8/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
The Java API
The Java Application Programmers Interface (API) provides
the definitive word on what classes Java predefines, and
what messages instances of those classes accept:
http://java.sun.com/api/index.html
Using this, we can build a working application using
whatever IDE or tools we have available…
I’ll use a text editor and the command-line here; you will use
Eclipse that Jeremy will demo for you soon…
(9/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College
Summary
Java is an OO language descended from Smalltalk and C++.
Java was designed to allow programs to be downloaded and
executed anywhere on the Internet; portability is paramount.
In object-oriented languages, much of the programming
effort goes into building classes.
Objects are instances of classes.
Objects communicate with one another by sending messages.
(10/10)
2003 Joel C. Adams. All Rights Reserved.
Dept of Computer Science
Calvin College