Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java...

43
Introduction to Java

Transcript of Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java...

Page 1: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Introduction to Java

Page 2: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Announcements

● Programming Assignment #1 Out:● Karel the Robot: Due Friday, January 20 at 3:15 PM.● Email: Due Sunday, January 22 at 11:59PM.

● Section Assignments Posted● Check online at http://cs198.stanford.edu.● Sections and LaIR hours start this week.● Feel free to switch sections to an open time if it works better

for you; your fellow CS106Aers are counting on you!

● Submitting Assignments:● Submitter now open.● Verify submissions at

http://paperless.stanford.edu

Page 3: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

A Farewell to Karel

Page 4: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Welcome to Java

Page 5: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

But First...

A Brief History of Computing

Page 6: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Image credit: http://en.wikipedia.org/wiki/File:Boulier1.JPG

Page 7: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Image: http://upload.wikimedia.org/wikipedia/commons/8/8b/Babbage_Difference_Engine.jpg

Page 8: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

http://upload.wikimedia.org/wikipedia/commons/thumb/a/a4/Analytical_Engine_(2290032530).jpg/200px-Analytical_Engine_(2290032530).jpg

Page 9: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Augusta Ada Byron: The First Programmer

Image Credit: http://upload.wikimedia.org/wikipedia/commons/0/0f/Ada_lovelace.jpg

Page 10: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Programming in the 1800s

MechanicalDevice

Page 11: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Image credit: http://upload.wikimedia.org/wikipedia/commons/4/4e/Eniac.jpg

Page 12: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Programming in the 1940s

ElectricalDevice

Page 13: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Image: http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Grace_Hopper.jpg/300px-Grace_Hopper.jpg

http://www.nytimes.com/2007/03/20/business/20backus.html

High-Level Languages

Page 14: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

ComputerComputer

Programming in the 1950s

DO 5 I = 1, 25 Compiler

Source Deck Program Deck

ComputerCompiler

Page 15: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Programming in the 1950s

DO 5 I = 1, 25 Compiler

Source Deck Program Deck

ComputerCompiler Computer

Page 16: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Programming Now(ish)

Compiler

Machine Code

move();turnLeft();

1101110010111011

Source Code

Page 17: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Hey! I wrote a programthat can draw stick figures!

That's great! I wrote aprogram that makes

speech bubbles!

Page 18: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Computer

Programming Now

Compiler

Source Code

Object File

move();turnLeft();

1101110111

1101110111

Machine Code

Linker

Object File

1101110111

Page 19: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Computer

Programming Now

Compiler

Source Code

Object File

move();turnLeft();

1101110111

Linker 1101110111

Machine Code

Linker

Object File

1101110111

Page 20: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Programming Now

Compiler

Source Code

Object File

Computer

move();turnLeft();

0010111000

Linker 1001010110

Machine Code

Linker

Object File

0110011101

Page 21: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Image credit: http://upload.wikimedia.org/wikipedia/commons/d/d2/Internet_map_1024.jpg

Page 22: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Computer

The Java Model

Compiler

Source Code

.class File

Computer

move();turnLeft();

1101110111

Linker 1101110111

JAR File

JavaVirtual

Machine

Linker

.class File

1101110111

Page 23: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Object-Oriented Programming

Page 24: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

An object is an entitythat has state and behavior.

Page 25: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Has a fur color.Has an energy level.

Has a level of cuteness.Can be your friend.

Can sit.Can stay.Can bark.

Page 26: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

A class is a set of features and behaviorcommon to a group of objects.

Page 27: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Class Dog

Has a fur color.Has an energy level.

Has a level of cuteness.Can sit.

Can stay.Can be your friend.

Instances of Dog

Page 28: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

An object is an entitythat has state and behavior.

A class is a set of features and behavior common to a group of objects.

An instance of a class is anobject that belongs to that class.

Page 29: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Class Dog

Has a fur color.Has an energy level.

Has a level of cuteness.Can sit.

Can stay.Can be your friend.

Class Cat

Has a fur color.Has an energy level.

Has a level of cuteness.Can purr.

Can haz cheezburger?

Page 30: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Dog Cat

Has a fur color.Has an energy level.

Has a level of cuteness.Can sit.

Can stay.Can be your friend.

Has a fur color.Has an energy level.

Has a level of cuteness.Can purr.

Can haz cheezburger?

Page 31: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Dog Cat

CuteAnimal

Has a fur color.Has an energy level.

