Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/10) An Overview of Objects and the Java API Joel Adams and Jeremy Frens.

Similar presentations


Presentation on theme: " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/10) An Overview of Objects and the Java API Joel Adams and Jeremy Frens."— Presentation transcript:

1  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/10) An Overview of Objects and the Java API Joel Adams and Jeremy Frens Department of Computer Science Calvin College

2  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(2/10) 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  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  Byte code compiler  Virtual machine (interpreter)  Garbage collector

3  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(3/10) When people say “Java” they may mean: What is Java? MS’s.Net gives much of this, except platform-independence. Much of what we will cover also applies to VB.Net.  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

4  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(4/10) 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.

5  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(5/10) Suppose we needed to represent fractions (1/2, 3/4) exactly… A Simple Java Class To store a fraction’s internal state, we need variables to hold its numerator and denominator class Fraction {... } private int myNumerator; private int myDenominator; We then define a method for each message to which we want a Fraction to respond public int getNumerator() { return myNumerator; } public int getDenominator() { return myDenominator; } A fair amount of design time goes into deciding what methods a class should provide for its users

6  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(6/10) class FractionTester { public static void main(String [] args) { } Creating and Using Objects Once we have an operational Fraction class… we can use it to create Fraction instances Fraction frac1 = new Fraction(); Fraction frac2 = new Fraction(3,4); if ( frac1.getDenominator() != 0) { // do something useful with frac1 } 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.

7  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(7/10) 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.

8  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(8/10) Many designers find it useful to sketch storyboards that show the appearances of their GUIs: OOD: Storyboarding If one storyboard can be reached from another, they are connected by an arrow labeled with the event that triggers the transition… HelloGUI Please enter your name: Enter HelloGUI Please enter your name: Welcome to Java, Jane! Jane

9  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(9/10) 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…

10  2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(10/10) 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.


Download ppt " 2003 Joel C. Adams. All Rights Reserved. Calvin CollegeDept of Computer Science(1/10) An Overview of Objects and the Java API Joel Adams and Jeremy Frens."

Similar presentations


Ads by Google