일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 자바기초
- 국비
- linux
- 자바기본
- 자바
- 리눅스
- 직장인자기계발
- 웹
- 패스트캠퍼스
- DesignPattern
- Spring
- java기초
- 디자인패턴
- 한번에끝내는JavaSpring웹개발마스터초격차패키지Online
- 디자인
- String
- 자바연습문제
- 스프링
- js
- 데이터베이스
- 패스트캠퍼스후기
- java
- ncs
- 재택근무
- 한번에끝내는JavaSpring웹개발마스터초격차패키지Online강의
- 패캠챌린지
- 자바예제
- 직장인인강
- javabasic
- DB
- Today
- Total
FIF's 코딩팩토리
자바 기초 NCS교육과정(19)-상속 문제풀이 본문
이번 시간은 문제풀이 시간 입니다.
코드를 바로 보지 마시고, 충분한 시간을 가지고 고민해 보세요!
생각하는 시간이 많을수록 실력은 향상됩니다.
Question 1) Prob01_inheritance.java
[주의]테스트를 할때 main( )메소드의 주석을 해제하세요.
[main메소드와 ClassTest_1은 수정하지 마세요]
1 ClassTest_1을 상속받아 ClassTest_2 클래스를 정의한다.
2 하위클래스에 String department 변수를 선언한다.
3 하위클래스를 객체생성시 인수값 3개를 넘겨준다(이름, 연봉, 부서)
4 아래와 같이 출력되도록 상위클래스 getInformation()메소드를 오버라이딩한다.
이름:이지나 연봉:3000 부서:교육부
5 하위클래스에 prn()메소드 오버라이딩
=> "서브클래스"문자열 출력
6 하위클래스에 callSuperThis()메소드 정의
상위의 prn()메소드와 하위의 prn()메소드 호출
[출력결과]
수퍼클래스
서브클래스
이름:이지나 연봉:3000 부서:교육부
class ClassTest_1 { private String name; private int salary; public ClassTest_1() { } ClassTest_1(String n, int s) { name = n; salary = s; } public String getName() { return name; } public int getSalary() { return salary; } public void getInformation() { System.out.println("이름:" + name + " 연봉:" + salary); } public void prn() { System.out.println("수퍼클래스"); } } // end ClassTest_1 //여기에 ClassTest_2클래스를 구현하세요./////// class ClassTest_2 extends ClassTest_1{ } /////////////////////////////////////////////////// public class Prob01_inheritance { public static void main(String[] args) { // 테스트를 할때는 아래 주석을 해제하세요. //ClassTest_2 ct = new ClassTest_2("이지나", 3000, "교육부"); //ct.callSuperThis(); //ct.getInformation(); }// end main() }// end class
Answer 1) Prob01_inheritance.java
class ClassTest_1 { private String name; private int salary; public ClassTest_1() { } ClassTest_1(String n, int s) { name = n; salary = s; } public String getName() { return name; } public int getSalary() { return salary; } public void getInformation() { System.out.println("이름:" + name + " 연봉:" + salary); } public void prn() { System.out.println("수퍼클래스"); } } // end ClassTest_1 //여기에 ClassTest_2클래스를 구현하세요. class ClassTest_2 extends ClassTest_1{ String department; ClassTest_2(String name, int salary, String department){ super(name,salary); this.department=department; } @Override public void getInformation() { System.out.printf("이름:%s 연봉:%d 부서:%s\n",getName(),getSalary(),department); } @Override public void prn() { System.out.println("서브클래스"); } public void callSuperThis(){ super.prn(); this.prn(); } } public class Prob01_inheritance { public static void main(String[] args) { //테스트를 할때는 아래 주석을 해제하세요. ClassTest_2 ct=new ClassTest_2("이지나",3000,"교육부"); ct.callSuperThis(); ct.getInformation(); }// end main() }// end class

