march 29, 2000cs102-01lecture 1.2 introduction to java cs 102-01 lecture 1.1 java: a simple,...

29
March 29, 2000 CS102-01 Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Upload: felicia-rance

Post on 11-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Introduction to Java

CS 102-01

Lecture 1.1

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 2: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

What is Java?

• Programming language– Computers are stupid– Step-by-step instructions

• Human languages allow for too much ambiguity“The policeman hit the boy with the stick.”

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 3: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Object-oriented

• A world full of objects– Programming a computer means translating

those objects

• OO languages make it easy to specify objects and their relationships

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 4: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Writing for Networks

• Network “rules of the road”– Protocols: TCP/IP, HTTP & FTP– Understand URLs

• Distributed programming– Get other computers to do your work for you– SETI@home

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 5: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Interpreted

• Computers don’t speak Java as a native language

• Two ways to translate a book– One line at a time for each person who reads it– Do the whole thing at once

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 6: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Run Anywhere

• Different strokes for different folks– Should you break eggs at the little end or the

big end?– “Look & feel”

• Byte-codes are universal

• Virtual machines are virtually the same

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 7: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

His Majesty desired I would take some other opportunity of bringing all the rest of his enemy's ships into his ports. And so unmeasurable is the ambition of princes, that he seemed to think of nothing less than reducing the whole empire of Blefuscu into a province, and governing it by a Viceroy; of destroying the Big-Endian exiles, and compelling that people to break the smaller end of their eggs, by which he would remain the sole monarch of the whole world.

-- Gulliver’s Travels by Jonathan Swift

My Platform is Better than Yours

Page 8: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

How to Do More thanOne Thing at a Time

• One processor means one thing at a time

• Switching among many tasks quickly is tough– Lots to track– Use the computer to track it all

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 9: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Simple?! (Maybe Similar)

• Looks like C/C++– Good for existing programmers– Scheme is simple, but Java isn’t

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

Page 10: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Simple = Harder to Hurt YourselfJava: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

• Removed some of C/C++’s foot-shooting ability– Useful but difficult to use well:

• Memory management

• Multiple inheritance

• Operator overloading

Page 11: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

“Do You Know Where Your Code Has Been?”

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

• Robust means it’s hard to break things

• Don’t talk to strangers– How do you know who’s a stranger over a

network?– Use a secret code!

Page 12: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

The Dynamic Uno

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

• Don’t do today what you can put off until tomorrow:

Either always carry suntan lotion and an umbrella, OR wait for the sun to come out

• “Waiting” is dynamic, or run-time, lookup

Page 13: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

High-Octane Programming

Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture neutral, portable, high-performance, multithreaded, dynamic language.

• Java runs more slowly for two reasons– Dynamic means waiting 'til a program runs to

do some housekeeping– Interpreted means translating each line every

time• Just-in-time compilation

Page 14: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

A First Java Program

// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

Page 15: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Run It, and Ta-Da!

Prints out the message “Welcome to Java Programming!” at a specified location

Page 16: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Got Something to Say?// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

Comments are key

The life you save might be your own...

Page 17: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

It Ain’t International Trade// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

import puts your code on a first-name basis with existing class(es)

Page 18: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Everything’s a Class// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

In Java, writing a program means creating a class

“Call me Welcome” -- Moby Java

Page 19: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Using Someone Else’s Wheel// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

Inheritance in action

A “Welcome” is a kind of “Applet”

Page 20: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Why Bother?

destroy() Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.

getAppletContext() Determines this applet's context, which allows the applet to query and affect the environment in which it runs.

getAppletInfo() Returns information about this applet.

getAudioClip(URL) Returns the AudioClip object specified by the URL argument.

getAudioClip(URL, String) Returns the AudioClip object specified by the URL and name arguments.

getCodeBase() Gets the base URL.

getDocumentBase() Gets the document URL.

getImage(URL) Returns an Image object that can then be painted on the screen.

getImage(URL, String) Returns an Image object that can then be painted on the screen.

getLocale() Gets the Locale for the applet, if it has been set.

getParameter(String) Returns the value of the named parameter in the HTML tag.

getParameterInfo() Returns information about the parameters than are understood by this applet.

init() Called by the browser or applet viewer to inform this applet that it has been loaded into the system.

isActive() Determines if this applet is active.

play(URL) Plays the audio clip at the specified absolute URL.

play(URL, String) Plays the audio clip given the URL and a specifier that is relative to it.

resize(Dimension) Requests that this applet be resized.

resize(int, int) Requests that this applet be resized.

setStub(AppletStub) Sets this applet's stub.

showStatus(String) Requests that the argument string be displayed in the "status window".

start() Called by the browser or applet viewer to inform this applet that it should start its execution.

stop() Called by the browser or applet viewer to inform this applet that it should stop its execution.

“Put away that checkbook! Look at all you get with the amazing new Applet class!”

Page 21: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Applets Inherit Too

Page 22: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Painting Pretty Pictures// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

When the browser wants to show an applet, it calls paint()

Page 23: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Is There an Artist in the House?// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

Browser says, “Paint this!” and passes a Graphics object

Call it g for short

Page 24: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Scribbling Strings// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

Graphics objects “know” how to print strings

Page 25: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Pick a String, Any String// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

If you want to draw a string, you’ve gotta specify:

A string (Given in quotes here because we want a literal string) and...

Page 26: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Where do you want it?// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

}

}

Start in the upper-left hand corner and count 25 (pixels) over, then 25 down

Page 27: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

Graphics Know-It-All// A first program in Java

// import Applet class

import java.applet.Applet;

// import Graphics class

import java.awt.Graphics;

public class Welcome extends Applet {

public void paint( Graphics g ) {

g.drawString( "Welcome to Java Programming!", 25, 25 );

g.drawOval(5, 5, 220, 35);

}

}

Page 28: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

New & Improved

Page 29: March 29, 2000CS102-01Lecture 1.2 Introduction to Java CS 102-01 Lecture 1.1 Java: A simple, object-oriented, network-savvy, interpreted, robust, secure,

March 29, 2000 CS102-01 Lecture 1.2

What is Java?

• Convenient language for building networked applications

• Designed to be easy for current programmers to learn

• Protects you from yourself