550 likes | 720 Views
V2012.13. 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. HTML/CSS Class. QUESTIONS?. Tech Club Executive Committee. Next Year: Election of Officers
E N D
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
HTML/CSS Class QUESTIONS?
Tech Club Executive Committee • Next Year: • Election of Officers • President • Vice President • Secretary • Treasurer • Send me an email with interest
Website Help • Two Projects • Non-Profit • Realtor • Contact me if you are interested
Upcoming Schedule • Today: Intro to Java • May 14th: Intro to C# (Guest Speaker) • May 21st: Embedded Programming • May 28th: TBD
Intro to Java • History • Brief Overview • Installation/Tools • Getting Started • Examples • Resources
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?
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
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
Comparing Java to Python Python Java
Java Conditionals if(this is true){ do this } else{ do this instead }
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. */
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 }
Java Loops /* Find the average of 10 of numbers */ intcounter; 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
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
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
Java Functions/Methods Example /* First we must define a method */ intaddTwoIntegers(int a, int b){ intsum = a + b; return sum; } Method Name Arguments Return Type Method Body Return Value
Java Functions/Methods Example /* First we must define a method */ intaddTwoIntegers(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 */
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 + 12we can use x += 12 • Also -=, *=, /= • The - sign is used for subtraction as well as negative numbers
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”
Quiz Time! System.out.println() is an example of a reusable chunk of code called a ________ method
Quiz Time! If we need our code to behave differently depending on some condition, we use the word _____ if
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
Quiz Time! If we need to repeat an action over and over again and we know how many times, we use a _____ loop for
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 “==“
Quiz Time! What is wrong with this statement: intx = 2.5; x is an integer but 2.5 is a float/double
Quiz Time! int, double, and floatare examples of _____ Types
Quiz Time! What does this block of code do: inti = 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
Getting Started with Java • Download Java Development Kit (JDK) • http://tinyurl.com/355cx3m • http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html • Download JDK + NetBeans • http://tinyurl.com/76mgogl • Java/NetBeansQuick Start • https://netbeans.org/kb/docs/java/quickstart.html • Tip: Configure PATH
Steps to Creating a Java Application Compiler (javac) Execute (java) Source Code (.java) Bytecode (.class)
Step 1: Source Code class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } }
Step 1: Source Code • Every application begins with a class definition class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); } }
Step 1: Source Code • In the Java programming language, every application must contain a mainmethod, 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
Step 1: Source Code • In the Java programming language, every application must contain a mainmethod, where app starts to execute: class HelloWorldApp { publicstatic void main(String[] args) { System.out.println("Hello World!"); } } • The statickeyword means main can be called before an object of the class has been created.
Step 1: Source Code • In the Java programming language, every application must contain a mainmethod, 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.
Step 1: Source Code • In the Java programming language, every application must contain a mainmethod, where app starts to execute: class HelloWorldApp { public static voidmain(String[] args){ System.out.println("Hello World!"); } } • The String declares a parameter named argsof type String (array)
Step 1: Source Code • The last line uses the Systemclass 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!"); } }
Step 1: Source Code • The last line uses the Systemclass 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!"); } }
Step 2: Compile Your Code javac HelloWorld.java • Provided there are no compilation errors, you should now have a .class file: HelloWorld.class
Step 3: Execute Your Application java HelloWorld Hello World! Holla! You just created your first Java App!
Creating Rich Clients • Swing • JavaFX/Scene Builder
Resources • Java Tutorial • http://docs.oracle.com/javase/tutorial/ • Learning • http://www.learnjavaonline.org/
Getting Started 1. Download the Java Development Kit (JDK 7) (For Ubuntu: sudoapt-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)
PATH on Windows Enter javacat 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"