Android

겜팔이의 안드로이드 세뇌교실 (자바)

 

  1. 자료형
  2. 배열, 반복문(for, while)
  3. 클래스, 상속
  4. 예외
  5. 인터페이스

 

 

https://www.youtube.com/playlist?list=PLG9ohJAOA2PBtTBlGkmzk6_MuaUQBg_pw

 

 

자료형

 

public class HelloWorld{

 

public static void main(String []args){

int number = 100;

long myBigNumber = 123456123456L;

String myString = “hihihi”;

char myChar = ‘A’;

float myFloat = 123.456f;

System.out.println(number);

System.out.println(myBigNumber);

System.out.println(myString);

System.out.println(myChar);

System.out.println(myFloat);

System.out.println(myString + number);

}

}

 

——————————————————–

 

100

123456123456

hihihi

A

123.456

hihihi100

 

 

 

 

 

배열, 반복문(for, while)

 

import java.util.Random;

 

public class HelloWorld{

 

public static void main(String []args){

Random random = new Random();

int money = 10000;

 

while (money >= 1000) {

money -= 1000;

int number = random.nextInt(100);

int lottoMoney = buyLotto(number);

System.out.println(“My Number is ” + number + ” / Lotto : ” + lottoMoney);

money += lottoMoney;

System.out.println(“My Money is ” + money);

}

}

 

public static int buyLotto(int number) {

int[] lotto = new int[100];

for (int i = 0; i < lotto.length; i++) {

lotto[i] = 0;

}

 

lotto[2] = 100;

lotto[77] = 1000;

lotto[99] = 500;

 

return lotto[number];

}

}

 

 

 

 

 

 

 

 

클래스, 상속

 

 

HelloWorld.java

 

public class HelloWorld{

 

// class extends

 

public static void main(String []args){

 

PlayerCharacter player = new PlayerCharacter(“gamepari”, 70, 12);

EnemyCharacter enemy = new EnemyCharacter(“Orc”, 80, 5);

 

while (player.isLive() && enemy.isLive()) {

player.attack(enemy);

if ( !enemy.isLive() ) break;

enemy.attack(player);

System.out.println(“——————————“);

}

 

if (player.isLive()) {

System.out.println(“player win”);

}

else {

System.out.println(“enemy win”);

}

 

}

}

 

 

 

Character.java

 

public class Character {

 

String name;

int hp;

int atk;

 

public Character(String name, int hp, int atk) {

this.name = name;

this.hp = hp;

this.atk = atk;

}

 

public void attack(Character enemy) {

System.out.println(this.name + ” Attack!”);

enemy.hp -= this.atk;

System.out.println(enemy.name + ” HP : ” + enemy.hp);

}

 

public boolean isLive() {

if (hp <= 0) {

return false;

}

else {

return true;

}

}

 

}

 

 

 

PlayerCharacter.java

 

public class PlayerCharacter extends Character {

 

public PlayerCharacter(String name, int hp, int atk) {

super(name, hp, atk);

}

 

public void heal() {

hp += 20;

System.out.println(name + ” HEAL!!!”);

System.out.println(name + ” HP : ” + hp);

}

 

}

 

 

 

 

EnemyCharacter.java

 

public class EnemyCharacter extends Character {

 

public EnemyCharacter(String name, int hp, int atk) {

super(name, hp, atk);

}

 

@Override

public void attack(Character enemy) {

 

if (hp <= 20) {

System.out.println(“Orc is ANGRY….>0< !!!!”);

this.atk += 15;

}

 

super.attack(enemy);

 

PlayerCharacter player = (PlayerCharacter)enemy;

 

if (player.hp <= 30) {

player.heal();

}

}

 

}

 

 

 

 

 

예외

 