Question 2) Prob02_inheritance.java
[주의]테스트를 할때 main( )메소드의 주석을 해제하세요.
[main메소드와 Human클래스는 수정하지 마세요]
1 Human 클래스를 상속 받은 StudentExam 클래스를 구현하시오.
2 3개의 StudentExam 객체를 생성 하여 배열에 셋팅 한 후 각각의
객체의 나이, 신장 출력 한다.
[출력결과]
name 나이 신장 몸무게 학번
홍길동 15 171 81 201101
정길동 13 183 72 201102
박길동 16 175 65 201103
class Human { String name; int age; int height; int weight; Human() { } Human(String name, int age, int height, int weight) { this.name = name; this.age = age; this.height = height; this.weight = weight; } public String toString() { String data = name + "\t" + age + "\t" + height + "\t" + weight; return data; } }//end Human //자손클래스를 여기에 구현하시오. public class Prob02_inheritance { public static void main(String args[]) { //테스트를 할때 아래 주석을 해제하세요. /*StudentExam se[]=new StudentExam[3]; se[0]=new StudentExam("홍길동",15,171, 81, "201101"); se[1]=new StudentExam("정길동",13,183, 72, "201102"); se[2]=new StudentExam("박길동",16,175, 65, "201103"); System.out.printf("%4s %5s %8s %8s %8s\n","name","나이","신장","몸무게","학번"); for(StudentExam sm : se) System.out.println(sm.toString());*/ }//end main() }//end class
Answer 2) Prob02_inheritance.java
class Human { String name; int age; int height; int weight; Human() { } Human(String name, int age, int height, int weight) { this.name = name; this.age = age; this.height = height; this.weight = weight; } public String toString() { String data = name + "\t" + age + "\t" + height + "\t" + weight; return data; } }//end Human //자손클래스를 여기에 구현하시오. class StudentExam extends Human{ String hak; StudentExam(String name, int age, int height, int weight, String hak){ super(name, age, height, weight); this.hak=hak; } @Override public String toString() { String data = name + "\t" + age + "\t" + height + "\t" + weight+"\t"+ hak; return data; } } public class Prob02_inheritance { public static void main(String args[]) { //테스트를 할때 아래 주석을 해제하세요. StudentExam se[]=new StudentExam[3]; se[0]=new StudentExam("홍길동",15,171, 81, "201101"); se[1]=new StudentExam("정길동",13,183, 72, "201102"); se[2]=new StudentExam("박길동",16,175, 65, "201103"); System.out.printf("%4s %5s %8s %8s %8s\n","name","나이","신장","몸무게","학번"); for(StudentExam sm : se) System.out.println(sm.toString()); }//end main() }//end class

