1.
상속
class Employee{
String name, address, department;
int age;
int salary;
public Employee(){
}
public Employee(String name, int age, String address, String department){
this.name = name;
this.age = age;
this.address = address;
this.department = department;
}
public void set_salary(int salary){
}
public void printInfo(){
}
public void set_workhours(int time){
}
}
class Regular extends Employee{
int salary;
public Regular(String name, int age, String address, String department){
super(name, age, address, department);
}
public void set_salary(int salary){
this.salary = salary;
}
public void printInfo(){
System.out.println("이름 : " + super.name);
System.out.println("나이 : " + super.age);
System.out.println("주소 : " + super.address);
System.out.println("부서 : " + super.department);
System.out.println("정규직 \n" + "월급 : " + salary + "\n");
}
}
class Temporary extends Employee{
int time;
int per_h = 10000;
int salary;
public Temporary(String name, int age, String address, String department){
super(name, age, address, department);
}
public void set_workhours(int time){
this.time = time;
salary = this.per_h * time;
}
public void printInfo(){
System.out.println("이름 : " + super.name);
System.out.println("나이 : " + super.age);
System.out.println("주소 : " + super.address);
System.out.println("부서 : " + super.department);
System.out.println("비정규직 \n" + "일한 시간 : " + time + "\n" + "급여 : " + salary);
}
}
public class 상속_0926_01 {
public static void main(String args[]){
Regular r = new Regular("이순신", 35, "서울", "인사부");
Temporary t = new Temporary("장보고", 25, "인천", "경리부");
r.set_salary(5000000);
r.printInfo();
t.set_workhours(120);
t.printInfo();
}
}
JavaScript
복사
2.
버블정렬
import java.util.Arrays;
public class 버블정렬_0926_02 {
public static void main(String args[]){
int[] arr = {7, 4, 5, 1, 3};
for(int i = 1; i < arr.length; i++) {
for(int j = 0; j < arr.length - i; j++) {
if(arr[i] < arr[j]){
swap(arr, i, j);
}
}
}
for (int a : arr){
System.out.print(a + " ");
}
// 우수인님 코드
// Arrays 내부 sort 메소드를 활용한 정렬 및 출력 - 듀얼피봇 퀵정렬
int[] bubble = {7,4,5,1,3};
Arrays.sort(bubble);
for (int b: bubble) {
System.out.print(b + " ");
}
}
// swap 메소드 - 두 배열의 값을 바꾸는 메소드 만들어 사용
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
Java
복사
3.
상속으로 도형 넓이 구하기
class Shape {
int x, y;
public Shape() {}
public Shape(int x, int y) {
this.x = x;
this.y = y;
}
public int getArea() {
return 0;
}
}
class Rect extends Shape {
public Rect(int x, int y) {
super(x, y);
}
@Override
public int getArea() {
return super.x*super.y;
}
}
class Triangle extends Shape {
public Triangle(int x, int y) {
super(x, y);
}
@Override
public int getArea() {
return (super.x*super.y)/2;
}
}
public class Figure {
public static void main(String[] args) {
Shape[] shape = {new Triangle(10,10), new Rect(10,10)};
int sumArea = 0;
for(Shape s : shape ) {
sumArea += s.getArea();
}
System.out.println(sumArea);
}
}
Java
복사