Lab7
- 최초 등록일
- 2014.06.28
- 최종 저작일
- 2014.04
- 8페이지/ MS 워드
- 가격 1,000원
목차
1. 문제 : 1. meat, rice, milk로 이루어진 enumeration type인 Food를 정의하여, 각각의 칼로리를 출력하는 프로그램을 완성하시오. 각각의 칼로리는 enumeration class 내에서 정의된다.(3점)
2. 문제 : HauStudent와 HauLecturer를 작성하고 CanIntro 인터페이스를 정의하여 다음 프로그램이 실행되도록 하시오.(3점)
3. 문제 : 3. 다음 Queue 인터페이스를 구현하는 클래스를 ArrayList 클래스를 확장하
4. 문제 : 다음 가변길이 매개변수 리스트를 사용한 함수를 추가하여 다음 프로그램을 완성하시오.
본문내용
(1) 문제 : 1. meat, rice, milk로 이루어진 enumeration type인 Food를 정의하여, 각각의 칼로리를 출력하는 프로그램을 완성하시오. 각각의 칼로리는 enumeration class 내에서 정의된다.(3점)
public class Main {
public static void main(String[] args) {
int total = 0;
for(Food food : Food.values()) {
System.out.println(food + "\t" + food.getCalory());
total += food.getCalory();
}
System.out.println ("Total Calory is "+total);
}
}
<수행화면>
meat 200
rice 50
milk 70
Total Calory is 320
(2) 소스코드
public class New1 {
public static void main(String[] args) {
int total = 0;
for(Food food : Food.values()) {
System.out.println(food + "\t" + food.getCalory());
total += food.getCalory();
}
System.out.println ("Total Calory is "+total);
}
}
enum Food{
meat(200),rice(50), milk(70) ; //enum type 선언
private int calory; //enum type 필드
Food(int calory1){ //enum 타입 변수의 정보값을 새로운 필드에 assign
calory = calory1;
}
int getCalory() //enum 에 저장되어있는 음식 칼로리 출력 메소드
{
return calory;
}
}
참고 자료
없음