+ All Categories
Home > Software > Java 101 intro to programming with java

Java 101 intro to programming with java

Date post: 14-Apr-2017
Category:
Upload: hawkman-academy
View: 395 times
Download: 2 times
Share this document with a friend
78
Java 101: Intro to Java Programming
Transcript
Page 1: Java 101  intro to programming with java

Java 101: Intro to Java Programming

Page 2: Java 101  intro to programming with java

Introduction

• Your Name• Your day job• Your last holiday destination?

Page 3: Java 101  intro to programming with java

Java 101

• Java Fundamentals– Setting up your development environment– Language Overview– How Java Works– Writing your first program– Built-in Data Types– Conditionals and Loops

Page 4: Java 101  intro to programming with java

Java 102

• Object-oriented Programming– Classes and Objects– Polymorphism, Inheritance and Encapsulation– Functions and Libraries

Page 5: Java 101  intro to programming with java

Java 103

• Data Structures– Arrays– Collections– Algorithms

Page 6: Java 101  intro to programming with java

Java 101: Introduction to Java

Setting up your Development Environment

Page 7: Java 101  intro to programming with java

Installing Java Development Kit• Download latest Java SE 8 JDK (not JRE) from

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

• For Windows, – download the X86 version, double click the .exe file and follow the instructions,

accepting all default• For MACs,

– check if java already installed (javac –version) and if not, download the JDK dmg file, run it and follow the instructions.

• After installation is complete, type javac –version in the Command window (Terminal window on MAC OS)- – The reported version should be 1.8.... – If not, you may need to modify the system variable PATH to include the bin

directory of JDK

Page 8: Java 101  intro to programming with java

What is an IDE?

• IDE = Integrated Development Environment • Makes you more productive • Includes text editor, compiler, debugger, context-

sensitive help, works with different Java SDKs • Eclipse is the most widely used IDE • Alternatives:– IntelliJ IDEA (JetBrains) – NetBeans (Oracle)

Page 9: Java 101  intro to programming with java

Installing Eclipse

• Download and install the latest Eclipse for Java EE (32 Bit version) from http://www.eclipse.org/downloads

• Unzip the content of the archive file you downloaded

• To start Eclipse– On PC, double-click on Eclipse.exe – On Mac, double click Eclipse.app in Application

folder

Page 10: Java 101  intro to programming with java

Hands-on Exercise

Eclipse Setup & Demo

Page 11: Java 101  intro to programming with java

Java 101: Introduction to Java

Language Overview

Page 12: Java 101  intro to programming with java

Java Language Overview

• Object-oriented• Statically typed• Widely available• Widely used

Page 13: Java 101  intro to programming with java

Java Versions • Brief History…

– 1990 : Small team at Sun Microsystems start work on C/C++ replacement

• Major Version Releases– JDK 1.0 (January 21, 1996)– JDK 1.1 (February 19, 1997)– J2SE 1.2 (December 8, 1998)– J2SE 1.3 (May 8, 2000)– J2SE 1.4 (February 6, 2002)– J2SE 5.0 (September 30, 2004)– Java SE 6 (December 11, 2006)– Java SE 7 (July 28, 2011)– Java SE 8 (March 18, 2014)

Page 14: Java 101  intro to programming with java

Java Editions

• Java SE: Java Standard Edition • Java EE: Java Enterprise Edition (a.k.a. J2EE)– includes a set of technologies built on top of Java

SE: Servlets, JSP, JSF, EJB, JMS, et al. • Java ME: Java Micro Edition• Java Card for Smart Cards• All Java programs run inside the Java Virtual

Machine (JVM)

Page 15: Java 101  intro to programming with java

JDK vs. JRE

• Java Development Kit (JDK) is required to develop and compile programs

• Java Runtime Environment (JRE) is required to run programs.

• Users must have JRE installed, • Developers must have the JDK installed• JDK includes the JRE

Page 16: Java 101  intro to programming with java

Java 101: Introduction to Java

How Java Works

Page 17: Java 101  intro to programming with java

How Java Works

Page 18: Java 101  intro to programming with java

Java File Structure

Page 19: Java 101  intro to programming with java

Java 101: Introduction to Java

Writing Your First Program

Page 20: Java 101  intro to programming with java

Hello, World!

Page 21: Java 101  intro to programming with java

Writing Your First Java Program• Create a new project in your IDE named Java101• Create a HelloWorld class in the src folder inside the Java101

project as illustrated below.

Page 22: Java 101  intro to programming with java

Compiling Your First Java Program• Save the HelloWorld class in the IDE• This automatically compiles the HelloWorld.java file

into into a HelloWorld.class file• Go to the folder you created the Java101 project on

your hard disk and open the src folder.• What do you see?

Page 23: Java 101  intro to programming with java

Running Your First Java Program

• Run your program in Eclipse by right-clicking and selecting Run As>Java Application.

Page 24: Java 101  intro to programming with java
Page 25: Java 101  intro to programming with java

Anatomy of a Java ApplicationComments Class Name

