Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Java
- Linux
- scrollview
- React
- 랜덤번호
- 리액트
- 오버라이딩
- JS
- Javscript
- GoogleMaps
- nodejs
- 랜덤넘버
- 안드로이드
- Kotlin
- 자바스크립트
- npm
- RecyclerView
- fragment
- Hook
- 코틀린
- Android
- 구글맵스
- array
- SpringBoot
- TypeScript
- 스프링부트
- TextView
- stylesheet
- button
- JavaScript
Archives
- Today
- Total
타닥타닥 개발자의 일상
java 상속 문제풀이 3~4 본문
Movie클래스를 상속받아 MovieWork클래스를 구현하세요.
MovieWork클래스에서 display( )메소드를 오버라이딩해서 아래와 같이 구현하세요.
[출력결과]
영화제목: 스파이더맨
감독 : 7, 배우 : 5
영화총점 :12
영화평점 : ☆☆☆☆
***********************************
영화제목: 매트릭스 감독:9, 배우:10, 작품성:10, 대중성:8, 대본:9
영화총점 : 46
영화평점 : ☆☆☆☆☆
Movie | MovieWork |
package movie; public 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; } public 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); } } |
package movie; public 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 public void display() { int total = director + acter + cinematic + popular + scenario; String result = ""; System.out.println("영화제목: "+ title); System.out.printf("감독:%d, 배우:%d\n, 작품성:%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); } } |
MainClass | |
package main; import movie.Movie; import movie.MovieWork; public class MainClass { public static void main(String[] args) { // TODO Auto-generated method stub Movie mv = new Movie("스파이더맨", 7, 5); mv.display(); System.out.println("***********************************"); // 매개변수 4개인 생성자로 객체 생성 MovieWork mk = new MovieWork("매트릭스", 9, 10, 10, 8, 9); mk.display(); } } |
|
출력화면 | |
영화제목:스파이더맨 감독:7, 배우:5 영화총점 :12 영화평점 : ☆☆☆☆ *********************************** 영화제목: 매트릭스 감독:9, 배우:10 , 작품성:10, 대중성:8, 대본:9 영화총점 :46 영화평점 : ☆☆☆☆☆ |
문제 4)
사람의 신장(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 비만입니다.
Health | HealthRate |
package health; public class Health { String name; // 이름 double height; // 신장 double weight;// 몸무게 public Health(String name, double height, double weight) { this.name = name; this.height = height; this.weight = weight; } public void prn() { System.out.printf("%s님의 키 %f , 몸무게 %f 입니다." ,name, height, weight); } } |
package health; public class HealthRate extends Health { public HealthRate(String name, double height, double weight) { super(name, height, weight); } //표준체중 public double standardHealth() { return (height -100)*0.9; } //비만도 public String rateCheck() { double res = (weight - standardHealth())/standardHealth()*100; if(res<10) { return "정상"; } else if(res>=10 && res<20) { return "과체중"; } else { return "비만"; } } @Override public void prn() { super.prn(); System.out.println(rateCheck()+"입니다."); } } |
MainClass | |
package main; import health.HealthRate; public class MainClass { public static void main(String[] args) { HealthRate hr = new HealthRate("홍길동", 168, 45); hr.prn(); HealthRate hr2 = new HealthRate("일지매", 168, 90); hr2.prn(); } } |
|
출력화면 | |
홍길동님의 키 168.000000 , 몸무게 45.000000 입니다.정상입니다. 일지매님의 키 168.000000 , 몸무게 90.000000 입니다.비만입니다. |