///////
Search

우수인

문제 1번

class Employee{ String name; int age; String address; String department; int salary; Employee (String name, int age, String address, String department){ this.name = name; this.age = age; this.address = address; this.department = department; } public void printInfo(){ System.out.println("이름: " + name); System.out.println("나이: " + age); System.out.println("주소: " + address); System.out.println("부서: " + department); } } class Regular extends Employee{ Regular(String name, int age, String address, String department) { super(name, age, address, department); } public void setSalary(int salary){ this.salary = salary; } @Override public void printInfo(){ super.printInfo(); System.out.println("정규직"); System.out.println("월급: " + salary); } } class Temporary extends Employee{ int workHours; int timeSalary = 10000; Temporary(String name, int age, String address, String department) { super(name, age, address, department); } public void setWorkHours(int workHours){ this.workHours = workHours; this.salary = workHours*timeSalary; } @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(); } }
Java
복사

문제2

//1. 클래스를 따로 생성하여 처리하는 방법 //class Sort{ // public void bubbleSort(int [] bubble){ // for (int i = 0; i < bubble.length; i++){ // for (int j = 0; j < bubble.length -i -1; j++){ // if (bubble[j] > bubble[j+1]){ // int temp = bubble[j]; // bubble[j] = bubble[j+1]; // bubble[j+1] = temp; // } // } // } // for (int i = 0; i < bubble.length; i++){ // System.out.print(bubble[i] + " "); // } // } //} import java.util.Arrays; public class BubbleSort { //2. 클래스 내에 함수를 생성하여 처리 public void bubbleSort(int [] bubble){ //정렬하는 함수 생성 for (int i = 0; i < bubble.length; i++){ for (int j = 0; j < bubble.length - i - 1; j++){ if (bubble[j] > bubble[j+1]){ int temp = bubble[j]; bubble[j] = bubble[j+1]; bubble[j+1] = temp; } } } //1-1.for문을 활용한 출력 for (int i = 0; i < bubble.length; i++){ System.out.print(bubble[i] + " "); } System.out.println(); //1-2.Arrays toStirng 메소드를 활용한 출력 System.out.println(Arrays.toString(bubble)); } public static void main(String[] args) { int[] bubble = {7,4,5,1,3}; //1. 개별 클래스 생성시 객체 선언 //Sort sort = new Sort(); //2. 클래스 내부 메소드 생성시 객체 선언 BubbleSort sort = new BubbleSort(); sort.bubbleSort(bubble); //Arrays 내부 sort 메소드를 활용한 정렬 및 출력 - 듀얼피봇 퀵정렬 Arrays.sort(bubble); for (int b: bubble) { System.out.print(b + " "); } } }
Java
복사

문제 3

class Shape{ private double width; private double height; public double getArea(){ return 0.0; } } class Triangle extends Shape{ private double width; private double height; public Triangle(double width, double height){ this.width = width; this.height = height; } @Override public double getArea(){ return width*height/2.0; } } class Rectangle extends Shape{ private double width; private double height; public Rectangle(int width, int height){ this.width = width; this.height = height; } @Override public double getArea(){ return width*height; } } public class Poly { 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); } }
Java
복사