cs 106a, lecture 25 life after cs 106a, part...

39
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, and others. CS 106A, Lecture 25 Life After CS 106A, Part 1

Upload: vandien

Post on 11-Jun-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

Thisdocumentiscopyright(C)StanfordComputerScienceandMartyStepp,licensedunderCreativeCommonsAttribution2.5License.Allrightsreserved.BasedonslidescreatedbyKeithSchwarz,MehranSahami,EricRoberts,StuartReges,andothers.

CS106A,Lecture25LifeAfterCS106A,Part1

Page 2: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

2

Plan for today•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications

Tomorrow:IntrotoMachineLearning!

Page 3: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

3

Anonymous Questions•Whatisyourmusictaste?•HowdidyouknowyouwantedtopursueCS?•Whatwasyourfirstcodefor?

Page 4: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

4

Plan for today•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications

Page 5: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

5

Export to JAR• JAR:JavaArchive.AcompressedbinaryofaJavaprogram.

– ThetypicalwaytodistributeaJavaappasasinglefile.– EssentiallyjustaZIPfilewithJava.classfilesinit.

• MakingaJARofyourprojectinEclipse:– File→Export...→Java→RunnableJARFile

• seehandoutoncoursewebsite

Page 6: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

6

Life After The ACM Libraries• AllquarterwehavereliedontheACMJavalibraries.

– Karel,ConsoleProgram,RandomGenerator– GraphicsProgram,GOval,GRect,GOval,GLine,GImage,...

• TodaywewillseehowstandardJava programsaremade.

Page 7: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

7

Using the ACM Librariesimport acm.program.*;

public class MyProgram extends ConsoleProgram {public void run() {

println("Hello, world!");}

}

• ThisisaconsoleprogramwrittenusingtheACMlibraries.– ItusestheConsoleProgram classtorepresentaconsole.– Therunmethodcontainstheprogramcode.– Theprintlnmethodprintsoutputtothegraphicalconsole.

Page 8: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

8

Pulling Back The CoversMyProgram program = new MyProgram();...program.init();...program.run();...

Page 9: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

9

Pulling Back The Coverspublic static void main(String[] args) {

MyProgram program = new MyProgram();program.init();program.run();

}

Page 10: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

10

A Barebones Java Program

public class Hello {public static void main(String[] args) {

System.out.println("Hello, world!");}

}

• Themethodmain isthetrueentrypointforaJavaprogram.– Itmusthavetheexactheadingshownabove.– TheString[] args are"commandlinearguments"(ignored).– Theprintln command'struenameisSystem.out.println.– StandardJavamethodsarestatic unlesspartofaclassofobjects.

Page 11: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

11

Console Programs• WhatdoestheConsoleProgram libraryclassdo?

– Createsanewgraphicalwindow– Putsascrollabletextarea intoit– Providesprint andprintln commandstosendtextoutput tothatwindow

– containsamainmethod thatcallsyourprogramclass'srunmethod•ConsoleProgram'srun isempty,butyouextendandoverrideit

public class Hello extends ConsoleProgram {public void run() {

println("Hello, world!");}

}

Page 12: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

12

ACM console inputpublic class Age extends ConsoleProgram {

public void run() {String name = readLine("What's your name? ");int age = readInt("How old are you? ");int years = 65 - age;println(name + " has " + years

+ " years until retirement!");}

}

• TheACMlibraryhassimpleconsoleinputcommandslikereadLine,readInt,readDouble,andsoon.

• Thesemethodsdisplaya'prompt'message,waitforinput,re-promptiftheusertypesabadvalue,andreturntheinput.

Page 13: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

13

Java console inputpublic class Age {

public static void main(String[] args) {Scanner console = new Scanner(System.in);System.out.print("What's your name? ");String name = console.nextLine();System.out.print("How old are you? ");int age = console.nextInt();int years = 65 - age;System.out.println(name + " has " + years

+ " years until retirement!");}

}

• InstandardJava,youmustcreateaScanner orsimilarobjecttoreadinputfromtheconsole,whichisalsocalledSystem.in.– Itdoesnotautomaticallyre-promptandcancrashonbadinput.

Page 14: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

14

Graphics ProgramsTheACMlibrarydoesseveralthingstomakegraphicseasier:• Automaticallycreatesanddisplaysawindow onthescreen.

– InstandardJava,wemustdothisourselves;itiscalledaJFrame.

• Setsupadrawingcanvas inthecenterofthewindowInstandardJava,wemustcreateourowndrawingcanvas.

