파일명 : FuncTest1.java
package function;
//객체생성 및 멤버함수의 기본문법
class Animal{
String name; //멤버변수
int age; //멤버변수
void apple(){
System.out.println("사과");
return; //리턴값이 없을경우 생략가능
} //멤버함수
}
public class FuncTest1 {
public static void main(String[] args) {
//main()함수는 프로그램의 시작과 종료를 맡고 있다.
System.out.println("Start");
//객체생성(메모리가 확보되는 시점)
Animal dog=new Animal(); //dog 객체생성
//int a; //a는 변수
//멤버변수와 멤버함수는 반드시 객체명을 통해서 사용가능
dog.age=5;
dog.name="해피";
System.out.println(dog.name);
System.out.println(dog.age);
dog.apple(); //멤버함수 호출
System.out.println("End");
}
}
파일명 : FuncTest2.java
package function;
//전달인자(argument) 및 매개변수(parameter)가 있는 멤버 함수
class Fruit{
String name;
int price;
void test(int n){
System.out.println(n);
return;
}
void test2(char ch, int n){
int p;
for(p=1; p<=n; p++){
System.out.print(ch);
}
return;
}
void test3(int a,int b)
{
if(a>b) //40>50
System.out.println(a);
else
System.out.println(b);
}
}
public class FuncTest2 {
public static void main(String[] args) {
//객체생성
Fruit apple=new Fruit();
apple.name="대구사과";
apple.price=1500;
apple.test(10);//전달인자 1개
apple.test(30);
apple.test2('#',8);
apple.test3(40,50); //둘중 큰수 출력
}
}
파일명 : FuncTest3.java
package function;
//리턴값이 있는 메소드
class Tree
{
String name;
int age;
int test4(int su)
{
int res=1,p;
for(p=su; p>=1; p--)
{
res=res*p; //res*=p
}
return res; //호출한 시점으로 복귀
//리턴값의 자료형(res의 자료형)과 동일하게 해준다
//리턴값이 없을경우에만 void
}
int test5(int p,int q,int r)
{
int max;
max=(p>q) ? p : q;
max=(r>max) ? r : max;
return max;
}
int test6(int p, int q)
{
int sum=0,n;
if(p>q)
{
int temp;
temp=p;
p=q;
q=temp;
}
for(n=p; n<=q; n++)
{
sum=sum+n; //sum+=n
}
return sum;
}
}
public class FuncTest3 {
public static void main(String[] args) {
Tree lime=new Tree();
lime.name="소나무";
lime.age=150;
System.out.println(lime.test4(8));
int m;
m=lime.test5(10,20,23);
System.out.println(m);
int hap;
hap=lime.test6(2,5);
System.out.println(hap);
}
}
//////////////////////////////////////////////
* 요일구하기
파일명 : Weekday.java
package function;
class Calendar
{
boolean leap(int y)
{
if(y%4==0 && y%100!=0 || y%400==0)
return true;
else
return false;
}
}
public class WeekDay {
public static void main(String[] args) {
int year=2012, month=4, day=23;
long hap=0; //총날수
int y,m,d;
Calendar dal=new Calendar();
for(y=1; y<year; y++)
{
if(dal.leap(y))
hap=hap+366;
else
hap=hap+365;
}
int[] mon={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(dal.leap(year)) mon[2]=29;
for(m=1; m<month; m++)
{
hap=hap+mon[m];
}
hap=hap+day;
System.out.print("서기1년1월1일~");
System.out.print(year+"년"+month+"월"+day+"일");
System.out.println(" 총날수="+hap);
switch((int)hap%7){
case 0:System.out.println("일요일"); break;
case 1:System.out.println("월요일"); break;
case 2:System.out.println("화요일"); break;
case 3:System.out.println("수요일"); break;
case 4:System.out.println("목요일"); break;
case 5:System.out.println("금요일"); break;
case 6:System.out.println("토요일"); break;
}
}
}
'..열심히 공부하세.. > JAVA 문법' 카테고리의 다른 글
| [16] 메소드호출방식 (0) | 2012.04.24 |
|---|---|
| [15] Math 클래스 및 Wrapper 클래스 (0) | 2012.04.24 |
| [14] 메소드(Method) 정의 (0) | 2012.04.24 |
| [13] 객체생성 (0) | 2012.04.24 |
| [요일구하는 프로그램] (0) | 2012.04.20 |