0% found this document useful (0 votes)
17 views

Lecture25

This lecture covers how to start Java programs, focusing on both standard Java and the use of ACM libraries. It includes practical examples of console and GUI applications, as well as instructions for creating a Java project using the jsoup library to read HTML from the web. The session emphasizes the importance of understanding real Java programming while also highlighting the benefits and limitations of using libraries.

Uploaded by

maxs34787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture25

This lecture covers how to start Java programs, focusing on both standard Java and the use of ACM libraries. It includes practical examples of console and GUI applications, as well as instructions for creating a Java project using the jsoup library to read HTML from the web. The session emphasizes the importance of understanding real Java programming while also highlighting the benefits and limitations of using libraries.

Uploaded by

maxs34787
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

How to Start Your Own Java Programs

Lecture 25

CS106A, Summer 2019


Sarai Gould && Laura Cruz-Albrecht
With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech, Brahm Capoor, & others.
Announcements

● Download blank starter code from the website!

2
Learning Goals for Today

Learn About Writing Java code in the Real World!

3
Plan for Today

● Review: Server & Client


● “Real” Java
● Console Program
● Basic Gui
● Creating Our Own Project!
● Example: Reading HTML with JSoup

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

Send me the full name for


[email protected]

[email protected]

Laura Cruz-Albrecht

“Laura Cruz-Albrecht”

7
URL
In PollClient, change the HOST constant to:

http://cbc0026a.ngrok.io

8
Real Java?

Does this mean we didn’t


learn REAL Java?

9
Of Course We Did!

While we did use some Stanford specific


Java libraries at times, you did learn
REAL Java!
Phew, that’s
All of the data structures, primitives, good.
classes, loops, etc are all part of real
programming and real Java!

10
ACM Libraries

All quarter, we have used the Java ACM Libraries.

● Karel, ConsoleProgram, RandomGenerator…


● GraphicsProgram, GOval, GRect, GImage...

Can you imagine I can’t...


Java without me?!

11
ACM Libraries

Today, we’ll look at how standard Java programs are


written!

Sounds fun! Can’t wait! 😊

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.*;

public class HelloWorld extends ConsoleProgram {


public void run() {
println(“Hello, world!”);
}
}
13
Standard Hello World
This is Hello World in Standard Java! Notice:

● no imports or extends needed for this program!


● It uses public static void main(String[] args)
● It uses System.out.println()

public class HelloWorld{


public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}
14
main
Why public static void main(String[] args)?

main is the true entry point for a program in Java.

● It must be written exactly as seen above!


● String[] args is an array of String arguments given to
the program.

15
main
Why public static void main(String[] args)?

main is the true entry point for a program in Java.

● It must be written exactly as seen above!


● String[] args is an array of String arguments given to
the program.

System.out.println() is the true println()

Standard Java methods are static unless part of a class


of objects.
16
Why did we use ConsoleProgram?
Creates a new window.

Puts a new scrollable area in the window.

Provides print and println commands to send text output


to the window.

Contains a main method that calls your run method.

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:

● It uses readLine and readInt to read input


import acm.program.*;

public class SayingHello extends ConsoleProgram {


public void run() {
String name = readLine(“What’s your name?”);
int age = readInt(“How old are you?”);

println(“Hello” + name + “!”);


println(name + “ is “ + age + “ years old.”);
}
}
20
Reading Input with Standard Java
This is another short program that read in input!
import java.util.Scanner;