• Providesconvenientmethodstolistenformouseevents.– InstandardJava,eventhandlingtakesabitmorecodetosetup.

Page 15: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

15

ACM GUI examplepublic class ColorFun extends Program {

public void init() {JButton button1 = new JButton("Red!");JButton button2 = new JButton("Blue!");add(button1, SOUTH);add(button2, SOUTH);addActionListeners();

}public void actionPerformed(ActionEvent event) {

if (event.getActionCommand().equals("Red!")) {setBackground(Color.BLUE);

} else {setBackground(Color.RED);

}}

}

Page 16: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

16

Java GUI examplepublic class ColorFun implements ActionListener {

public static void main(String[] args) {new ColorFun().init();

}private JFrame frame;public void init() {

frame = new JFrame("ColorFun");frame.setSize(500, 300);JButton button1 = new JButton("Red!");JButton button2 = new JButton("Blue!");button1.addActionListener(this);button2.addActionListener(this);frame.add(button1, "South");frame.add(button2, "South");frame.setVisible(true);

}public void actionPerformed(ActionEvent event) {

if (event.getActionCommand().equals("Red!")) {frame.setBackground(Color.BLUE);

} else {frame.setBackground(Color.RED);

} } }

Page 17: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

17

Summary• Benefitsoflibraries:

– simplifysyntax/roughedgesoflanguage/API– avoidre-writingthesamecodeoverandover– possibletomakeadvancedprogramsquickly– leverageworkofothers

• Drawbacksoflibraries:– learna"dialect"ofthelanguage("ACMJava"vs."realJava")– lackofunderstandingofhowlowerlevelsorrealAPIswork– somelibrariescanbebuggyorlackdocumentation– limitationsonusage;e.g.ACMlibrarycannotbere-distributedforcommercialpurposes

Page 18: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

18

Plan for today•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications

Page 19: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

19

Programming Languages

Page 20: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

20

Programming Languages

https://imgs.xkcd.com/comics/standards.png

Page 21: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

21

JavaArrayList<Double> evens = new ArrayList<>();for(int i = 0; i < 100; i++) {

if(i % 2 == 0) {evens.add(i);

}}println(evens);

prints [2, 4, 6, 8, 10, 12, … ]

Page 22: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

22

C++Vector<double> evens;for(int i = 0; i < 100; i++) {

if(i % 2 == 0) {evens.add(i);

}}cout << evens << endl;

prints [2, 4, 6, 8, 10, 12, … ]

Page 23: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

23

Pythonevens = []for i in range(100):

if i % 2 == 0:evens.append(i)

print evens

prints [2, 4, 6, 8, 10, 12, … ]

Page 24: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

24

Javascriptvar evens = []for(var i = 0; i < 100; i++) {

if(i % 2 == 0) {evens.push(i)

}}console.log(evens)

prints [2, 4, 6, 8, 10, 12, … ]

Page 25: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

25

Plan for today•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications

Page 26: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

26

Programs and the Internet

Howdoesyourphone

communicatewithFacebook?

Page 27: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

27

Programs and the Internet

TheJavaprogramonyourphone talkstotheJavaprogram

atFacebook.

Page 28: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

28

Facebook

*AndroidphonesrunJava.SodoFacebookservers!

FacebookServer

Page 29: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

29

FacebookFacebookServer

[email protected]

Is this login ok?

Confirmed. [email protected] now logged in.

Page 30: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

30

FacebookFacebookServer

Send me the full name for

[email protected]

“Nick Troccoli”

NickTroccoli

Page 31: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

31

FacebookFacebookServer

Send me the cover photo for

[email protected]

Page 32: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

32

Programs and the Internet

Therearetwotypesofinternet

programs:clientsandservers.

Page 33: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

33

Programs and the Internet

Clientssendrequests toservers,whorespond tothoserequests.

Page 34: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

34

Servers Are Computer Programs!

FacebookServer

=

Page 35: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

35

Servers

Server

RequestsomeRequest

StringserverResponse

Page 36: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

36

What is a Request?

/* Request has a command */String command;

/* Request has parameters */HashMap<String,String> params;

Page 37: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

37

Clients

Send

ChatClient

Send

ChatClient> Hello world> Hello world

Page 38: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

38

Plan for today•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications

Page 39: CS 106A, Lecture 25 Life After CS 106A, Part 1web.stanford.edu/class/archive/cs/cs106a/cs106a.1178/...4 Plan for today •Announcements •Life after the ACM Libraries •Life after

39

Recap•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications

Tomorrow:IntrotoMachineLearning!