Study/Java

Power Java 9장 프로그래밍

Houkibosi 2012. 6. 28. 15:02

1. 비행기를 나타내는 Plane라는 이름의 클래스를 설계하라.

Plane 클래스는 식별 번호, 모델, 승객수를 필드로 가지고 있다.

1) 필드를 정의하라. 모든 필드는 전용 멤버로 하라.

2) 모든 필드에 대한 접근자와 설정자 메소드를 작성한다.

3) Plane 클래스의 생성자 몇 개를 중복 정의하라. 생성자는 모든 데이터를 받을 수도 있고 아니면 하나도 받지 않을수도 있다.

4) Plane 객체의 현재 상태를 문자열로 반환하는 toString 메소드도 포함시켜라.

5) PlaneTest라는 이름의 테스트 클래스를 만드는데 main()에서 Plane객체 여러 개를 생성하고 접근자와 설정자를 호출하여보라.

6) 정적 변수인 planes를 추가하고, main()에서 0으로 초기화하라.

7) 정적 변수 planes의 값을 반환하는 정적 메소드인 getPlanes()를 추가하고, main()에서 호출하여 보라.


import java.util.*;


class Plane 

{

int ID=0, person=0;

String Model="";

static int planes=0;

public Plane()

{ planes++; }

public Plane(int ID_1)

ID = ID_1;

planes++; 

}

public Plane(int ID_1, String Model_1)

ID = ID_1;

Model = Model_1;

planes++; 

}

public Plane(int ID_1, String Model_1, int person_1)

ID = ID_1;

Model = Model_1;

person = person_1;

planes++; 

}

// ID Get, Set 메소드

public int Get_ID() { return ID ; }  

public void Set_ID( int set_ID ) { ID = set_ID; }

 

// person Get, Set 메소드

public int Get_person() { return person ; }  

public void Set_person( int set_person ) { person = set_person; }

// ID Get, Set 메소드

public String Get_Model() { return Model ; }  

public void Set_Model( String set_Model ) { Model = set_Model; }

public String toString()

{

return ID + "번의 비행기는 " + Model + "모델의 비행기이며 " + person + "의 승객이 탑승중입니다.";

}

public String getPlanes()

{

return planes + "대의 비행기가 있습니다.";

}


}


public class test_1 

{

public static void main(String[] args) 

{

Plane plane_1 = new Plane(1, "ham_200", 200);

Plane plane_2 = new Plane(2, "boing_500", 500);

System.out.println( plane_1.toString() );

System.out.println( plane_2.toString() );

plane_1.Set_Model("boing200");

plane_1.Set_person(50);

plane_2.Set_Model("ham500");

plane_2.Set_person(480);

System.out.println( plane_1.toString() );

System.out.println( plane_2.toString() );

System.out.println( plane_1.getPlanes() );

}

}




2. 상자를 나타내는 Box라는 이름의 클래스를 설계하라.
Box 클래스는 상자의 높이, 너비, 높이를 필드로 가지고 있다. 
박스가 비어 있는지 그렇지 않은지를 나타내는 empty라고 하는 필드도 추가한다.
Box 클래스의 생성자를 중복 정의하라.
생성자는 모든 데이터를 받을 수도 있고, 아니면 하나도 받지 않을 수 있다.
새로 생성된 Box는 비어 있다고 가정한다.

import java.util.*;

class Box 
{
int width=0, lenght=0, height=0;
boolean empty=true;
public Box()
{  }
public Box(int width_1)
width = width_1;
}
public Box(int width_1, int lenght_1)
width = width_1;
lenght = lenght_1;
}
public Box(int width_1, int lenght_1, int height_1)
width = width_1;
lenght = lenght_1;
height = height_1;
}
// width Get, Set 메소드
public int Get_width() { return width ; }  
public void Set_width( int set_width ) { width = set_width; }
 
// person Get, Set 메소드
public int Get_lenght() { return lenght ; }  
public void Set_lenght( int set_lenght ) { lenght = set_lenght; }
// height Get, Set 메소드
public int Get_height() { return height ; }  
public void Set_height( int set_height ) { height = set_height; }
public String toString()
{
return "가로 : " + width + ", 세로 : " + lenght + ", 높이 : " + height + "의 박스입니다.";
}

}

public class test_1 
{
public static void main(String[] args) 
{
Box box = new Box(10, 20, 5);
System.out.println( box.toString() );
}
}


3. 영화를 나타내는 Movie라는 이름의 클래스를 설계하라.
제목, 감독, 제작사를 나타내는 필드를 가진다.
Movie클래스의 생성자를 중복 정의하라.
생성자는 모든 데이터를 받을 수도 있고 아니면 하나도 받지 않을 수 있다.

import java.util.*;

class Movie 
{
String title="", supervision="", maker="";
public Movie()
{  }
public Movie(String title_1)
title = title_1;
}
public Movie(String title_1, String supervision_1)
title = title_1;
supervision = supervision_1;
}
public Movie(String title_1, String supervision_1, String maker_1)
title = title_1;
supervision = supervision_1;
maker = maker_1;
}
// title Get, Set 메소드
public String Get_title() { return title ; }  
public void Set_title( String set_title ) { title = set_title; }
 
// supervision Get, Set 메소드
public String Get_supervision() { return supervision ; }  
public void Set_supervision( String set_supervision ) { supervision = set_supervision; }
// maker Get, Set 메소드
public String Get_maker() { return maker ; }  
public void Set_maker( String set_maker ) { maker = set_maker; }
public String toString()
{
return title + "의 감독은 " + supervision + "이고, 제작사는 " + maker + "입니다.";
}

}

public class test_1 
{
public static void main(String[] args) 
{
Movie movie = new Movie("영화이름", "감독", "제작사");
System.out.println( movie.toString() );
}
}



4. 은행 계좌를 나타내는 BankAccount 라는 이름의 클래스를 설계하라.
BankAccount 클래스는 이름, 계좌번호, 잔액, 이자율을 나타내는 필드를 가진다.
BankAccount 클래스의 생성자를 중복 정의하라.
생성자는 모든 데이터를 받을 수도 있고, 아니면 하나도 받지 않을 수도 있다.

import java.util.*;

class BankAccount  
{
String name="", number="";
int remain=0, interest=0;
public BankAccount()
{  }
public BankAccount(String name_1)
name = name_1;
}
public BankAccount(String name_1, String number_1)
name = name_1;
number = number_1;
}
public BankAccount(String name_1, String number_1, int remain_1)
name = name_1;
number = number_1;
remain = remain_1;
}
public BankAccount(String name_1, String number_1, int remain_1, int interest_1)
name = name_1;
number = number_1;
remain = remain_1;
interest = interest_1;
}
// name Get, Set 메소드
public String Get_name() { return name ; }  
public void Set_name( String set_name ) { name = set_name; }
 
// number Get, Set 메소드
public String Get_number() { return number ; }  
public void Set_number( String set_number ) { number = set_number; }
// remain Get, Set 메소드
public int Get_remain() { return remain ; }  
public void Set_remain( int set_remain ) { remain = set_remain; }
// interest Get, Set 메소드
public int Get_interest() { return interest ; }  
public void Set_interest( int set_interest ) { remain = set_interest; }
public String toString()
{
return name + "님의 계좌번호는 " + number + "이고, 잔액은 " + remain + "이며, 이자율은 " + interest + "입니다.";
}

}

public class test_1 
{
public static void main(String[] args) 
{
BankAccount bank = new BankAccount("이재민", "010-00210-548", 500000, 10);
System.out.println( bank.toString() );
}
}