1. Employee 상속
문제
- 다음을 만족하는 클래스 Employee를 작성하시오(1에서 3번까지 관련된 문제입니다).
- 클래스 Employee(직원)은 클래스 Regular(정규직)와 Temporary(비정규직)의 상위 클래스
- 필드: 이름, 나이, 주소, 부서, 월급 정보를 필드로 선언
- 생성자 : 이름, 나이, 주소, 부서를 지정하는 생성자 정의
- 메소드 printInfo() : 인자는 없고 자신의 필드 이름, 나이, 주소, 부서를 출력
- 다음을 만족하는 클래스 Regular를 작성하시오.
- 클래스 Regular는 위에서 구현된 클래스 Employee의 하위 클래스
- 생성자 : 이름, 나이, 주소, 부서를 지정하는 상위 생성자 호출
- Setter : 월급 정보 필드를 지정
- 메소드 printInfo() : 인자는 없고 "정규직"이라는 정보와 월급을 출력
- 다음을 만족하는 클래스 Temporary를 작성하시오.
- 클래스 Temporary는 위 클래스 Employee의 하위 클래스
- 필드 : 일한 시간, 시간당 보수를 선언하고 시간당 보수의 초기 값으로 10000 저장
- 생성자 : 이름, 나이, 주소, 부서를 지정하는 상위 생성자 호출
- Setter : 일한 시간을 지정하면서 급여를 일한 시간 * 시간당 보수로 계산하여 저장
- 메소드 printInfo() : 인자는 없고 "비정규직"이라는 정보와 일한 시간과 급여를 출력
- 또한 다음 프로그램으로 클래스 Employee, Regular, Temporary를 점검하는 프로그램 실행
코드
class Employee {
String name;
int age;
String address;
String team;
long salary;
public Employee(String name, int age, String address, String team) {
this.name = name;
this.age = age;
this.address = address;
this.team = team;
}
}
class Regular extends Employee{
public Regular(String name, int age, String address, String team) {
super(name, age, address, team);
}
void setSalary(long salary) {
super.salary = salary;
}
void printInfo() {
System.out.println("이름 : " + super.name);
System.out.println("나이 : " + super.age);
System.out.println("주소 : " + super.address);
System.out.println("부서 : " + super.team);
System.out.println("정규직");
System.out.println("월급 : " + super.salary);
}
}
class Temporary extends Employee {
int workHours;
int salaryPerHour;
public Temporary(String name, int age, String address, String team) {
super(name, age, address, team);
salaryPerHour = 10000;
}
void setWorkHours(int time) {
this.workHours = time;
setSalary();
}
void setSalary() {
super.salary = this.workHours * this.salaryPerHour;
}
void printInfo() {
System.out.println("이름 : " + super.name);
System.out.println("나이 : " + super.age);
System.out.println("주소 : " + super.address);
System.out.println("부서 : " + super.team);
System.out.println("비정규직");
System.out.println("일한 시간 : " + workHours);
System.out.println("급여 : " + super.salary);
}
}
public class HwEmployee {
public static void main(String arg[]) {
Regular r = new Regular("이순신", 35, "서울", "인사부");
Temporary t = new Temporary("장보고", 25, "인천", "경리부");
r.setSalary(5000000);
r.printInfo();
t.setWorkHours(120);
t.printInfo();
}
}
2.
문제
- main 함수에서 다음과 같은 코드가 동작하도록 Shape를 상속받은 Triangle, Rect 클래스를 만들어라.
Shape[] shape = {new Triangle(10,10), new Rect(10,10)};
double sumArea = 0;
for(Shape s : shape ) {
sumArea += s.getArea();
}
System.out.println(sumArea);
코드
- Shape 부모 클래스의 getArea() 함수는 넓이 값을 반환해주는데 부모 클래서는 반환할 값이 없으므로
추상 클래스
, 추상 메서드
를 사용했다.
- Shape를 상속받은 자손 클래스에서 추상 메서드를 꼭 구현해줘야 한다.
abstract class Shape {
protected int x, y;
public Shape() {}
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
abstract public double getArea();
}
class Triangle extends Shape {
public Triangle(){};
public Triangle(int x, int y) {
super(x, y);
}
@Override
public double getArea() {
return super.x * super.y * 0.5;
}
}
class Rect extends Shape {
public Rect() {};
public Rect(int x, int y) {
super(x, y);
}
public double getArea() {
return super.x * super.y;
}
}
public class ShapeHw {
public static void main(String[] args) {
Shape[] shape = {new Triangle(10,10), new Rect(10,10)};
double sumArea = 0;
for(Shape s : shape ) {
sumArea += s.getArea();
}
System.out.println(sumArea);
}
}