introduction to java - stanford university · introduction to java. announcements ... the java...
TRANSCRIPT
![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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/31.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/32.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/33.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/34.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/35.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/36.jpg)
/* 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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/37.jpg)
/* 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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/38.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/39.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/40.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/41.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/42.jpg)
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](https://reader034.vdocuments.mx/reader034/viewer/2022042605/5f4f229d7aff8b62173e2062/html5/thumbnails/43.jpg)
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