Top Banner
Basic JAVA for Android Development Nattapong Tonprasert
28

Basic java for Android Developer

May 10, 2015

Download

Software

Getting Starts Basic java for Android Developer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basic java for Android Developer

Basic JAVA for Android Development

Nattapong Tonprasert

Page 2: Basic java for Android Developer

Reference

• Head First Java, O'Reilly

Page 3: Basic java for Android Developer

Introduction

Page 4: Basic java for Android Developer

The Way Java Works

Page 5: Basic java for Android Developer

Installation Java Platform (JDK)

• Download : http://www.oracle.com/technetwork/java/javase/downloads/index.html

Page 6: Basic java for Android Developer

Look how easy it is to write java

Page 7: Basic java for Android Developer

Look how easy it is to write java

Page 8: Basic java for Android Developer

Code structure in Java

Page 9: Basic java for Android Developer

Code structure in Java

source file

class file

method 1 statement

method 2 statement

Page 10: Basic java for Android Developer

Code structure in Java

public class Dog { !!!!}

class

public class Dog { void bark () { !! } }

method

public class Dog { void bark () { statement1;!! ! statement2; } }

statements

Page 11: Basic java for Android Developer

Anatomy of a class

Page 12: Basic java for Android Developer

Writing a class with a main

public class MyFirstApp {! public static void main (String[] args) { System.out.println("I Rule!"); System.out.println("The World!"); } }

Page 13: Basic java for Android Developer

Statements

int x = 3;String name = "Dirk";x = x * 17;System.out.print("x is " + x);double d = Math.random();// this is a comment

Page 14: Basic java for Android Developer

Conditionif (x == 10) { System.out.println("x must be 10");} else { System.out.println("x isn't 10");}!if ((x < 3) && (name.equals("Dirk"))) { System.out.println("Gently");}!if ((x < 3) || (name.equals("Dirk"))) { System.out.println("Gently");}

Page 15: Basic java for Android Developer

Looping

while (x > 12) { x = x - 1; System.out.println("x is " + x);}!for (x = 0; x < 10; x = x + 1) { System.out.println("x is " + x);}

Page 16: Basic java for Android Developer

Classes and Objects

Page 17: Basic java for Android Developer

Classes and Objects

Page 18: Basic java for Android Developer

Classes and Objects

Page 19: Basic java for Android Developer

Classes and Objects

Page 20: Basic java for Android Developer

thinking about objects

When you design a class, think about the objects that will be created from that class type. Think about

• things the object knows

• things the object does

Page 21: Basic java for Android Developer

thinking about objects

Page 22: Basic java for Android Developer

What’s the difference between a class and an object?

• A class is not an object (but it’s used to construct them)

• A class is a blueprint for an object

Page 23: Basic java for Android Developer

Encapsulation

• public

• private - self class

• protected - self package

Page 24: Basic java for Android Developer

Class GoodDogclass GoodDog { private int size; public int getSize() { return size; } public void setSize(int s) { size = s; } void bark() { if (size > 60) { System.out.println("Woof! Woof!"); } else if (size > 14) { System.out.println("Ruf! Ruf!"); } else { System.out.println("Yip! Yip!"); } }}

Page 25: Basic java for Android Developer

GoodDog Test Drive

public class GoodDogTestDrive { public static void main (String[] args) { GoodDog one = new GoodDog(); one.setSize(70); GoodDog two = new GoodDog(); two.setSize(8); System.out.println("Dog one: " + one.getSize()); System.out.println("Dog two: " + two.getSize()); one.bark(); two.bark(); }}

Page 26: Basic java for Android Developer

Static variables

Page 27: Basic java for Android Developer

Static variables

Page 28: Basic java for Android Developer

Static variables

class Player { static int playerCount = 0; private String name;! public Player(String n) { name = n; playerCount++; }}!public class PlayerTestDrive { public static void main(String[] args) { System.out.println(Player.playerCount); Player one = new Player("Tiger Woods"); System.out.println(Player.playerCount); }!}