cs 106a, lecture 25 life after cs 106a, part 1...–puts a scrollable text areainto it –provides...
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/26.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/27.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/28.jpg)
28
*AndroidphonesrunJava.SodoFacebookservers!
FacebookServer
![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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/32.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/33.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/34.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/35.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/36.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/37.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/38.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070818/5f174d4481b865679333f99c/html5/thumbnails/39.jpg)
39
Recap•Announcements•LifeaftertheACMLibraries•LifeafterJava•LifeafterPCprograms–Internetapplications–Mobileapplications
Tomorrow:IntrotoMachineLearning!