4-1. 실행-Hello World
-
main 안에 코드 작성.그대로 동작이 됨
-
저장을 누르면,
HelloWorldApp.class
&HelloWorldApp.java
2개가 생성됨 -
RUN을 누를 것.
HELLO WORLD가 출력될 것
public class HelloWorldApp{
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
HelloWorldApp.java
- 인간이 이해할 수 있는 텍스트
HelloWorldApp.class
- 이상하게 깨진 문자.
- 컴퓨터가 읽도록 생성된 텍스트이므로!
4-2. JAVA의 동작원리
-
IN : source=code=language
-
f(x) : JAVA
-
OUT : application / program
Java Source Code ( .java )
- 사람만이 이해할수 있음.
Java Application ( .class )
- 기계가 이해하기 위해서 전환(=compile)해줘야 함
- RUN을 누르면, JVM에서 컴퓨터에서 작동시킴
JVM (Java Virtual Machine)
Computer
5. Hello Java World
5-1. Desktop Application 만들기
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Toolkit;
public class HelloWorldGUIApp{
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("HelloWorld GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800, 300));
JLabel label = new JLabel("Hello World!!", SwingConstants.RIGHT);
frame.getContentPane().add(label);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width/2-400/2, dim.height/2-300/2);
frame.pack();
frame.setVisible(true);
}
});
}
}
6-1. 데이터와 연산
다양한 data type들
- Number
- +, - , / , x
- String
- length, substring, search
- etc…
6-2. 데이터 타입
public class Datatype{
public static void main(String[] args) {
System.out.println(6); // Number
System.out.println("six"); // String ( 큰따옴표 주의! )
System.out.println("6"); // String
System.out.println(6+6); // Number+Number
System.out.println("6"+"6"); // String+String
System.out.println("abcd".length()); // length of string
}
}
6-3. 숫자와 연산
public class Number {
public static void main(String[] args) {
// Operator
System.out.println(6+2); // 8
System.out.println(6-2); // 4
System.out.println(6*2); // 12
System.out.println(6/2); // 3
System.out.println(Math.PI); // 3.1415926353...
System.out.println(Math.floor(Math.PI)); // 3.0
System.out.println(Math.ceil(Math.PI)); // 4.0
}
}
6-4. 문자열의 표현
public class StringApp {
public static void main(String[] args) {
System.out.println("Hello World"); // String (n글자) - 큰 따옴표
System.out.println('H'); // character (1글자) - 작은 따옴표
System.out.println("Hello "
+ "World");
System.out.println("Hello \nWorld"); // n = new line
System.out.println("Hello \"World\""); // escape
}
}
6-5. 문자열 다루기
public class String_operation {
public static void main(String[] args) {
System.out.println("Hello World".length());
System.out.println("Hello World".replace("Hello","Bye"));
}
}
8-1. 변수의 정의
public class variable {
public static void main(String[] args) {
int a = 1; // datatype 지정해야
System.out.println(a);
double b = 1.1; // double = real number
System.out.println(b);
String c = "Hello World!";
System.out.println(c);
}
}
8-2. 변수의 효용
public class Letter {
public static void main(String[] args) {
String name="egoing";
System.out.println("Hello,"+name+" ... "+name);
double VAT = 10.0;
System.out.println(VAT);
}
}
8-3. Casting
converting datatype
public class Casting {
public static void main(String[] args) {
double a = 1.1;
double b = 1;
double b2 = (double) 1;
int c = (int) 1.1;
String d = Integer.toString(1);
System.out.println(a);
System.out.println(b);
System.out.println(b2);
System.out.println(c);
System.out.println(d);
System.out.println(d.getClass());
}
}
9. IOT 프로그램 만들기
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Security;
import org.opentutorials.iot.Lighting;
public class OKJavaGoingHome {
public static void main(String[] args) {
String id = "Java APT 507";
// Elevator call
Elevator myElevator = new Elevator(id);
myElevator.callForUp(1);
// Security off
Security mySecurity = new Security(id);
mySecurity.off();
// Light on
Lighting hallLamp = new Lighting(id+" / Hall Lamp");
hallLamp.on();
Lighting floorLamp = new Lighting(id+" / Floor Lamp");
floorLamp.on();
}
}
11. 입력 & 출력
Input -> Program -> Output
Input
- Argument / File / Network / Audio / Program
Output
- Monitor / File / Audio / Program
import javax.swing.JOptionPane;
import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Security;
import org.opentutorials.iot.Lighting;
public class OKJavaGoingHomeInput {
public static void main(String[] args) {
String id = JOptionPane.showInputDialog("Enter a ID");
String bright = JOptionPane.showInputDialog("Enter brightness");
// Elevator call
Elevator myElevator = new Elevator(id);
myElevator.callForUp(1);
// Security off
Security mySecurity = new Security(id);
mySecurity.off();
// Light on
Lighting hallLamp = new Lighting(id+" / Hall Lamp");
hallLamp.on();
Lighting floorLamp = new Lighting(id+" / Floor Lamp");
floorLamp.on();
DimmingLights moodLamp = new DimmingLights(id+" moodLamp");
moodLamp.setBright(Double.parseDouble(bright));
moodLamp.on();
}
}
import javax.swing.JOptionPane;
import org.opentutorials.iot.DimmingLights;
import org.opentutorials.iot.Elevator;
import org.opentutorials.iot.Security;
import org.opentutorials.iot.Lighting;
public class OKJavaGoingHomeInput {
public static void main(String[] args) {
String id = args[0];
String bright = args[1];
// Elevator call
Elevator myElevator = new Elevator(id);
myElevator.callForUp(1);
// Security off
Security mySecurity = new Security(id);
mySecurity.off();
// Light on
Lighting hallLamp = new Lighting(id+" / Hall Lamp");
hallLamp.on();
Lighting floorLamp = new Lighting(id+" / Floor Lamp");
floorLamp.on();
DimmingLights moodLamp = new DimmingLights(id+" moodLamp");
moodLamp.setBright(Double.parseDouble(bright));
moodLamp.on();
}
}
12. Compile
javac -cp ".:lib" OkJavaGoingHome.java
- class 파일의 path :
- 1) “.” : 현재 directory
- 2) “lib” : lib 폴더
- ( 윈도우의 경우 :대신 ;로)
13. Input ( args )
command line에 명령어 실행할때, 2개의 input을 넣어줘야!
-
args[0] & args[1]
-
ex)
java OKJavaGoingHomeInput "Java APT507" 15.0
14. Packages & Class
[ 기본 구조 ]
Package # 1
- Class # 1-1
- variable & method로 구성
- Class # 1-2
- variable & method로 구성
Package # 2
- Class # 2-1
- variable & method로 구성
[ Class ]
- ex) Math 클래스
public class ClassApp {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.floor(1.6));
System.out.println(Math.ceil(1.6));
}
}
[ Instance ]
-
ex)
PrintWriter
는 constructor가 있다 -> instance를 만들 수 있다 -
p1
: instance
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.IOException;
public class InstanceApp {
public static void main(String[] args) throws IOException {
PrintWriter p1 = new PrintWriter("result1.txt");
p1.write("Hello 1");
p1.close();
PrintWriter p2 = new PrintWriter("result2.txt");
p2.write("Hello 2");
p2.close();
}
}
15. 상속
기존에 있는 class를 물려 받으면서, 새로운거 추가!
java.long.Object ………….(a)
- java.io.Writer ………..(b)
- java.io.PrintWriter ………(c)
(c)는 (b)를 상속받아서, (b)는 (a)를 상속받아서 만들어졌다.
16. 나만의 App 만들기
public class AccountingApp {
public static void main(String[] args) {
double valueOfSupply = Double.parseDouble(args[0]);
double vatRate = 0.1;
double expenseRate = 0.3;
double vat = valueOfSupply*vatRate;
double total = valueOfSupply+ vat;
double expense = valueOfSupply*expenseRate;
double income = valueOfSupply - expense;
double dividend1 =income*0.5;
double dividend2 =income*0.3;
double dividend3 =income*0.2;
System.out.println("Value of Supply : "+valueOfSupply);
System.out.println("VAT : "+vat);
System.out.println("Total : "+total);
System.out.println("Expense : "+expense);
System.out.println("Income : "+income);
System.out.println("Dividend 1 : "+dividend1);
System.out.println("Dividend 2 : "+dividend2);
System.out.println("Dividend 3 : "+dividend3);
}
}
eclipse말고 terminal에서 실행하기
- cd ~ ( java파일이 있는 경로로 이동 )
- javac AccountingApp.java
- java AccountingApp 33333.0
17. If문
public class AccountingIfApp {
public static void main(String[] args) {
double valueOfSupply = Double.parseDouble(args[0]);
double vatRate = 0.1;
double expenseRate = 0.3;
double vat = valueOfSupply*vatRate;
double total = valueOfSupply+ vat;
double expense = valueOfSupply*expenseRate;
double income = valueOfSupply - expense;
double dividend1;
double dividend2;
double dividend3;
if(income>10000.0) {
dividend1 =income*0.5;
dividend2 =income*0.3;
dividend3 =income*0.2;
}else {
dividend1 =income*1.0;
dividend2 =income*0.0;
dividend3 =income*0.0;
}
System.out.println("Value of Supply : "+valueOfSupply);
System.out.println("VAT : "+vat);
System.out.println("Total : "+total);
System.out.println("Expense : "+expense);
System.out.println("Income : "+income);
System.out.println("Dividend 1 : "+dividend1);
System.out.println("Dividend 2 : "+dividend2);
System.out.println("Dividend 3 : "+dividend3);
}
}
18. Method
public class AccountingMethodApp {
public static void double valueOfSupply;
public static void double vatRate;
private static double expenseRate;
public static void main(String[] args) {
valueOfSupply = 10000.0;
vatRate = 0.1;
expenseRate = 0.3;
print();
}
private static void print() {
System.out.println("Value of Supply : "+valueOfSupply);
System.out.println("VAT : "+getVAT());
System.out.println("Total : "+getTotal());
System.out.println("Expense : "+getExpense());
System.out.println("Income : "+getIncome());
System.out.println("Dividend 1 : "+getDividend1());
System.out.println("Dividend 2 : "+getDividend2());
System.out.println("Dividend 3 : "+getDividend3());
}
private static double getDividend1() {
return getIncome() *0.5;
}
private static double getDividend2() {
return getIncome() *0.3;
}
private static double getDividend3() {
return getIncome() *0.2;
}
private static void getIncome(){
return valueOfSupply - getExpense();
}
private static void getExpense(){
return valueOfSupply*expenseRate;
}
private static void getTotal(){
return valueOfSupply+ getVAT();
}
private static double getVAT() {
return valueOfSupply*vatRate;
}
}
19. Class
class Accounting{
public static void double valueOfSupply;
public static void double vatRate;
private static double expenseRate;
private static void print() {
System.out.println("Value of Supply : "+valueOfSupply);
System.out.println("VAT : "+getVAT());
System.out.println("Total : "+getTotal());
System.out.println("Expense : "+getExpense());
System.out.println("Income : "+getIncome());
System.out.println("Dividend 1 : "+getDividend1());
System.out.println("Dividend 2 : "+getDividend2());
System.out.println("Dividend 3 : "+getDividend3());
}
private static double getDividend1() {
return getIncome() *0.5;
}
private static double getDividend2() {
return getIncome() *0.3;
}
private static double getDividend3() {
return getIncome() *0.2;
}
private static void getIncome(){
return valueOfSupply - getExpense();
}
private static void getExpense(){
return valueOfSupply*expenseRate;
}
private static void getTotal(){
return valueOfSupply+ getVAT();
}
private static double getVAT() {
return valueOfSupply*vatRate;
}
}
public class AccountingMethodApp {
public static void main(String[] args) {
Accounting.valueOfSupply = 10000.0;
Accounting.vatRate = 0.1;
Accounting.expenseRate = 0.3;
Accounting.print();
}
}
12. Instance
class Accounting{
public static void double valueOfSupply;
public static void double vatRate;
private static double expenseRate;
private static void print() {
System.out.println("Value of Supply : "+valueOfSupply);
System.out.println("VAT : "+getVAT());
System.out.println("Total : "+getTotal());
System.out.println("Expense : "+getExpense());
System.out.println("Income : "+getIncome());
System.out.println("Dividend 1 : "+getDividend1());
System.out.println("Dividend 2 : "+getDividend2());
System.out.println("Dividend 3 : "+getDividend3());
}
private double getDividend1() {
return getIncome() *0.5;
}
private double getDividend2() {
return getIncome() *0.3;
}
private double getDividend3() {
return getIncome() *0.2;
}
private void getIncome(){
return valueOfSupply - getExpense();
}
private void getExpense(){
return valueOfSupply*expenseRate;
}
private void getTotal(){
return valueOfSupply+ getVAT();
}
private double getVAT() {
return valueOfSupply*vatRate;
}
}
public class AccountingMethodApp {
public static void main(String[] args) {
// Instance a1 & a2
Accounting a1 = new Accounting();
Accounting a2 = new Accounting();
a1.valueOfSupply = 10000.0;
a1.vatRate = 0.1;
a1.expenseRate = 0.3;
a1.print();
a2.valueOfSupply = 20000.0;
a2.vatRate = 0.05;
a2.expenseRate = 0.2;
a2.print();
}
}