Lecture25
Lecture25
Lecture 25
2
Learning Goals for Today
3
Plan for Today
4
There are two types
of internet programs:
servers and clients.
5
Clients send requests to
servers. Servers respond
to those requests.
6
Facebook Server
Laura Cruz-Albrecht
“Laura Cruz-Albrecht”
7
URL
In PollClient, change the HOST constant to:
http://cbc0026a.ngrok.io
8
Real Java?
9
Of Course We Did!
10
ACM Libraries
11
ACM Libraries
12
Hello World with ACM
This was the first ConsoleProgram we ever wrote! Notice:
● it imports acm.program.*
● it extends a ConsoleProgram
● It uses public void run()
● It uses println()
import acm.program.*;
15
main
Why public static void main(String[] args)?
17
Why did we use ConsoleProgram?
18
Standard Java Version
19
Reading Input with ACM
This is a short program that read in input! Notice:
27
Basic GUI with ACM
public class ColorFun extends Program {
public void init() {
JButton redButton = new JButton(“Red!”);
JButton blueButton = new JButton(“Blue!”);
add(redButton, SOUTH);
add(blueButton, SOUTH);
addActionListeners();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(“Blue!”){
setBackground(Color.BLUE);
} else {
setBackground(Color.RED);
}
}
} 28
Basic GUI with Standard Java
public class ColorFun implements ActionListener {
public static void main(String[] args) { public void actionPerformed(ActionEvent e) {
new ColorFun().init(); if(e.getActionCommand().equals(“Blue!”){
} frame.setBackground(Color.BLUE);
private JFrame frame; } else {
public void init() { frame.setBackground(Color.RED);
frame = new JFrame(“ColorFun”); }
frame.setSize(500, 300); }
JButton redButton = new JButton(“Red!”); }
JButton blueButton = new JButton(“Blue!”);
redButton.addActionListener(this);
blueButton.addActionListener(this);
frame.add(redButton, “South”);
frame.add(blueButton, “North”);
frame.setVisible(true);
}
29
Basic GUI with Standard Java
public class ColorFun implements ActionListener {
public static void main(String[] args) { public void actionPerformed(ActionEvent e) {
new ColorFun().init(); if(e.getActionCommand().equals(“Blue!”){
} frame.setBackground(Color.BLUE);
private JFrame frame; } else {
public void init() { frame.setBackground(Color.RED);
frame = new JFrame(“ColorFun”); }
frame.setSize(500, 300); }
JButton redButton = new JButton(“Red!”); }
JButton blueButton = new JButton(“Blue!”);
redButton.addActionListener(this);
blueButton.addActionListener(this);
frame.add(redButton, “South”);
frame.add(blueButton, “North”);
frame.setVisible(true);
}
30
Basic GUI with Standard Java
public class ColorFun implements ActionListener {
public static void main(String[] args) { public void actionPerformed(ActionEvent e) {
new ColorFun().init(); if(e.getActionCommand().equals(“Blue!”){
} frame.setBackground(Color.BLUE);
private JFrame frame; } else {
public void init() { frame.setBackground(Color.RED);
frame = new JFrame(“ColorFun”); }
frame.setSize(500, 300); }
JButton redButton = new JButton(“Red!”); }
JButton blueButton = new JButton(“Blue!”);
redButton.addActionListener(this);
blueButton.addActionListener(this);
frame.add(redButton, “South”);
frame.add(blueButton, “North”);
frame.setVisible(true);
}
31
Basic GUI with Standard Java
public class ColorFun implements ActionListener {
public static void main(String[] args) { public void actionPerformed(ActionEvent e) {
new ColorFun().init(); if(e.getActionCommand().equals(“Blue!”){
} frame.setBackground(Color.BLUE);
private JFrame frame; } else {
public void init() { frame.setBackground(Color.RED);
frame = new JFrame(“ColorFun”); }
frame.setSize(500, 300); }
JButton redButton = new JButton(“Red!”); }
JButton blueButton = new JButton(“Blue!”);
redButton.addActionListener(this);
blueButton.addActionListener(this);
frame.add(redButton, “South”);
frame.add(blueButton, “North”);
frame.setVisible(true);
}
32
Basic GUI with Standard Java
public class ColorFun implements ActionListener {
public static void main(String[] args) { public void actionPerformed(ActionEvent e) {
new ColorFun().init(); if(e.getActionCommand().equals(“Blue!”){
} frame.setBackground(Color.BLUE);
private JFrame frame; } else {
public void init() { frame.setBackground(Color.RED);
frame = new JFrame(“ColorFun”); }
frame.setSize(500, 300); }
JButton redButton = new JButton(“Red!”); }
JButton blueButton = new JButton(“Blue!”);
redButton.addActionListener(this);
blueButton.addActionListener(this);
frame.add(redButton, “South”);
frame.add(blueButton, “North”);
frame.setVisible(true);
}
33
Basic GUI with Standard Java
We had to create a frame.
34
Why Not Libraries?
35
Basic GUI with ACM
● Benefits of libraries:
○ simplify syntax/rough edges of language/API
○ avoid re-writing the same code over and over
○ possible to make advanced programs quickly
○ leverage work of others
Yay! ❤
36
Basic GUI with ACM
● Benefits of libraries:
○ simplify syntax/rough edges of language/API
○ avoid re-writing the same code over and over
○ possible to make advanced programs quickly
○ leverage work of others
Oh. 😔
● Drawbacks of libraries:
○ limitations on usage; e.g. ACM library cannot
be distributed for commercial purposes.
37
Creating Our Own Java Project
What other sorts of cool Java libraries are out there?
38
Reading from the Web
We’re going to write a program that will grab HTML from a
webpage from the internet and print out important
information from that page!
39
Let’s Create an Empty Project First!
To create an empty project on your own, you don’t need
the starter code!
You will need the starter code for some of the cool code
we’ll use to read the HTML from our web page.
40
Let’s Create an Empty Project First!
In your Eclipse menu, click on:
41
Let’s Create an Empty Project First!
Give your project a name:
Click Finish:
42
Let’s Create an Empty Project First!
Eclipse will ask if you want to create a module.
43
Let’s Create a lib Folder
In your Eclipse menu, click on:
44
Let’s Create a lib Folder
Select your
ReadingFromTheWeb
project folder.
45
Let’s Get a .JAR
We’re going to use the
jsoup library to process
HTML in a webpage. Let’s
go get it!
46
Let’s Get a .JAR
After you’ve clicked Download, you will see some links.
47
Let’s Get a .JAR
Let’s put the .JAR file we just downloaded into our lib folder we
just created!
Step 1: Open your Folder Step 2: Open lib Step 3: Drag the .jar
file into lib
48
Refresh Our Project
Now, we can return to Eclipse and refresh our project!
49
Configuring Our Build Path
Even though we’ve added our .jar to our folder, we need to
add the .jar to our Build Path.
50
Configuring Our Build Path
Click:
● Libraries
● Classpath
● Add JARs...
51
Configuring Our Build Path
Select our jsoup jar and click OK. Then, you can press Apply
and Close.
52
Creating Our Program Class
Now, we create our
JSoupExample class so we can
start coding!
53
Starter Code
While you could this program from Scratch there is a good
starting point in the Starter Code.
54
Documentation/API
Using the Documentation from the JSoup website, we can
create a program that will:
https://jsoup.org/cookbook/extracting-data/selector-syntax
55
Let’s Code It!
56
Plan for Today