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

eyelashesnectarineΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 9 μήνες)

133 εμφανίσεις

V2012.13

2

Avon High School Tech
Club

Agenda


Old Business


Delete
Files


New Business


Week 19 Topics:


Intro to HTML/CSS: Questions?


Group
E
xecutive Committee


Website Help


Introduction to Python Review


Intro to Java


3

Avon High School Tech
Club

HTML/CSS Class

QUESTIONS?

4

Avon High School Tech
Club

Tech Club Executive Committee


Next Year:


Election of Officers


President


Vice President


Secretary


Treasurer


Send me an email with interest

5

Avon High School Tech
Club

Website Help


Two Projects


Non
-
Profit


Realtor


Contact me if you are interested

6

Avon High School Tech
Club

Upcoming Schedule


Today:
Intro to Java


May 14
th
:
Intro to C#
(Guest Speaker)


May 21
st
:
Embedded Programming


May 28
th
: TBD

7

Avon High School Tech
Club

Python review

8

Avon High School Tech
Club

INTRO TO JAVA

9

Avon High School Tech
Club

Intro to
Java


History


Brief Overview


Installation/Tools


Getting Started


Examples


Resources

10

Avon High School Tech
Club


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?

Java Overview

11

Avon High School Tech
Club

More on Java


Object
-
Oriented
programming language


H
ow
we think about the world (nouns
and verbs
)


Java
is the 1st or 2nd most popular, depending
on who you ask and when

12

Avon High School Tech
Club

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

13

Avon High School Tech
Club

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

14

Avon High School Tech
Club

Comparing Java to Python

Python

Java

15

Avon High School Tech
Club

Java Conditionals

if(
this is true
){


do
this


}

else{


do
this instead


}

16

Avon High School Tech
Club

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


17

Avon High School Tech
Club

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


}

18

Avon High School Tech
Club

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

19

Avon High School Tech
Club

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

20

Avon High School Tech
Club

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

21

Avon High School Tech
Club

Java
Functions/Methods Example

/* First we must define a method */


int

addTwoIntegers
(
int

a,
int

b
){


int

sum = a + b;


return
sum;

}

Return Type

Method Name

Arguments

Method Body

Return Value

22

Avon High School Tech
Club

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 */

23

Avon High School Tech
Club

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

24

Avon High School Tech
Club

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


25

Avon High School Tech
Club

Quiz Time!

System.out.println
()
is an
example of a reusable
chunk of
code
called a ________

method

26

Avon High School Tech
Club

Quiz Time!

If we need our code to behave
differently depending
on some
condition, we use the word _____

if

27

Avon High School Tech
Club

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

28

Avon High School Tech
Club

Quiz Time!

If we need to repeat an action over
and over again
and we
know how
many times, we use a _____ loop

for

29

Avon High School Tech
Club

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 “==“

30

Avon High School Tech
Club

Quiz Time!

What is wrong with this
statement:


int

x =
2.5;

x is an integer but 2.5 is a float/double

31

Avon High School Tech
Club

Quiz Time!

int
, double
, and
float

are
examples of
_____

Types

32

Avon High School Tech
Club

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

33

Avon High School Tech
Club

GETTING STARTED

34

Avon High School Tech
Club

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

35

Avon High School Tech
Club

Steps to Creating a Java Application

Source Code

(.java)

Compiler

(
javac
)

Bytecode

(.class)

Execute

(java)

36

Avon High School Tech
Club

Step 1: Source Code


class
HelloWorldApp

{



public
static void main(String[]
args
) {



System.out.println
("Hello World
!");



}

}

37

Avon High School Tech
Club

Step 1: Source Code


Every
application begins with a class
definition


class
HelloWorldApp

{




public
static void main(String[]
args
) {



System.out.println
("Hello World
!");



}

}

38

Avon High School Tech
Club

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

39

Avon High School Tech
Club

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.

40

Avon High School Tech
Club

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.

41

Avon High School Tech
Club

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)

42

Avon High School Tech
Club

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
!");



}

}


43

Avon High School Tech
Club

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
!");



}

}


44

Avon High School Tech
Club

Step
2: Compile Your Code


javac

HelloWorld.java



Provided there are no compilation errors, you
should now have a
.class
file:


HelloWorld.class

45

Avon High School Tech
Club

Step
3: Execute Your Application


java
HelloWorld


Hello World!

Holla
!

You
just created
your
first

Java
App
!

46

Avon High School Tech
Club

Creating Rich Clients


Swing


JavaFX
/Scene Builder


47

Avon High School Tech
Club

Resources


Java Tutorial


http://docs.oracle.com/javase/tutorial
/


Learning


http://www.learnjavaonline.org
/

48

Avon High School Tech
Club

Your turn …

49

Avon High School Tech
Club

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
)

50

Avon High School Tech
Club

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"

51

Avon High School Tech
Club

Write Your Own Java Program

Write a program that prints the
name of your operating system and
Java version.



Hint:

Search for system properties …

52

Avon High School Tech
Club

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
));


}

}

53

Avon High School Tech
Club

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 …

54

Avon High School Tech
Club

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
();

55

Avon High School Tech
Club

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
();


}


}

}