Access modifier

Function/static method

Arguments

Page 26: Java 101  intro to programming with java

Language Features

Page 27: Java 101  intro to programming with java

Introduction to Java

Built-in Data Types

Page 28: Java 101  intro to programming with java

Built-in Data Types• Data type are sets of values and operations

defined on those values.

Page 29: Java 101  intro to programming with java

Basic Definitions

• Variable - a name that refers to a value.• Assignment statement - associates a value with a

variable.

Page 30: Java 101  intro to programming with java

String Data Type

Data Type AttributesValues sequence of charactersTypical literals “Hello”, “1 “, “*”Operation ConcatenateOperator +

• Useful for program input and output.

Page 31: Java 101  intro to programming with java

String Data Type

Page 32: Java 101  intro to programming with java

String Data Type• Meaning of characters depends on context.

Page 33: Java 101  intro to programming with java

String Data Type

Expression Value“Hi, “ + “Bob” “Hi, Bob”

“1” + “ 2 “ + “ 1” “ 1 2 1”“1234” + “ + “ + “99” “1234 + 99”

“1234” + “99” “123499”

Page 34: Java 101  intro to programming with java

Hands-on Exercise

Command Line Arguments

Page 35: Java 101  intro to programming with java

Exercise: Command Line Arguments

• Create the Java program below that takes a name as command-line argument and prints “Hi <name>, How are you?”

Page 36: Java 101  intro to programming with java

Integer Data Type

Data Type Attributes

Values Integers between -2E31 to +2E31-1

Typical literals 1234, -99 , 99, 0, 1000000Operation Add subtract multiply divide remainder

Operator + - * / %

• Useful for expressing algorithms.

Page 37: Java 101  intro to programming with java

Integer Data TypeExpression Value Comment

5 + 3 85 – 3 25 * 3 155 / 3 1 no fractional

part5 % 3 2 remainder1 / 0 run-time error

3 * 5 - 2 13 * has precedence

3 + 5 / 2 5 / has precedence

3 – 5 - 2 -4 left associative(3-5) - 2 -4 better style3 – (5-2) 0 unambiguous

Page 38: Java 101  intro to programming with java

Double Data Type

• Useful in scientific applications and floating-point arithmetic

Data Type Attributes

Values Real numbers specified by the IEEE 754 standard

Typical literals 3.14159 6.022e23 -3.0 2.0 1.41421356237209

Operation Add subtract multiply divide

Operator + - * /

Page 39: Java 101  intro to programming with java

Double Data TypeExpression Value

3.141 + 0.03 3.171

3.141 – 0.03 3.111

6.02e23 / 2 3.01e23

5.0 / 2.0 1.6666666666667

10.0 % 3.141 0.577

1.0 / 0.0 Infinity

Math.sqrt(2.0) 1.4142135623730951

Page 40: Java 101  intro to programming with java

Java Math Library

Methods

Math.sin() Math.cos()Math.log() Math.exp()Math.sqrt() Math.pow()

Math.min() Math.max()Math.abs() Math.PI

http://java.sun.com/javase/6/docs/api/java/lang/Math.html

Page 41: Java 101  intro to programming with java

Hands-on Exercise

Integer Operations

Page 42: Java 101  intro to programming with java

Exercise: Integer Operations• Create a Java class named IntOpsin the Java101 project that performs integer

operations on a pair of integers from the command line and prints the results.

Page 43: Java 101  intro to programming with java

Solution: Integer Operations

Page 44: Java 101  intro to programming with java

Boolean Data Type

• Useful to control logic and flow of a program.

Data Type Attributes

Values true or false

Typical literals true false

Operation and or not

Operator && || !

Page 45: Java 101  intro to programming with java

Truth-table of Boolean Operations

a !a a b a && b a || b

true false false false false false

false true false true false true

true false false true

true true true true

Page 46: Java 101  intro to programming with java

Boolean Comparisons

• Take operands of one type and produce an operand of type boolean.

operation meaning true false== equals 2 == 2 2 == 3!= Not equals 3 != 2 2 != 2< Less than 2 < 13 2 < 2

<= Less than or equal

2 <= 2 3 <= 2

> Greater than 13 > 2 2 > 13>= Greater than

or equal3 >= 2 2 >= 3

Page 47: Java 101  intro to programming with java

Type Conversion

• Convert from one type of data to another. • Implicit – no loss of precision– with strings

• Explicit: – cast – method.

Page 48: Java 101  intro to programming with java

Type Conversion Examplesexpression Expression type Expression value“1234” + 99 String “123499”

Integer.parseInt(“123”) int 123(int) 2.71828 int 2

Math.round(2.71828) long 3(int) Math.round(2.71828) int 3(int) Math.round(3.14159) int 3

11 * 0.3 double 3.3(int) 11 * 0.3 double 3.311 * (int) 0.3 int 0

(int) (11 * 0.3) int 3

Page 49: Java 101  intro to programming with java

Hands-on Exercise

Leap Year Finder

