DOC PREVIEW
Stanford CS 106A - Simple Java

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #13CS 106A January 11, 2010Simple JavaSimple JavaEric RobertsCS 106AJanuary 11, 2010The “Hello World” ProgramOne of the important influences on thedesign of Java was the C programminglanguage, which was developed at BellLabs in the early 1970s. The primaryreference manual for C was written byBrian Kernighan and Dennis Ritchie.On the first page of their book, theauthors suggest that the first step inlearning any language is to write asimple program that prints the message“hello, world” on the display. Thatadvice remains sound today.1.1 Getting Started The only way to learn a new programminglanguage is to write programs in it. The firstprogram to write is the same for all languages: Print the words hello, worldThis is the big hurdle; to leap over it you have tobe able to create the program text somewhere,compile it, load it, run it, and find out where youroutput went. With these mechanical detailsmastered, everything else is comparatively easy. In C, the program to print “hello, world” is #include <stdio.h> main() { printf("hello, world"); }Evolution of Computer Languages The 2002 ACM Turing AwardKristen Nygaard Ole Johan DahlThe most prestigious prize incomputer science, the ACMTuring Award, was given in 2002to two Norwegian computingpioneers, who developed the firstobject-oriented programminglanguage in 1967.Systems of ClassificationIn the mid-18th century, the Scandinavianbotanist Carl Linnaeus revolutionized thestudy of biology by developing a newsystem for classifying plants and animalsin a way that revealed their structuralrelationships and paved the way forDarwin’s theory of evolution a centurylater.Carl Linnaeus (1707–1778)Linnaeus’s great contribution was torecognize that organisms fit into ahierarchical classification scheme in whichthe placement of individual species withinthe hierarchy reflects their anatomicalsimilarities.Biological Class HierarchyCrustacea ArachnidaInsectaAnnelida Brachiopoda Mollusca ChordataArthropodaPlants FungiAnimalsLiving ThingsLiving ThingsAnimalsArthropodaInsectaHymenopteraFormicidaeIridomyrmexpurpureusKingdomPhylumOrderClassFamilyGenusSpeciesClassification of the red antIridomyrmex purpureusEvery red ant is also an animal,an arthropod, and an insect, aswell as the other superclasses inthe chain.– 2 –Instances vs. PatternsDrawn after you, you pattern of all those.—William Shakespeare, Sonnet 98In thinking about any classification scheme—biological orotherwise—it is important to draw a distinction between the categorydefined by a particular class and specific instances of that class. In themost recent example, the designation Iridomyrmex purpureus is notitself an ant, but rather a class of ant. There can be (and usually are)many ants, each of which is an individual of that class.Each of these fire ants is an instance of the general categoryencompassing all ants of its class. Each instance is of the speciespurpureus, the genus Iridomyrmex, the family Formicidae (whichmakes it an ant), and so on. Thus, each ant is not only an ant, but alsoan insect, an arthropod, and an animal.The Program HierarchyAppletJAppletProgramDialogProgram GraphicsProgramConsoleProgramJava class hierarchies aresimilar to the biological classhierarchy from the previousslide. This diagram showsthe hierarchy formed by theclasses in the acm.programpackage.Every ConsoleProgram isalso a Program, a JApplet,and an Applet. That meansthat every ConsoleProgramcan run as an applet on theweb. The same is true forevery DialogProgram andGraphicsProgram.import acm.program.*;public class HelloProgram extends ConsoleProgram { public void run() { println("hello, world"); }}Hello World as a Console Programimport acm.program.*;public class HelloProgram extends ConsoleProgram { public void run() { println("hello, world"); }}HelloConsolehello, worldHello World as a Dialog Programimport acm.program.*;public class HelloProgram extends DialogProgram { public void run() { println("hello, world"); }}import acm.program.*;public class HelloProgram extends DialogProgram { public void run() { println("hello, world"); }}Hello World as a Graphics Programimport acm.graphics.*;import acm.program.*;public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel("hello, world", 100, 75) ); }}HelloProgramhello, worldimport acm.graphics.*;import acm.program.*;public class HelloProgram extends GraphicsProgram { public void run() { add( new GLabel("hello, world", 100, 75) ); }}The Java Coordinate System• Positions and distances in a graphics program are measured interms of pixels, which are the individual dots that cover thescreen.• Unlike traditional mathematics, Java defines the origin of thecoordinate system to be in the upper left corner. Values forthe x coordinate increase as you move rightward across thescreen; y coordinate values increase as you move downward.HelloProgramhello, world(100, 100)• Creating a JLabel at a particular x and y position means thatthe baseline of the first character in the label appears at thatpoint, as


View Full Document

Stanford CS 106A - Simple Java

Download Simple Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Simple Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Simple Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?