개발(Web)/Web

[Java] 생활코딩 JAVA method

shinyelee 2020. 12. 28. 17:02

[생활코딩 JAVA method]

Ⅱ. 이미 익숙한 메소드

package projectMethod;

public class FirstMethod {

	public static void main(String[] args) {
		
		System.out.println("Hello Method");
		System.out.println(Math.floor(1.1));
//		Email.send("이메일주소", "Math floor", Math.floor(1.1));
	}

}

Ⅲ. 메소드의 기본 형식

package projectMethod;

public class WhyMethod {

	public static void main(String[] args) {

		// 100000000
		printTwoTimesA();
		// 100000000
		printTwoTimesA();
		// 100000000
		printTwoTimesA();

	}

	public static void printTwoTimesA() {
		System.out.println("-");
		System.out.println("a");
		System.out.println("a");
	}

}

Ⅳ. 메소드의 입력

package projectMethod;

import java.io.FileWriter;
import java.io.IOException;

public class WhyMethod {

	public static void main(String[] args) throws IOException {
		
		System.out.println(twoTimes("a", "-"));
		FileWriter fw = new FileWriter("out.txt");
		fw.write(twoTimes("a", "*"));
		fw.close();
//		Email.send("이메일주소", "two times a", twoTimes("a", "&"));
	}
	
	public static String twoTimes(String text, String delimiter) {
		
		String out = "";
		out = out + delimiter + "\n";
		out = out + text + "\n";
		out = out + text + "\n";
		return out;
	}
	
}

Ⅴ. 메소드의 출력

package projectMethod;

public class OutputMethod {

	public static String a() {
		return "a";
	}
	
	public static int one() {
		return 1;
	}
	
	public static void main(String[] args) {
		System.out.println(a());
		System.out.println(one());
	}

}

Ⅵ. 메소드의 활용

package projectMethod;

class Accounting { // 클래스로 연관된 메소드끼리 정리정돈.
	
	public double valueOfSupply;
    public static double vatRate = 0.1;
    
    public Accounting(double valueOfSupply) {
    	this.valueOfSupply = valueOfSupply;
    }
 
    public double getVAT() {
        return valueOfSupply * vatRate;
    }
    
    public double getTotal() {
        return valueOfSupply + getVAT();
    }
    
}

public class AccountingApp {

    public static void main(String[] args) {
    	
    	Accounting a1 = new Accounting(10000.0);
    	Accounting a2 = new Accounting(20000.0);
    	
        System.out.println("Value of supply : " + a1.valueOfSupply);
        System.out.println("Value of supply : " + a2.valueOfSupply);
        System.out.println("VAT : " + a1.getVAT());
        System.out.println("VAT : " + a2.getVAT());
        System.out.println("Total : " + a1.getTotal());
        System.out.println("Total : " + a2.getTotal());
    }

}

부록 1. access level modifier

package projectMethod;

class Greeting{
    //public, protected, default, private
    public static void hi() {
        System.out.println("Hi");
    }
}

public class AccessLevelModifiersMethod {
	public static void main(String[] args) {
        Greeting.hi();
    }

}

부록 2. static

package projectMethod;

class Print{
	
    public String delimiter;
    
    public void a() {
        System.out.println(this.delimiter);
        System.out.println("a");
        System.out.println("a");
    }
    
    public void b() {
        System.out.println(this.delimiter);
        System.out.println("b");
        System.out.println("b");
         
    }
    public static void c(String delimiter) {
        System.out.println(delimiter);
        System.out.println("b");
        System.out.println("b");
    }
}

public class staticMethod {
     
    public static void main(String[] args) {
//      Print.a("-");
//      Print.b("-");
         
        // instance
        Print t1 = new Print();
        t1.delimiter = "-";
        t1.a();
        t1.b();
        Print.c("$");
         
//      Print.a("*");
//      Print.b("*");
         
        Print t2 = new Print();
        t2.delimiter = "*";
        t2.a();
        t2.b();
    }
}

 

출처 : 

 

JAVA - method - 생활코딩

수업소개 많아진 코드 때문에 고통스럽나요? 이 수업은 서로 연관된 코드를 모아서 이름을 붙이는 정리 정돈의 도구인 메소드를 다루는 수업입니다.메소드를 이용하면 1억 줄의 코드도 단 한단

opentutorials.org

반응형