AHS-TechClub-050713 - Avon High School Tech Crew

Download Report

Transcript AHS-TechClub-050713 - Avon High School Tech Crew

V2012.13
Agenda
• Old Business
– Delete Files
• New Business
– Week 19 Topics:
•
•
•
•
•
Intro to HTML/CSS: Questions?
Group Executive Committee
Website Help
Introduction to Python Review
Intro to Java
Avon High School Tech Club
2
HTML/CSS Class
QUESTIONS?
Avon High School Tech Club
3
Tech Club Executive Committee
• Next Year:
– Election of Officers
•
•
•
•
President
Vice President
Secretary
Treasurer
• Send me an email with interest
Avon High School Tech Club
4
Website Help
• Two Projects
– Non-Profit
– Realtor
• Contact me if you are interested
Avon High School Tech Club
5
Upcoming Schedule
•
•
•
•
Today: Intro to Java
May 14th: Intro to C# (Guest Speaker)
May 21st: Embedded Programming
May 28th: TBD
Avon High School Tech Club
6
Python review
Avon High School Tech Club
7
INTRO TO JAVA
Avon High School Tech Club
8
Intro to Java
•
•
•
•
•
•
History
Brief Overview
Installation/Tools
Getting Started
Examples
Resources
Avon High School Tech Club
9
Java Overview
•
•
•
•
•
•
Over 17 years old
Used in mobile, desktop and enterprise apps
Emphasizes code portability
Statically typed
Runs on multiple platforms (via JVM)
Did you know Android is Java-based?
Avon High School Tech Club
10
More on Java
• Object-Oriented programming language
– How we think about the world (nouns and verbs)
• Java is the 1st or 2nd most popular, depending
on who you ask and when
Avon High School Tech Club
11
Java Variables
• Variable Naming Conventions:
– Variable names must start with a lowercase letter
– Variable names can’t have spaces in them
– Variable names can have numbers in them, but they
can’t start with them
– Variables can’t have any symbols in them except for
an underscore ( the _ character)
• Achieved in Java using Types
Avon High School Tech Club
12
Java Types … called Primitives
Java Type
Example
byte
byte b = 100;
Integer
short
short s = 10000;
Integer
int
int n = 5;
Integer
long
long ssNum = 999_99_9999L;
Integer
float
float f = (float) 4.5;
Decimal
double
double x = 123.4;
Decimal
boolean
boolean b = false;
true or false
String
String s = “Hello";
Words
char
char c = ‘t';
Letter
Avon High School Tech Club
13
Comparing Java to Python
Python
Avon High School Tech Club
Java
14
Java Conditionals
if(this is true){
do this
}
else{
do this instead
}
Avon High School Tech Club
15
Java Conditionals
if(x > 10){
System.out.println(“x is big!”);
}
else{
System.out.println(“x is small!”);
}
/* System.out.println() just tells Java to print what’s in the quotes. */
Avon High School Tech Club
16
Java Loops
/* Keep doing this until it isn’t true anymore */
while(this is true){
do domething
}
/* Or repeat something an exact number of times */
for(counter = 0; counter < 10; counter = counter + 1){
do this every time
}
Avon High School Tech Club
17
Java Loops
/* Find the average of 10 of numbers */
int counter;
float sum = 0;
for(counter = 0; counter < 10; counter = counter + 1){
sum = sum + nextNumber;
}
float average = sum / 10;
For now, we’ll pretend that nextNumber comes from somewhere
Avon High School Tech Club
18
Java Functions/Methods
• There are many situations where you will write
code that you want to use again, executing the
same instructions but on a different set of data.
• This is why we use functions/methods
• In the System.out.println() example, the
String in quotes is the argument. The job of
System.out.println()is to print its
argument to the screen
• Arguments can be variables or constants, it
depends on the method
Avon High School Tech Club
19
Java Functions/Methods
• Functions/Methods:
– Reusable, named chunks of code
– You’ve seen a Method already:
• System.out.println()
• Methods can take in arguments, represents
data that the method will manipulate
Avon High School Tech Club
20
Java Functions/Methods Example
/* First we must define a method */
int addTwoIntegers(int a, int b){
int sum = a + b;
return sum;
Method Name
}
Arguments
Return Type
Method Body
Return Value
Avon High School Tech Club
21
Java Functions/Methods Example
/* First we must define a method */
int addTwoIntegers(int a, int b){
return a + b;
}
/* Then we use the method */
addTwoIntegers(10,15);
/* 25! */
/* We can treat a method like it is a variable
of its return type */
int x = addTwoIntegers(10, 15); /* x is 25 */
Avon High School Tech Club
22
Java Programming Tips
• Increment and Decrement an integer (add or
subtract 1)
– Instead of x = x + 1 use x++ or ++x (use -- for
decrement)
– All of the arithmetic operators can be “combined” with
an equal sign
– Instead of x = x + 12 we can use x += 12
– Also -=, *=, /=
– The - sign is used for subtraction as well as negative
numbers
Avon High School Tech Club
23
Java Programming Tips
• Equals like assignment vs. equals like
comparison
– We use = to assign a value to a variable, so how do
we ask if something is equal to something else?
– == means “is equal to”
• When you’re “reading” code, you should read “=”
as “becomes” and “==” as “is equal to”
Avon High School Tech Club
24
Quiz Time!
System.out.println() is an
example of a reusable chunk of code
called a ________
method
Avon High School Tech Club
25
Quiz Time!
If we need our code to behave
differently depending on some
condition, we use the word _____
if
Avon High School Tech Club
26
Quiz Time!
If we need to repeat an action over and
over again but we aren’t sure how
many times, we use a _____ loop
while
Avon High School Tech Club
27
Quiz Time!
If we need to repeat an action over
and over again and we know how
many times, we use a _____ loop
for
Avon High School Tech Club
28
Quiz Time!
True or False: If we want to make a
decision based on whether or not a
variable is equal to something, we
use “=”
False
We use “==“
Avon High School Tech Club
29
Quiz Time!
What is wrong with this statement:
int x = 2.5;
x is an integer but 2.5 is a float/double
Avon High School Tech Club
30
Quiz Time!
int, double, and float are
examples of _____
Types
Avon High School Tech Club
31
Quiz Time!
What does this block of code do:
int i = 0;
while(i < 10) {
System.out.println(i);
i = i + 1;
a.) Print “i” 10 times
}
b.) Prints the numbers 1 - 10
c.) Prints the numbers 0 - 10
d.) Prints the numbers 0 - 9
Avon High School Tech Club
32
GETTING STARTED
Avon High School Tech Club
33
Getting Started with Java
• Download Java Development Kit (JDK)
– http://tinyurl.com/355cx3m
– http://www.oracle.com/technetwork/java/javase/downl
oads/jdk7-downloads-1880260.html
• Download JDK + NetBeans
– http://tinyurl.com/76mgogl
• Java/NetBeans Quick Start
– https://netbeans.org/kb/docs/java/quickstart.html
• Tip: Configure PATH
Avon High School Tech Club
34
Steps to Creating a Java Application
Source Code
(.java)
Avon High School Tech Club
Compiler
(javac)
Bytecode
(.class)
Execute
(java)
35
Step 1: Source Code
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Avon High School Tech Club
36
Step 1: Source Code
• Every application begins with a class definition
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Avon High School Tech Club
37
Step 1: Source Code
• In the Java programming language, every application must
contain a main method, where app starts to execute:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• The public keyword is called an access modifier,
meaning it can be accessed by code outside the class
Avon High School Tech Club
38
Step 1: Source Code
• In the Java programming language, every application must
contain a main method, where app starts to execute:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• The static keyword means main can be called before
an object of the class has been created.
Avon High School Tech Club
39
Step 1: Source Code
• In the Java programming language, every application must
contain a main method, where app starts to execute:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• The void keyword tells the compiler main does not return
a value.
Avon High School Tech Club
40
Step 1: Source Code
• In the Java programming language, every application must
contain a main method, where app starts to execute:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
• The String declares a parameter named args of type
String (array)
Avon High School Tech Club
41
Step 1: Source Code
• The last line uses the System class from the core library
to print the "Hello World!" message to standard output.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Avon High School Tech Club
42
Step 1: Source Code
• The last line uses the System class from the core library
to print the "Hello World!" message to standard output.
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Avon High School Tech Club
43
Step 2: Compile Your Code
javac HelloWorld.java
• Provided there are no compilation errors, you
should now have a .class file:
HelloWorld.class
Avon High School Tech Club
44
Step 3: Execute Your Application
java HelloWorld
Hello World!
Avon High School Tech Club
Holla!
You just created
your first
Java App!
45
Creating Rich Clients
• Swing
• JavaFX/Scene Builder
Avon High School Tech Club
46
Resources
• Java Tutorial
– http://docs.oracle.com/javase/tutorial/
• Learning
– http://www.learnjavaonline.org/
Avon High School Tech Club
47
Your turn …
Avon High School Tech Club
48
Getting Started
1. Download the Java Development Kit (JDK 7)
(For Ubuntu: sudo apt-get install openjdk-7-jdk)
2. Open a text editor
3. Write a simple ‘Hello World’ program
4. Save it as “HelloWorld.java”
5. Compile it (javac HelloWorld.java)
6. Run your program (java HelloWorld)
Avon High School Tech Club
49
PATH on Windows
Enter javac at command prompt, if you receive an error,
install the JDK
If JDK is installed, you may need to set the PATH
At the command prompt:
set path=%path%;“C:\Program Files\Java\jdk1.7.0_21\bin"
Avon High School Tech Club
50
Write Your Own Java Program
Write a program that prints the
name of your operating system and
Java version.
Hint:
Search for system properties …
Avon High School Tech Club
51
Challenge 1
public class SysInfo {
public static void main(String[] args) {
String nameOS = "os.name";
String javaVersion = "java.version";
System.out.println("\n
General System Information");
System.out.println("\n OS Name: " + System.getProperty(nameOS));
System.out.println(" Java Version: " + System.getProperty(javaVersion));
}
}
Avon High School Tech Club
52
Advanced Challenge …
Write a program that prints the
name of your operating system
and Java version, then writes it to
a file called sys.info
Hint:
Search for setProperty and file I/O …
Avon High School Tech Club
53
Challenge 2
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropApp
{
public static void main( String[] args ) {
String nameOS = "os.name";
String javaVersion = "java.version";
Properties prop = new Properties();
Avon High School Tech Club
54
Challenge 2
try {
//set the properties value
prop.setProperty("OperatingSystem", System.getProperty(nameOS));
prop.setProperty("JavaVersion", System.getProperty(javaVersion));
//save properties to project root folder
prop.store(new FileOutputStream(“sys.info”), null);
System.out.println("\n
System info stored in sys.info”);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Avon High School Tech Club
55