함수
package mymain;
import java.util.Scanner;
class MyMath
{
//더하기
public int plus(int a, int b)
{
int c = a + b;
return c;
}
//빼기
public int minus(int a, int b)
{
int c = a - b;
return c;
}
//n까지의 합
public int hap(int n)
{
int sum = 0;
for(int i=1; i<=n; i++)
{
sum = sum + i;
}
return sum;
}
//n까지의 짝수의 합
public int hap_even(int n)
{
int sum = 0;
// for(int i=1; i<=n; i+=2)
for(int i=1; i<=n; i++)
{
if(i%2==0)//0 짝수
sum = sum + i;
}
return sum;
}
}
public class Method1 {
public static void main(String[] args) {
//계산객체 생성
MyMath mm = new MyMath();
int x = 10, y = 5, res;
res = mm.plus(x, y);
System.out.printf("%d + %d = %d\n", x, y, res);
res = mm.minus(x, y);
System.out.printf("%d - %d = %d\n", x, y, res);
res = mm.hap(x);
System.out.printf("%d까지의 합: %d\n", res);
res = mm.hap_even(2);
System.out.printf("%d \n", res);
}
}
======================================
package mymain;
class MyUtil
{
// static : 프로그램 시작시에 이미 생성되어있다(정적메소드)
public static void draw_line()
{
System.out.println("------------------------------------------------");
}
// method overload(중복메소드) : 메소드명을 동일하나 가인자정보가 상이한 메소드
public static void draw_line(int line_size,char line_style)
{
for(int i=0;i<line_size;i++)
System.out.printf("%c",line_style);
System.out.println();//줄바꾸기
}
public static void draw_line(int line_size,String line_style)
{
int len = line_style.length();//문자열 길이
for(int i=0;i<line_size/len;i++)
System.out.printf("%s",line_style);
System.out.println();//줄바꾸기
}
}
public class Method2 {
public static void main(String[] args) {
System.out.println(" 성적관리\n");
int line_size = 50;
char line_style='*';
//클래스명.메소드() <= static 메소드 호출시
MyUtil.draw_line(line_size, "+-*#");
//MyUtil.draw_line();
System.out.println(" 번호 이름 국어 영어 수학 총점 평균 등수");
MyUtil.draw_line(line_size, line_style);
//MyUtil.draw_line();
System.out.println(" 1 홍길동 99 88 88 278 88 1");
System.out.println(" 2 이길동 99 88 88 278 88 1");
line_style='-';
MyUtil.draw_line(line_size, line_style);
//MyUtil.draw_line();
}
}