class Employee {
protected String name;
protected int age;
protected String address;
protected String department;
protected int salary;
public Employee() {}
public Employee(String mName, int mAge, String mAddress, String mDepartment) {
name = mName;
age = mAge;
address = mAddress;
department = mDepartment;
}
public void printInfo() {
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
System.out.println("주소 : " + address);
System.out.println("부서 : " + department);
}
}
class Regular extends Employee {
public Regular(String mName, int mAge, String mAddress, String mDepartment) {
super(mName, mAge, mAddress, mDepartment);
}
public void setSalary(int mSalary) {
salary = mSalary;
}
@Override
public void printInfo() {
super.printInfo();
System.out.println("정규직");
System.out.println("월급 : " + salary);
}
}
class Temporary extends Employee {
int workHours;
int perSalary = 10000;
public Temporary(String mName, int mAge, String mAddress, String mDepartment) {
super(mName, mAge, mAddress, mDepartment);
}
public void setWorkHours(int mWorkHours) {
workHours = mWorkHours;
salary = workHours * perSalary;
}
@Override
public void printInfo() {
super.printInfo();
System.out.println("비정규직");
System.out.println("일한 시간 : " + workHours);
System.out.println("급여 : " + salary);
}
}
public class EmployeeTest {
public static void main(String[] args) {
Regular r = new Regular("이순신", 35, "서울", "인사부");
Temporary t = new Temporary("장보고", 25, "인천", "경리부");
r.setSalary(5000000);
r.printInfo();
t.setWorkHours(120);
t.printInfo();
}
}
🟢 super(), super.printInfo()가 이 코드의 포인트!
public class bubbleSort {
public static int[] bubuleSort(int n, int[] arr) {
int tmp;
for(int i = 1; i < n; i++) {
for(int j = 0; j < (n-i); j++) {
if(arr[j] > arr[j+1]) {
tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
}
}
}
return arr;
}
public static void main(String[] args) {
int[] arr = {7,4,5,1,3};
arr = bubuleSort(5, arr);
for(int i = 0; i < 5; i++) {
System.out.println(arr[i]);
}
}
}
🟢 파이썬과는 다르게 tmp변수를 만들어 교체해야 함
🟢 이중 for문에서 n을 1부터 시작한 것과 j < (n-i) 가 포인트 (이해하면 쉬움)
class Shape{
protected int width;
protected int height;
public Shape(int width, int height) {
this.width = width;
this.height = height;
}
public double getArea() {
return (double)width * height;
}
}
class Rectangle extends Shape {
public Rectangle(int width, int height) {
super(width, height);
}
@Override
public double getArea() {
return (double)width * height;
}
}
class Triangle extends Shape {
public Triangle(int width, int height) {
super(width, height);
}
@Override
public double getArea() {
return width * height / 2.0;
}
}
public class java_13_p5 {
public static void main(String[] args) {
Shape[] shape = {new Triangle(10,10),new Rectangle(10,10)};
double sumArea = 0;
for (Shape s : shape) {
sumArea += s.getArea();
}
System.out.println(sumArea);
}
}
🟢 Rectangle과 Triangle에서 getArea()를 오버라이딩