cs 106a, lecture 25 life after cs 106a, part 1...–puts a scrollable text areainto it –provides...

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: others

Post on 03-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

2

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

Tomorrow:IntrotoMachineLearning!

Page 3: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

3

Anonymous Questions•Whatisyourmusictaste?•HowdidyouknowyouwantedtopursueCS?•Whatwasyourfirstcodefor?

Page 4: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

4

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

Page 5: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

8

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

Page 9: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

18

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

Page 19: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

19

Programming Languages

Page 20: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

20

Programming Languages

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

Page 21: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

25

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

Page 26: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

26

Programs and the Internet

Howdoesyourphone

communicatewithFacebook?

Page 27: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

27

Programs and the Internet

TheJavaprogramonyourphone talkstotheJavaprogram

atFacebook.

Page 28: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

28

Facebook

*AndroidphonesrunJava.SodoFacebookservers!

FacebookServer

Page 29: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

30

FacebookFacebookServer

Send me the full name for

[email protected]

“Nick Troccoli”

NickTroccoli

Page 31: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

31

FacebookFacebookServer

Send me the cover photo for

[email protected]

Page 32: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

32

Programs and the Internet

Therearetwotypesofinternet

programs:clientsandservers.

Page 33: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

33

Programs and the Internet

Clientssendrequests toservers,whorespond tothoserequests.

Page 34: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

34

Servers Are Computer Programs!

FacebookServer

=

Page 35: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

35

Servers

Server

RequestsomeRequest

StringserverResponse

Page 36: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

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 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

37

Clients

Send

ChatClient

Send

ChatClient> Hello world> Hello world

Page 38: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

38

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

Page 39: CS 106A, Lecture 25 Life After CS 106A, Part 1...–Puts a scrollable text areainto it –Provides printand printlncommands to send text outputto that window –contains a mainmethodthat

39

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

Tomorrow:IntrotoMachineLearning!