Question 3) Prob03_inheritance.java
Movie클래스를 상속받아 MovieWork클래스를 구현하세요.
MovieWork클래스에서 display( )메소드를 오버라이딩해서
아래와 같이 구현하세요.
[출력결과]
영화제목:추격자
감독 : 7, 배우 : 5
영화총점 :12
영화평점 : ☆☆☆☆
***********************************
영화제목:사도세자
감독:9, 배우:10, 작품성:10, 대중성:8, 대본:9
영화총점 : 46
영화평점 : ☆☆☆☆☆
class Movie { String title; // 영화제목 - 멤버변수 int director; // 감독점수 - 멤버변수 int acter; // 배우점수 - 멤버변수 public Movie(String title, int director, int acter) { this.title = title; this.director = director; this.acter = acter; } void display() { int total = director+ acter; String result=""; System.out.printf("영화제목:%s\n",title); System.out.printf("감독 : %d, 배우 : %d\n",director,acter); System.out.printf("영화총점 :%d\n",total); if (total >= 15) result = "☆☆☆☆☆"; else if(total>=12) result = "☆☆☆☆"; else if(total>=10) result = "☆☆☆"; else result = "☆☆"; System.out.println("영화평점 : " + result); } } class MovieWork extends Movie{ private int cinematic; //작품성 private int popular; //대중성 private int scenario; //시나리오 public MovieWork(String title, int director, int acter, int cinematic, int popular, int scenario) { super(title,director, acter); this.cinematic = cinematic; this.popular = popular; this.scenario = scenario; } @Override void display() { //여기를 구현하세요. } } public class Prob03_inheritance { public static void main(String[] args) { Movie mv=new Movie("추격자",7,5); mv.display(); System.out.println("***********************************"); // 매개변수 4개인 생성자로 객체 생성 MovieWork mk = new MovieWork("사도세자", 9, 10, 10,8,9); mk.display(); } }
Answer 3) Prob03_inheritance.java
class Movie { String title; // 영화제목 - 멤버변수 int director; // 감독점수 - 멤버변수 int acter; // 배우점수 - 멤버변수 public Movie(String title, int director, int acter) { this.title = title; this.director = director; this.acter = acter; } void display() { int total = director+ acter; String result=""; System.out.printf("영화제목:%s\n",title); System.out.printf("감독 : %d, 배우 : %d\n",director,acter); System.out.printf("영화총점 :%d\n",total); if (total >= 15) result = "☆☆☆☆☆"; else if(total>=12) result = "☆☆☆☆"; else if(total>=10) result = "☆☆☆"; else result = "☆☆"; System.out.println("영화평점 : " + result); } } class MovieWork extends Movie{ private int cinematic; //작품성 private int popular; //대중성 private int scenario; //시나리오 public MovieWork(String title, int director, int acter, int cinematic, int popular, int scenario) { super(title,director, acter); this.cinematic = cinematic; this.popular = popular; this.scenario = scenario; } @Override void display() { //여기를 구현하세요. int total = director+ acter+cinematic+popular+scenario; String result=""; System.out.printf("영화제목:%s\n",title); System.out.printf("감독 : %d, 배우 : %d, 작품성:%d, 대중성:%d, 대본:%d\n",director,acter,cinematic,popular,scenario); System.out.printf("영화총점 :%d\n",total); if (total >= 15) result = "☆☆☆☆☆"; else if(total>=12) result = "☆☆☆☆"; else if(total>=10) result = "☆☆☆"; else result = "☆☆"; System.out.println("영화평점 : " + result); } } public class Prob03_inheritance { public static void main(String[] args) { Movie mv=new Movie("추격자",7,5); mv.display(); System.out.println("***********************************"); // 매개변수 4개인 생성자로 객체 생성 MovieWork mk = new MovieWork("사도세자", 9, 10, 10,8,9); mk.display(); } }

Question 4) Prob04_inheritance.java
사람의 신장(H)와 몸무게(W)를 입력받아 비만도(B)를 계산하는 프로그램작성
표준체중:SW=(H-100)*0.9
비만도: B(%)=(W-SW)/SW*100
HealthRate 클래스에 아래 사항을 추가합니다.
1 표준체중 계산하는 메소드를 정의한다.
2 비만도를 계산하는 메소드를 정의한다.
비만도가 10% 이내이면 정상, 10%이상 20%이면 과체중, 20% 이상은 비만
3 출력결과
홍길동님의 신장 168 , 몸무게 45 정상입니다.
박둘이님의 신장 168 , 몸무게 90 비만입니다.
class Health { String name; // 이름 double height; // 신장 double weight; // 몸무게 Health(String name, double height, double weight) { this.name = name; this.height = height; this.weight = weight; } public void prn() { System.out.printf("%s님의 키 %d , 몸무게 %d 입니다.\n", name, height, weight); } }// end Health class HealthRate extends Health { public HealthRate(String name, double height, double weight) { super(name, height, weight); } private double standardHealth(){ //표준체중 로직구현을 구현하세요. } private String rateCheck(){ //비만도를 로직구현을 구현하세요 } //prn()메소드 오버라이딩 @Override public void prn() { System.out.printf("%s님의 신장 %.0f , 몸무게 %.0f %s입니다.\n", name, height, weight,rateCheck()); } }// end HealthRate class Prob04_inheritance { public static void main(String[] args) { HealthRate hr = new HealthRate("홍길동", 168, 45); hr.prn(); HealthRate hr2 = new HealthRate("박둘이", 168, 90); hr2.prn(); }//end main() }//end class
Answer 4) Prob04_inheritance.java
class Health { String name; // 이름 double height; // 신장 double weight; // 몸무게 Health(String name, double height, double weight) { this.name = name; this.height = height; this.weight = weight; } public void prn() { System.out.printf("%s님의 키 %d , 몸무게 %d 입니다.\n", name, height, weight); } }// end Health class HealthRate extends Health { public HealthRate(String name, double height, double weight) { super(name, height, weight); } private double standardHealth(){ //표준체중 로직구현 return (height-100)*0.9; } private String rateCheck(){ //비만도를 로직구현 double res=(weight-standardHealth())/standardHealth()*100; if(res<10) return "정상"; else if(res>=10 && res<20) return "과체중"; else return "비만"; } //prn()메소드 오버라이딩 @Override public void prn() { System.out.printf("%s님의 신장 %.0f , 몸무게 %.0f %s입니다.\n", name, height, weight,rateCheck()); } }// end HealthRate class Prob04_inheritance { public static void main(String[] args) { HealthRate hr = new HealthRate("홍길동", 168, 45); hr.prn(); HealthRate hr2 = new HealthRate("박둘이", 168, 90); hr2.prn(); }//end main() }//end class

'Back-End > 국비 NCS교과과정' 카테고리의 다른 글
자바 기초 NCS교육과정(21)-캐스팅과 바인딩 문제풀이 (0) | 2019.07.31 |
---|---|
자바 기초 NCS교육과정(20)-캐스팅과 바인딩 (0) | 2019.07.30 |
자바 기초 NCS교육과정(18)-상속 (0) | 2019.07.30 |
자바 기초 NCS교육과정(17)-접근제어자 문제풀이 (0) | 2019.07.30 |
자바 기초 NCS교육과정(16)-접근제어자 (0) | 2019.07.30 |