public class HelloWorld {

 

public static void main(String[] args) {

try {

boolean isSuccess = login(“hun”, “1234”);

if (isSuccess) System.out.println(“Login Success”);

else System.out.println(“Login Failed”);

}

catch (Exception e) {

System.out.println(e.getMessage());

}

finally {

System.out.println(“Copyright Hello World”);

}

}

 

public static boolean login(String id, String pw) throws Exception {

 

boolean isNetworkFailed = false;

boolean isNoId = false;

boolean isPasswordWrong = false;

boolean isPWExpired = false;

 

if (isNetworkFailed) throw new Exception(“Network Failed”);

else if (isNoId) throw new Exception(“User ID no exists”);

else if (isPasswordWrong) throw new Exception(“Password wrong”);

else if (isPWExpired) throw new Exception(“Need change password”);

 

return true;

 

}

}

 

// 메서드에 throws Exception 을 추가하면 상위 메서드에 예외를 전달한다는 뜻이다.

// 해당 메서드에서 try~catch 로 처리를 하면 throws Exception 이 필요 없다.

 

 

 

 

 

 

인터페이스

 

1. 다중 상속

2. 콜백 메서드

 

 

 

HelloWorld.java

 

public class HelloWorld {

 

public static void main(String[] args) {

Animal dog = new Dog(“baduk”);

Animal cat = new Cat(“Nyaong”);

Animal wolf = new Wolf(“wooooo”);

 

dog.Cry();

cat.Cry();

wolf.Cry();

 

Pet pet1 = new Cat(“nyaong”);

Pet pet2 = new Dog(“baduk”);

 

pet1.FoodCall();

pet2.FoodCall();

 

((Cat)pet1).Cry();

}

}

 

 

 

Animal.java

 

public abstract class Animal {

 

public String name;

public Animal(String name) { this.name = name; }

public abstract void Cry();

 

}

 

 

 

Pet.java

 

public interface Pet {

public void FoodCall();

}

 

 

 

Dog.java

 

public class Dog extends Animal implements Pet {

 

public Dog(String name) { super(name); }

 

@Override

public void Cry() {

System.out.println(name + “!” + name + “!”);

}

 

@Override

public void FoodCall() {

System.out.println(name + “~~~!”);

}

}

 

 

 

Cat.java

 

public class Cat extends Animal implements Pet {

 

public Cat(String name) { super(name); }

 

@Override

public void Cry() {

System.out.println(name + “~~~~!!!”);

}

 

@Override

public void FoodCall() {

System.out.println(“……”);

}

}

 

 

 

Wolf.java

 

public class Wolf extends Animal implements Pet {

 

public Wolf(String name) { super(name); }

 

@Override

public void Cry() {

System.out.println(name + “~~~~~~~~~~~~~~~~~”);

}

 

@Override

public void FoodCall() {

System.out.println(name + “~~~!”);

}

}

 

 

인터페이스를 이용한 다운로드

 

 

HelloWorld.java

 

public class HelloWorld {

 

public static void main(String[] args) {

Browser browser = new Browser();

browser.imgClick();

}

}

 

 

 

Browser.java

 

public class Browser implements OnDownloadListener {

 

public void imgClick() {

Downloader loader = new Downloader(this);

loader.start();

}

 

@Override

public void onDownFinish()

{

System.out.println(“Browser : onDownFinish()”);

}

 

@Override

public void onDownFailed()

{

System.out.println(“Browser : onDownFailed()”);

}

}

 

 

 

OnDownloadListener.java

 

public interface OnDownloadListener {

public void onDownFinish();

public void onDownFailed();

}

 

 

 

Downloader.java

 

import java.lang.Thread;

import java.lang.InterruptedException;

 

public class Downloader {

 

private OnDownloadListener mListener;

 

public Downloader(OnDownloadListener listener) {

mListener = listener;

}

 

public void start() {

System.out.println(“Download Start”);

 

try {

Thread.sleep(5000);

}

catch (InterruptedException e) {

System.out.println(e.getMessage());

}

 

mListener.onDownFinish();

}

}

 

 

 

 

 

 

 

 

 

Related posts

Leave a Comment