Has a level of cuteness.Can sit.

Can stay.Can be your friend.

Has a fur color.Has an energy level.

Has a level of cuteness.Can purr.

Can haz cheezburger?

Page 32: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Dog Cat

CuteAnimal

Has a fur color.Has an energy level.

Has a level of cuteness.Can sit.

Can stay.Can be your friend.

Has a fur color.Has an energy level.

Has a level of cuteness.Can purr.

Can haz cheezburger?

Has a fur color.Has an energy level.

Has a level of cuteness.

Page 33: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

The classes Dog and Cat inherit commonfeatures and behaviors from CuteAnimal

The class CuteAnimal is a superclassof the Dog and Cat classes.

The class Dog and Cat classes aresubclasses of the CuteAnimal class.

Dog Cat

CuteAnimal

Can sit.Can stay.

Can be your friend.

Can purr.Can haz cheezburger?

Has a fur color.Has an energy level.

Has a level of cuteness.

Page 34: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Dog Cat

CuteAnimal

Can sit.Can stay.

Can be your friend.

Can purr.Can haz cheezburger?

Has a fur color.Has an energy level. Animal

GrizzlyBear

Has an amount of grizzle.Can keep cubs safe.

Has a level of cuteness.

A Class Hierarchy

Page 35: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Classes so Far

SuperKarel

Karel

moveturnLeft

pickBeeperputBeeper

turnAroundturnRight

Superclass

Subclass

Page 36: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

/* File: RoombaKarel.java * * A Karel program in which Karel picks up all the beepers in a * square world. */

import stanford.karel.*;

public class RoombaKarel extends SuperKarel {public void run() {

while (leftIsClear()) {cleanOneRow();moveToNextRow();

}cleanOneRow();

}

/* Precondition: Karel is facing East at the start of a row. * Postcondition: Karel is facing East at the start of a row, * but the row has all beepers cleared from it */private void cleanOneRow() {

sweepRow();moveBackToStart();

}

/* ... */}

Page 37: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

/* File: RoombaKarel.java * * A Karel program in which Karel picks up all the beepers in a * square world. */

import stanford.karel.*;

public class RoombaKarel extends SuperKarel {public void run() {

while (leftIsClear()) {cleanOneRow();moveToNextRow();

}cleanOneRow();

}

/* Precondition: Karel is facing East at the start of a row. * Postcondition: Karel is facing East at the start of a row, * but the row has all beepers cleared from it */private void cleanOneRow() {

sweepRow();moveBackToStart();

}

/* ... */}

Page 38: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

How Does Karel Fit In?

SuperKarel

Karel

StoneMasonKarel

CheckerboardKarel

MidpointKarel

CollectNewspaper

Karel

Page 39: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Program

DialogProgram GraphicsProgramConsoleProgram

JApplet

Applet

GraphicsProgramConsoleProgram

Program

DialogProgram

JApplet

Applet

acm.program Hierarchy

Graphic courtesy of Eric Roberts

Page 40: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

Let's See Some Java!

Page 41: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

The Add2Integers Program

Add2Integers

class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); }} n1 n2 total

This program adds two numbers.

Enter n2:The total is 42.

17 25 42

25

class Add2Integers extends ConsoleProgram { public void run() { println("This program adds two numbers."); int n1 = readInt("Enter n1: "); int n2 = readInt("Enter n2: "); int total = n1 + n2; println("The total is " + total + "."); }} n1 n2 total

17 25 42

Enter n1: 17

Graphic courtesy of Eric Roberts

Page 42: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

The GObject Hierarchy

The classes that represent graphical objects form a hierarchy, part of which looks like this:

GObject

GRect GOval GLineGLabel

Graphic courtesy of Eric Roberts

Page 43: Introduction to Java - Stanford University · Introduction to Java. Announcements ... The Java Model Compiler Source Code.class File Computer move(); turnLeft(); 11011 10111 Linker

public class HelloProgram extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello, world", 100, 75); label.setFont("SansSerif-36"); label.setColor(Color.RED); add(label); }}

HelloProgram

Sending Messages to a GLabel

hello, world

label

hello, worldhello, worldhello, world

public class HelloProgram extends GraphicsProgram { public void run() { GLabel label = new GLabel("hello, world", 100, 75); label.setFont("SansSerif-36"); label.setColor(Color.RED); add(label); }}

label

hello, world

Graphic courtesy of Eric Roberts