Page 50: Java 101  intro to programming with java

Exercise: Leap Year Finder

• A year is a leap year if it is either divisible by 400 or divisible by 4 but not 100.

• Write a java class named LeapYear in the Java101 project that takes a numeric year as command line argument and prints true if it’s a leap year and false if not

Page 51: Java 101  intro to programming with java

Solution: Leap Year Finder

Page 52: Java 101  intro to programming with java

Data Types Summary• A data type is a set of values and operations on those values.

– String for text processing– double, int for mathematical calculation– boolean for decision making

• In Java, you must: – Declare type of values. – Convert between types when necessary

• Why do we need types? – Type conversion must be done at some level. – Compiler can help do it correctly. – Example: in 1996, Ariane 5 rocket exploded after takeoff because of

bad type conversion.

Page 53: Java 101  intro to programming with java

Introduction to Java

Conditionals and Loops

Page 54: Java 101  intro to programming with java

Conditionals and Loops

• Sequence of statements that are actually executed in a program.

• Enable us to choreograph control flow.

Page 55: Java 101  intro to programming with java

Conditionals

• The if statement is a common branching structure. – Evaluate a boolean expression.

• If true, execute some statements. • If false, execute other statements.

Page 56: Java 101  intro to programming with java

If Statement Example

Page 57: Java 101  intro to programming with java

More If Statement Examples

Page 58: Java 101  intro to programming with java

While Loop

• A common repetition structure. – Evaluate a boolean expression. – If true, execute some statements. – Repeat.

Page 59: Java 101  intro to programming with java

For Loop• Another common repetition structure.

– Execute initialization statement. – Evaluate a boolean expression.

• If true, execute some statements. – And then the increment statement. – Repeat.

Page 60: Java 101  intro to programming with java

Anatomy of a For Loop

Page 61: Java 101  intro to programming with java

Loop Examples

Page 62: Java 101  intro to programming with java

For Loop

Page 63: Java 101  intro to programming with java

Hands-on Exercise

Powers of Two

Page 64: Java 101  intro to programming with java

Exercise: Powers of Two• Create a new Java project in Eclipse named Pow2• Write a java class named PowerOfTwo to print powers of 2 that are <= 2N

where N is a number passed as an argument to the program.– Increment i from 0 to N. – Double v each time

Page 65: Java 101  intro to programming with java

Solution: Power of 2

Page 66: Java 101  intro to programming with java

Control Flow Summary• Sequence of statements that are actually executed in a

program. • Conditionals and loops enable us to choreograph the

control flow. Control flow Description Example

Straight line programs

all statements are executed in the order given

Conditionals certain statements are executed depending on the values of certain variables

IfIf-else

Loops certain statements are executed repeatedly until certain conditions are met

while for

do-while

Page 67: Java 101  intro to programming with java

Homework Exercises

Java 101: Introduction to Java

Page 68: Java 101  intro to programming with java

Hands-on Exercise

Random Number Generator

Page 69: Java 101  intro to programming with java

Exercise: Random Number Generator• Write a java class named RandomInt to generate a pseudo-

random number between 0 and N-1 where N is a number passed as an argument to the program

Page 70: Java 101  intro to programming with java

Solution: Random Number Generator

Page 71: Java 101  intro to programming with java

Hands-on Exercise

Array of Days

Page 72: Java 101  intro to programming with java

Exercise: Array of Days

• Create a java class named DayPrinter that prints out names of the days in a week from an array using a for-loop.

Page 73: Java 101  intro to programming with java

Solution: Arrays of Days

public class DayPrinter {public static void main(String[] args) {

//initialize the array with the names of days of the week

String[] daysOfTheWeek =

{"Sunday","Monday","Tuesday","Wednesday",

"Thuesday","Friday”,"Saturday"};

//loop through the array and print their elements to //stdout

for (int i= 0;i < daysOfTheWeek.length;i++ ){System.out.println(daysOfTheWeek[i]);

}}

}

% javac DayPrinter.java

% java DayPrinterSundayMondayTuesdayWednesdayThuesdayFridaySaturday

Page 74: Java 101  intro to programming with java

Hands-on Exercise

Print Personal Details

Page 75: Java 101  intro to programming with java

Exercise: Print Personal Details

• Write a program that will print your name and address to the console, for example:

Alex Johnson23 Main StreetNew York, NY 10001 USA

Page 76: Java 101  intro to programming with java

Hands-on Exercise

Sales Discount

Page 77: Java 101  intro to programming with java

Exercise: Sales Discount• Create a new project in Eclipse named Sale • Create, compile, and run the FriendsAndFamily class as illustrated below • Debug this program in your IDE to find out how it works

Page 78: Java 101  intro to programming with java

Further Reading• Java Tutorials - https://docs.oracle.com/javase/tutorial/ • Java Language Basics -

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html • Eclipse IDE Workbench User Guide - http://help.eclipse.org/kepler/

index.jsp • Eclipse Tutorial - http://www.vogella.com/tutorials/Eclipse/article.html


Recommended