public class SayingHello{


public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(“What’s your name?”);
String name = console.nextLine();
System.out.println(“What’s your name?”);
int age = console.nextInt();

System.out.println(“Hello” + name + “!”);


System.out.println(name + “ is “ + age + “ years old.”);
}
}
21
Reading Input with Standard Java
This is another short program that read in input!
import java.util.Scanner;
We have to
create a Scanner
public class SayingHello{
to read input!
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(“What’s your name?”);
String name = console.nextLine();
System.out.println(“What’s your name?”);
int age = console.nextInt();

System.out.println(“Hello” + name + “!”);


System.out.println(name + “ is “ + age + “ years old.”);
}
}
22
Reading Input with Standard Java
This is another short program that read in input!
import java.util.Scanner;
System.in is
another name for
public class SayingHello{
the console here.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(“What’s your name?”);
String name = console.nextLine();
System.out.println(“What’s your name?”);
int age = console.nextInt();

System.out.println(“Hello” + name + “!”);


System.out.println(name + “ is “ + age + “ years old.”);
}
}
23
Reading Input with Standard Java
This is another short program that read in input!
import java.util.Scanner;

public class SayingHello{ We have to print out


public static void main(String[] args) { the question using:
Scanner console = new Scanner(System.in); System.out.println
System.out.println(“What’s your name?”);
String name = console.nextLine();
System.out.println(“What’s your name?”);
int age = console.nextInt();

System.out.println(“Hello” + name + “!”);


System.out.println(name + “ is “ + age + “ years old.”);
}
}
24
Reading Input with Standard Java
This is another short program that read in input!
import java.util.Scanner;

public class SayingHello{


public static void main(String[] args) {
Scanner console = new Scanner(System.in); We read in the input
System.out.println(“What’s your name?”); using:
String name = console.nextLine(); console.readLine()
System.out.println(“What’s your name?”);
int age = console.nextInt();

System.out.println(“Hello” + name + “!”);


System.out.println(name + “ is “ + age + “ years old.”);
}
}
25
Reading Input with Standard Java
This is another short program that read in input!
import java.util.Scanner;

public class SayingHello{


public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println(“What’s your name?”);
String name = console.nextLine();
System.out.println(“What’s your name?”);
int age = console.nextInt();

System.out.println(“Hello” + name + “!”);


System.out.println(name + “ is “ + age + “ years old.”);
}
}
26
GraphicsPrograms?
Creates a new window.

Creates a drawing canvas in the center of the window.

Provides convenient methods to detect Mouse Events.

Contains a main method that calls your run method.

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.

Adding actionListeners was a lot more involved!

34
Why Not Libraries?

Why don’t we just use


ACM all the time then?

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?

How do we create our own Java projects using them?

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!

Before we pick the webpage, let’s set up our new Java


Project!

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:

File -> New -> Java Project

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.

Click Don’t Create:

43
Let’s Create a lib Folder
In your Eclipse menu, click on:

File -> New -> Folder

44
Let’s Create a lib Folder
Select your
ReadingFromTheWeb
project folder.

Create a lib folder to


hold any .JAR files we
may want!

Press Finish when done!

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!

If you search “jsoup”


online, you should find it!
Click on Download.

46
Let’s Get a .JAR
After you’ve clicked Download, you will see some links.

Click on jsoup-1.12.1.jar to download the .JAR file that


contains jsoup!

47
Let’s Get a .JAR
Let’s put the .JAR file we just downloaded into our lib folder we
just created!

We can drag it into our folder using Finder or Windows Explorer!

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!

Secondary click (two-finger or right click) on the project name


and press Refresh.

There’s your .jar file!

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.

Let’s Configure 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!

We create this the same way


we created a new project and
a new folder!

53
Starter Code
While you could this program from Scratch there is a good
starting point in the Starter Code.

You can code this portion in the Starter Code, or copy-paste


JSoupExample from the Starter Code into your file!

54
Documentation/API
Using the Documentation from the JSoup website, we can
create a program that will:

● Get HTML from an explore courses page about CS classes


● Print out all of the course titles for CS classes

Links to relevant documentation:


https://jsoup.org/cookbook/extracting-data/attributes-text-html

https://jsoup.org/cookbook/extracting-data/selector-syntax
55
Let’s Code It!

56
Plan for Today

● Review: Server & Client


● “Real” Java
● Console Program
● Basic Gui
● Creating Our Own Project!
● Example: Reading HTML with JSoup

Next Time: Life After 106A


57

You might also like