Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday,...

Post on 23-Jul-2020

7 views 0 download

Transcript of Introduction to Android App Development · Android App Development EDP 129/A By: Jose Cruz Tuesday,...

Introduction toAndroid App Development

EDP 129/ABy: Jose Cruz

Tuesday, June 25, 13

Course Structure

• No book (resources + blog)• Teach concepts/write code• Attendance• 5-minute break• Homework + Quiz• Take home final exam

Tuesday, June 25, 13

Development Environment

• Java SE JDK

• Goto java.oracle.com• Click on Java SE (under Top Downloads)

• Click Java Download button• Download the JDK version for your OS

• Android ADT Bundle• developer.android.com

• Click on “Get the SDK”• Download SDK - ADT Bundle for [OS]

Tuesday, June 25, 13

What is Android

• Operating system for “mobile” devices• Runs on phones, tablets, Google TV, Google Glass, etc• Created by Google• Free, Open-Source• #1 mobile operating system worldwide

• 75% Android• 17.2% iOS• 3.2% Windows Phone• 2.9% Blackberry• Based on IDC (May 2013)

Tuesday, June 25, 13

Let’s start with Java & Programming

• Java is a Programming Language• Android Apps are written in Java• Before learning Android, we need Java• Learning Java + Learning to program

Tuesday, June 25, 13

Java Principles

• Simple, object-oriented, familiar• Robust and Secure• Portable (compile once/run many

times)• High-performance• Interpreted, threaded, dynamic

Tuesday, June 25, 13

Java Tools

• javac - compiler• java - run time to run apps• jar - a packager to package apps• javadoc - to create documentation

Tuesday, June 25, 13

Hello World in Java

class HelloWorld {

public static void main(String[] args) { System.out.println("Hello, World"); }}

Tuesday, June 25, 13

Programming Concepts

• Object• Class• Field, Method• Variable• Data Type• Array• Loops• Other syntax

Tuesday, June 25, 13

Objects

• Entity which has state and behavior• Example: Dog

• state: name, color, breed, hungry• behavior: bark, fetch, wag tail

• Example: Smartphone• state: color, brand, cpu speed, ram• behavior: place call, send text, browse

Tuesday, June 25, 13

Objects (Cont)

• Another way to say it:

• An object has properties• And object can perform some actions

• Example: Calculator• properties: color, brand, energy source

• actions: add, subtract, get square root• Example: Car

• properties: make, model, horse power• actions: turn on, accelerate, break

Tuesday, June 25, 13

Objects - Software

• Object Oriented Programming• State/properties = fields • Actions = methods (aka functions)

Tuesday, June 25, 13

Class

• A class is a definition from which an object is created

• A class is a blueprint from which an object is created

Tuesday, June 25, 13

Anatomy of a Java Class

class Car {...

}

Tuesday, June 25, 13

Anatomy of a Class (continued)

class Car {String make;String model; ...}

Tuesday, June 25, 13

Anatomy of a Class (contintued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

...}

Tuesday, June 25, 13

Anatomy of a Class(continued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

void turnOn() {...

}}

Tuesday, June 25, 13

Anatomy of a Class(continued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

void turnOn() { engineStatus = “on”; }}

Tuesday, June 25, 13

Anatomy of a Class(continued)

class Car { String make; String model; String engineStatus; int currentSpeed; int topSpeed;

void turnOn() {engineStatus = “on”;

}

void turnOff() { engineStatus = “off”; }}

Tuesday, June 25, 13

Class Example

class Multiplier { int firstNumber; int secondNumber; int result;

void multiply() { result = firstNumber * secondNumber; } void displayResult() { System.out.println(“Result is: ” + result); }}

Tuesday, June 25, 13

The Entry Point

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

• Java apps have an entry point• Only one entry point per app

Tuesday, June 25, 13

Creating the Objectclass Main { public static void main(String[] args) { // step 1: declare object Multiplier myObject; // step 2: create object myObject = new Multiplier(); // step 3: use the object to accomplish task myObject.firstNumber = 4; myObject.secondNumber = 18; myObject.multiply(); myObject.displayResult(); }}

Tuesday, June 25, 13

Things to Note

• A program usually has at least two classes:• One class which has ability to solve problem• The ‘Main’ class which actually solves it

• Before an object is used, it must be declared and created

• Syntax to declare is: ClassName objectName;• Syntax to create is: objectName = new ClassName();• Object’s fields/method accessed with dot notation

• myObject.fieldOrMethod

Tuesday, June 25, 13

Conventions

• Class names use Pascal casing• Car, SportsCar, Multiplier

• Class fields/methods use camel casing• firstNumber, topSpeed, myObject

• Comments delimit sections of code

Tuesday, June 25, 13

Homework

• Set up your dev environment• Install Java SE JDK• Install ADT Bundle• Set up your workspace directory

• Create a program to calculate average of 3 numbers and display

Tuesday, June 25, 13

Variables

• All programs work in the same way:• They get input from user• They do something with that input

• Examples:• Place online order• Place a call• Printer• Multiplier

Tuesday, June 25, 13

Variables (Continued)

• A variable is a placeholder for data• Called “variable” because its value can

vary each time the program runs• The programmer uses a name for the

place holder• All class fields are variables, but not all

variables are fields

Tuesday, June 25, 13

Data Types (Primitive)

Data Type Bits Min Value Max Value

byte 8 -128 127

short 16 -32,768 32,767

int 32 -2,147,483,648 2,147,483,647

long 64 -9.22E+18 9.22E+18

float 32 decimals decimals

double 64 decimals decimals

char 16 single character single character

boolean - true/false true/false

Tuesday, June 25, 13

Naming Rules

• Names can have letters and digits• Also _ character• Cannot begin with a number• Case-sensitive

byte firstNumber = 10; // Okshort 2ndNum = 20; // NOT OKint num2 = 30520; // Okdouble third_num = 3.14d; // Ok

Tuesday, June 25, 13

Examples

• Variables must be declared• Variable declaration examples:

byte firstNumber = 10;short secNum = 20;int thirdNum = 30520;

Tuesday, June 25, 13

More Examples

• Long, float, double require letter:

long forthNum = 50L;float fifthNum = 3.14f;double sixthNum = 10.876d;

• Letter can be upper or lower case• By convention, L is upper case, while d

and f are lowercase

Tuesday, June 25, 13

More Examples

• Long, float, double require letter:

long forthNum = 50L;float fifthNum = 3.14f;double sixthNum = 10.876d;

• Letter can be upper or lower case• By convention, L is upper case, while d

and f are lowercase

Tuesday, June 25, 13

Data Types (complex)

• Complex Data types hold more than a single value• String• Date• Objects

Tuesday, June 25, 13